纯Mybatis与Mybatis-Plus整合
-- 创建测试表
CREATE TABLE `tb_user` (
`id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_name` VARCHAR ( 20 ) NOT NULL COMMENT '用户名',
`password` VARCHAR ( 20 ) NOT NULL COMMENT '密码',
`name` VARCHAR ( 30 ) DEFAULT NULL COMMENT '姓名',
`age` INT ( 11 ) DEFAULT NULL COMMENT '年龄',
`email` VARCHAR ( 50 ) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY ( `id` )
) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
-- 插入测试数据
INSERT INTO `tb_user` ( `id`, `user_name`, `password`, `name`, `age`, `email` ) VALUES( '1', 'zhangsan', '123456', '张三', '18', '[email protected]' );
INSERT INTO `tb_user` ( `id`, `user_name`, `password`, `name`, `age`, `email` ) VALUES( '2', 'lisi', '123456', '李四', '20', '[email protected]' );
INSERT INTO `tb_user` ( `id`, `user_name`, `password`, `name`, `age`, `email` ) VALUES( '3', 'wangwu', '123456', '王五', '28', '[email protected]' );
INSERT INTO `tb_user` ( `id`, `user_name`, `password`, `name`, `age`, `email` ) VALUES( '4', 'zhaoliu', '123456', '赵六', '21', '[email protected]' );
INSERT INTO `tb_user` ( `id`, `user_name`, `password`, `name`, `age`, `email` ) VALUES( '5', 'sunqi', '123456', '孙七', '24', '[email protected]' );
com.baomidou</groupId>
mybatis-plus</artifactId>
3.1.1</version>
</dependency>
mysql</groupId>
mysql-connector-java</artifactId>
5.1.47</version>
</dependency>
com.alibaba</groupId>
druid</artifactId>
1.0.11</version>
</dependency>
org.projectlombok</groupId>
lombok</artifactId>
true</optional>
1.18.4</version>
</dependency>
junit</groupId>
junit</artifactId>
4.12</version>
</dependency>
org.slf4j</groupId>
slf4j-log4j12</artifactId>
1.6.4</version>
</dependency>
</dependencies>
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
第一步,编写mybatis-config.xml文件
"1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
"development"> "development"> type="JDBC" /> type="POOLED"> "driver" value="com.mysql.jdbc.Driver" /> "url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false" /> "username" value="root" /> "password" value="123456" /> </dataSource> </environment> </environments> "UserMapper.xml" /> </mappers> </configuration> 第二步,编写User实体对象
@Data @NoArgsConstructor @AllArgsConstructor public class User { private Long id; private String userName; private String password; private String name; private Integer age; private String email; }
第三步,编写UserMapper接口
public interface UserMapper { List<User> findAll(); }
第四步,编写UserMapper.xml文件
"1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"cn.com.javakf.mapper.UserMapper"> <select id="findAll" resultType="cn.com.javakf.pojo.User"> select * from tb_user </select> </mapper> 第五步,编写TestMybatis测试用例
public class TestMybatis { @Test public void testFindAll() throws Exception { String config = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(config); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 测试查询 List<User> users = userMapper.findAll(); for (User user : users) { System.out.println(user); } } }
测试结果
[main] [cn.com.javakf.mapper.UserMapper.findAll]-[DEBUG] ==> Preparing: select * from tb_user [main] [cn.com.javakf.mapper.UserMapper.findAll]-[DEBUG] ==> Parameters: [main] [cn.com.javakf.mapper.UserMapper.findAll]-[DEBUG] <== Total: 5 User(id=1, userName=null, password=123456, name=张三, age=18, email=zhangsan@163.com) User(id=2, userName=null, password=123456, name=李四, age=20, email=lisi@163.com) User(id=3, userName=null, password=123456, name=王五, age=28, email=wangwu@163.com) User(id=4, userName=null, password=123456, name=赵六, age=21, email=zhaoliu@163.com) User(id=5, userName=null, password=123456, name=孙七, age=24, email=sunqi@163.com)
第一步,将UserMapper继承BaseMapper,将拥有了BaseMapper中的所有方法
public interface UserMapper extends BaseMapper<User> { List<User> findAll(); }
第二步,使用Mybatis-Plus中的MybatisSqlSessionFactoryBuilder进行构建
public class TestMybatisPlus { @Test public void testFindAll() throws Exception { String config = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(config); SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 测试查询 // List
users = userMapper.findAll(); List<User> users = userMapper.selectList(null); for (User user : users) { System.out.println(user); } } }运行报错
>
解决:在User对象中添加@TableName,指定数据库表名@Data @NoArgsConstructor @AllArgsConstructor @TableName("tb_user") public class User { private Long id; private String userName; private String password; private String name; private Integer age; private String email; }
测试结果
[main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] ==> Preparing: SELECT id,user_name,password,name,age,email FROM tb_user [main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] ==> Parameters: [main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] <== Total: 5 User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=zhangsan@163.com) User(id=2, userName=lisi, password=123456, name=李四, age=20, email=lisi@163.com) User(id=3, userName=wangwu, password=123456, name=王五, age=28, email=wangwu@163.com) User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=zhaoliu@163.com) User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=sunqi@163.com)
简单说明
由于使用了MybatisSqlSessionFactoryBuilder进行了构建,继承的BaseMapper中的方法就载入到了SqlSession中,所以就可以直接使用相关的方法
代码托管:mybatis-mybatis-plus