官网:https://baomidou.com
GitHub:GitHub - baomidou/mybatis-plus: An powerful enhanced toolkit of MyBatis for simplify development
Gitee:mybatis-plus: mybatis 增强工具包,简化 CRUD 操作。 文档 http://baomidou.com低代码组件库 http://aizuda.com
Mybatis-plus 在Spring中的使用
新建数据库
建表语句
CREATE TABLE `employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`admin` bit(1) DEFAULT NULL,
`dept_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
插入数据
INSERT INTO `employee` VALUES (1, 'admin', '1', 'admin@abc.com', 40, b'1', 6);
INSERT INTO `employee` VALUES (2, '赵总', '1', 'zhaoz@langfeiyes.com', 35, b'0', 1);
INSERT INTO `employee` VALUES (3, '赵一明', '1', 'zhaoym@langfeiyes.com', 25, b'0', 1);
INSERT INTO `employee` VALUES (4, '钱总', '1', 'qianz@langfeiyes.com', 31, b'0', 2);
INSERT INTO `employee` VALUES (5, '钱二明', '1', 'qianem@langfeiyes.com', 25, b'0', 2);
INSERT INTO `employee` VALUES (6, '孙总', '1', 'sunz@langfeiyes.com', 35, b'0', 3);
INSERT INTO `employee` VALUES (7, '孙三明', '1', 'sunsm@langfeiyes.com', 25, b'0', 3);
INSERT INTO `employee` VALUES (8, '李总', '1', 'liz@langfeiyes.com', 35, b'0', 4);
INSERT INTO `employee` VALUES (9, '李四明', '1', 'lism@langfeiyes.com', 25, b'0', 4);
INSERT INTO `employee` VALUES (10, '周总', '1', 'zhouz@langfeiyes.com', 19, b'0', 5);
INSERT INTO `employee` VALUES (11, '周五明', '1', 'zhouwm@langfeiyes.com', 25, b'0', 5);
INSERT INTO `employee` VALUES (12, '吴总', '1', 'wuz@langfeiyes.com', 41, b'0', 5);
INSERT INTO `employee` VALUES (13, '吴六明', '1', 'wulm@langfeiyes.com', 33, b'0', 5);
INSERT INTO `employee` VALUES (14, '郑总', '1', 'zhengz@langfeiyes.com', 35, b'0', 3);
INSERT INTO `employee` VALUES (15, '郑七明', '1', 'zhengqm@langfeiyes.com', 25, b'0', 2);
INSERT INTO `employee` VALUES (16, '孙四明', '1', 'sunsim@langfeiyes.com', 25, b'0', 3);
INSERT INTO `employee` VALUES (17, '孙五明', '1', 'sunwm@langfeiyes.com', 25, b'0', 3);
INSERT INTO `employee` VALUES (18, '李五明', '1', 'liwm@langfeiyes.com', 25, b'0', 4);
INSERT INTO `employee` VALUES (19, '李六明', '1', 'lilm@langfeiyes.com', 25, b'0', 4);
INSERT INTO `employee` VALUES (20, '叶子', '1', 'yezi@langfeiyes.com', 0, b'0', 1);
创建项目
新建个Maven工程
配置下maven
引入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.3.4</version>
</dependency>
<!--数据连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--mysql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--自动生成getter/setter方法依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<!--spring环境依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<!--spring事务依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<!--spring jdbc依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<!--spring 日志依赖包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
在resources目录下配置数据db.properties、日志文件log4j.properties、spring容器文件spring-context-mybatis.xml
实体类
创建测试类,注意包路径
@Test
public void testSave(){
Employee employee = new Employee();
employee.setAdmin(1);
employee.setAge(18);
employee.setDeptId(1L);
employee.setEmail("yezi@langfeiyes.com");
employee.setName("yezi");
employee.setPassword("111");
employeeMapper.insert(employee);
}
@Test
public void testUpdate(){
Employee employee = new Employee();
employee.setId(1L);
employee.setAdmin(1);
employee.setAge(18);
employee.setDeptId(1L);
employee.setEmail("yezi@langfeiyes.com");
employee.setName("yezi");
employee.setPassword("111");
employeeMapper.updateById(employee);
}
@Test
public void testDelete(){
employeeMapper.deleteById(1L);
}
@Test
public void testGet(){
System.out.println(employeeMapper.selectById(1L));
}
@Test
public void testList(){
System.out.println(employeeMapper.selectList(null));
}
Mybatis-plus SpringBoot版本
新建个工程
引入devtools热部署 mysql驱动 mybatis框架 lombok SpringBoot web容器 不然无法运行(如果单纯在测试类里测试 不引入Web也可以)
Spring Data JDBC 可以帮助检查数据源设置
将SpringBoot版本改成了2.7.2 不为什么
额外引入了德鲁伊和Mybatis-plus 去掉Mybatis框架 Mybatis-plus只做升级,本身还是Mybatis
顺便配置下maven,SpringBoot自带的maven很多莫名错误
代码还是上面Spring的代码
application.properties
其实驼峰配不配都一样
server.port=8088
spring.devtools.restart.enabled=true
#设置重启目录
spring.devtools.restart.additional-paths=src/main/java
#排除目录
spring.devtools.restart.exclude=static/**
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis-plus?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置sql打印日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#Mybatis-plus配置
#指定实体包名
mybatis-plus.type-aliases-package=com.example.mybatisplusspringboot.pojo
#指定.xml路径
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
#开启驼峰命名
mybatis-plus.configuration.map-underscore-to-camel-case=true