MyBatis~(壹)初探.
项目代码
简介
MyBatis 是优秀的持久层框架,简单来说就是方便存-取数据
的工具.
持久层: Dao 层、Service 层、Controller 层
之前听说过
MyBatis-plus
,这里贴一篇二者的区别下图中蓝色小鸟就是 MyBatis-plus
Hello-MyBatis
环境配置
用 Maven 创建空项目,
导入依赖
&设置 Maven 过滤器
:<!--导入依赖-->
<dependencies>
<!--mysqlq驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
<!--Mapper资源导出,不设置的话会报错-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>然后创建空的子模块,子模块中开发就不需要再导入依赖了,MyBatis 需要添加一个配置文件:
src/main/resources/MyBatis-config.xml
- 后面每增加一个 Mapper.xml,都需要在这里面添加 Mapper 映射;后面会提到.
<!--configuration核心配置文件-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
数据库
初始化数据库和 pojo
create schema User;
create table User
(
id int not null,
name varchar(30) default null null,
pwd varchar(30) default null null,
constraint User_pk
primary key (id)
) ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;
insert into User(id, name, pwd)
values (1, '张三', 123456),
(2, '李四', 123456),
(3, '王五', 123456),
(4, '赵六', 123456)src/main/java/pojo/User.java
package pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
public class User {
private int id;
private String name;
private String pwd;
}
工具类
src/main/java/utils/MyBatisUtils.java
package utils; |
Dao
dao 中编写
接口
和绑定Mapper.xml
https://github.com/Weidows/Java/blob/master/MyBatis/1-hello-MyBatis/src/main/java/dao
这里的 xml 配置绑定好接口,就可以让 MyBatis 负责 SQL 代码了,避免了 dao 层中 JDBC 代码过于繁杂和重复的问题
<!--namespace=绑定一个指定的Dao/Mapper接口-->
<mapper namespace="dao.UserMapper">
<!--这里id就是方法名,相当于实现方法-->
<select id="getUserList" resultType="pojo.User">
select *
from User
</select>
</mapper>
Mapper.xml 绑定的是与之对应的接口,然后需要在
MyBatis-config.xml
中绑定需要的Mapper.xml
绑定关系
: UserMapper.java -> UserMapper.xml -> MyBatis-config.xml添加绑定如下:
<!--configuration核心配置文件-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--绑定Mapper.xml-->
<mappers>
<mapper resource="dao/UserMapper.xml"/>
</mappers>
</configuration>
测试
注意 test 中层级与 main 中层级一一对应
package dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import utils.MyBatisUtils;
import java.util.List;
public class UserMapperTest {
public void test() {
//1.获取SqlSession对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//2.执行SQL
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
public void test1() {
//1.获取SqlSession对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//2.执行SQL
List<User> userList = sqlSession.selectList("dao.UserMapper.getUserList");
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
}排错:
org.apache.ibatis.binding.BindingException: Type interface dao.UserDao is not known to the MapperRegistry.
这是因为 Mapper.xml 资源没有被 Maven 导出,所以找不到资源,解决办法:资源导出
1字节的UTF-8序列的字节1无效的问题处理
这个问题格外 dan 疼,把下面的
UTF-8
改为UTF8
试试…
开始 CRUD
完善 CRUD 只需要修改下面三个文件:
src/main/java/dao/UserMapper.java
package dao;
import pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}src/main/java/dao/UserMapper.xml
<!--namespace=绑定一个指定的Dao/Mapper接口-->
<mapper namespace="dao.UserMapper">
<!--这里id就是方法名,相当于实现方法-->
<select id="getUserList" resultType="pojo.User">
select *
from User
</select>
<select id="getUserById" resultType="pojo.User" parameterType="int">
select *
from mybatis.user
where id = #{id}
</select>
<insert id="addUser" parameterType="pojo.User">
insert into mybatis.user(id, name, pwd)
values (#{id}, #{name}, #{pwd})
## 对象中的属性可以被直接取出来(这里取得可以是传入的参数,也可以是传入对象的属性)
</insert>
<update id="updateUser" parameterType="pojo.User">
update mybatis.user
set name =#{name},
pwd=#{pwd}
where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete
from mybatis.user
where id = #{id}
</delete>
</mapper>src/test/java/dao/UserMapperTest.java
package dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import utils.MyBatisUtils;
import java.util.List;
public class UserMapperTest {
public void test() {
//1.获取SqlSession对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//2.执行SQL
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
public void test1() {
//1.获取SqlSession对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//2.执行SQL
List<User> userList = sqlSession.selectList("dao.UserMapper.getUserList");
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
public void getUserById() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.getUserById(3));
sqlSession.close();
}
public void addUser() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.addUser(new User(5, "五号", "123456"));
if (i > 0) {
//注意 增删改 需要提交事务
sqlSession.commit();
}
sqlSession.close();
}
public void updateUser() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.updateUser(new User(5, "⑤号", "654321"));
if (i > 0) {
sqlSession.commit();
}
sqlSession.close();
}
public void deleteUser() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUser(5);
if (i > 0) {
sqlSession.commit();
}
sqlSession.close();
}
}
通过 Map 优化
//用Map插入用户 |
<!--通过USer传参的话,必须用User的属性名取出; 而通过map传参的话,属性名可以任意--> |
@Test |