搜索
首页
笔记
案例
关于
课程列表
MyBatis-Plus
CURD
自动填充
条件构造器
ActiveRecord
代码生成器
插件
逻辑删除
课程导航
计算机基础知识
C
Linux
linux常用软件
计算机网络
程序员修养
设计模式
工具
Git
composer
vim
IntelliJ IDEA
wireshark
laravel
Spring
SpringMVC
Maven
数据库
MySQL
Redis
MongoDB
JDBC
Mybatis
MyBatis-Plus
服务端编程
PHP
Java
shell script
JavaWeb
HTML / CSS
HTML
CSS
HTML5
CSS3
BOOTSTRAP
JavaScript
JavaScript
JQuery
layui
自动填充
CURD
## Spring中引入MybatisPlus ### 准备工作 #### 创建maven工程 略 #### 准备数据表 ```SQL CREATE TABLE t_employee ( emp_id int(10) not null auto_increment primary key, emp_name varchar(8) not null default '', sex tinyint(1) not null default 0, age tinyint(3) not null default 0, email varchar(64) not null default '' ); INSERT INTO t_employee(emp_name, age, sex, email) VALUES ("paul", 36, 1, "pual@qq.com"), ("james", 36, 1, "james.com"), ("刘亦菲", 34, 2, "liu@qq.com"), ("郑爽", 30, 2, "zhengshuang@qq.com"), ("周杰伦", 42, 1, "jay@163.com"), ("谢霆锋", 40, 1, "xietingfeng@178.com"), ("索隆", 16, 1, "suolong@google.com"), ("乔巴", 6, 1, "qiaoba@zz.com") ``` #### JavaBean ```java package com.mybatisplus.entity; /** * @author monkeykingGWX * email 704835519@qq.com * created 2021-04-24 15:12 */ public class Employee { private Integer empId; private Integer age; private String empName; private String email; private Integer sex; public Employee(Integer empId, Integer age, String empName, String email, Integer sex) { this.empId = empId; this.age = age; this.empName = empName; this.email = email; this.sex = sex; } public Employee() { } public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } @Override public String toString() { return "Employee{" + "empId=" + empId + ", age=" + age + ", empName='" + empName + '\'' + ", email='" + email + '\'' + ", sex=" + sex + '}'; } } ``` #### 加入依赖 在 pom.xml 中加入对 MP、Spring、连接池、Junit、Mysql 驱动等依赖。注意,不需要添加Mybatis依赖以及mybatis-spring,MybatiPlus会帮我们自动引入这两个依赖。 ![](http://img.1024phper.com/blog21042415302412263) ```xml
junit
junit
4.11
test
org.springframework
spring-context
5.2.5.RELEASE
org.springframework
spring-orm
5.2.5.RELEASE
org.springframework
spring-webmvc
5.2.5.RELEASE
mysql
mysql-connector-java
5.1.9
com.alibaba
druid
1.1.12
com.baomidou
mybatis-plus
2.3
log4j
log4j
1.2.17
``` #### 创建Mybatis配置文件 resources/mybatis.xml ```xml
``` #### 创建Spring配置文件 数据源jdbc.properties ```properties jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 jdbc.user=root jdbc.pass= jdbc.maxActive=10 ``` ![](http://img.1024phper.com/blog21042415515520169) ![](http://img.1024phper.com/blog21042415515546356) 可以看到,mybatisplus的整合方式几乎和Mybatis一模一样,除了上图用红线框出来的地方不一样外。 #### 创建EmployeeMapper ```java public interface EmployeeMapper extends BaseMapper
{ } ``` ![](http://img.1024phper.com/blog21042416074815995) #### 设置注解 在实体类上需要做一些配置才可以正常使用mybatisplus ```java @TableName(value = "t_employee") public class Employee { @TableId(value = "emp_id", type = IdType.AUTO) private Integer empId; …… } ``` #### 测试插入一条数据 ```java public class TestCurd { private ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); private EmployeeMapper mapper = context.getBean("employeeMapper", EmployeeMapper.class); @Test public void testInsert1 () { Employee employee = new Employee(); employee.setAge(32); employee.setEmpName("curry"); employee.setEmail("curry@qq.com"); employee.setSex(1); Integer affect = mapper.insert(employee); System.out.println(affect); } } ``` ### 全局配置 ```java
``` ![](http://img.1024phper.com/blog21042416493724840) 配置好了后,实体类的注解就可以去掉了。(@TableId需要保留,告诉框架这个字段是主键) 至此,准备工作已就绪。 ## SpringBoot中引入MybatisPlus ### 创建项目 略 ### 引入依赖 ```xml
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
mysql
mysql-connector-java
com.alibaba
druid
1.1.12
com.baomidou
mybatis-plus-boot-starter
3.3.1
``` ### 实体类 ```java @Data @AllArgsConstructor @NoArgsConstructor @ToString public class Employee { private Integer empId; private Integer age; private String empName; private String email; private Integer sex; } ``` ### 配置MybatPlus ```yaml spring: datasource: name: myDataSource type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://47.114.137.16:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC username: gwx password: 8425df6795cf driver-class-name: com.mysql.cj.jdbc.Driver logging: level: com.boot.mp.mapper: debug server: port: 8888 mybatis-plus: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mapper/*Mapper.xml global-config: db-config: table-prefix: t_ #表前缀 id-type: auto # 主键类型 ``` ### 创建Mapper ```java @Repository public interface EmployeeMapper extends BaseMapper
{ } ``` ### 启动类 ```java @MapperScan("com.boot.mp.mapper") @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } ``` ### 测试 ```java @SpringBootTest class MpTest { @Autowired EmployeeMapper employeeMapper; @Test void test1 () { List
employees = employeeMapper.selectList(null); employees.forEach(System.out::println); } @Test void test2 () { Employee employee = new Employee(); employee.setEmpName("嘟嘟"); employee.setAge(3); employeeMapper.insert(employee); } } ``` ## CURD ### insert 插入方法有两种: * insert 只插入非null的字段 * insertAllColumn 插入所有字段,即使该字段为null值 ```java Employee employee = new Employee(); employee.setAge(32); employee.setEmpName("curry"); Integer affect = mapper.insert(employee); // INSERT INTO t_employee ( age, emp_name ) VALUES ( ?, ? ) Integer affect = mapper.insertAllColumn(employee); //==> Preparing: INSERT INTO t_employee ( emp_id,age,emp_name,email,sex ) VALUES ( ?,?,?,?,? ) //==> Parameters: null, 32(Integer), curry(String), null, null ``` ### update * updateById * updateAllColumnById ```java Employee employee = new Employee(); employee.setEmpId(13); employee.setEmail("curry@google.io"); employee.setSex(1); mapper.updateById(employee); ``` ### select * selectById ```java Employee employee = mapper.selectById(1); System.out.println(employee); ``` * selectBatchIds ```java HashSet
ids = new HashSet<>(); ids.add(1); ids.add(2); ids.add(3); List
employees = mapper.selectBatchIds(ids); employees.forEach(System.out::println); ``` * selectByMap ```java HashMap
map = new HashMap<>(); map.put("emp_name", "curry"); map.put("age", 32); List
employees = mapper.selectByMap(map); employees.forEach(System.out::println); ``` * selectOne ### delete * deleteById ```java mapper.deleteById(13); ``` * deleteByMap ```java HashMap
map = new HashMap<>(); map.put("emp_name", "curry"); map.put("age", 32); mapper.deleteByMap(map); ``` * deleteBatchIds ```java HashSet
ids = new HashSet<>(); ids.add(11); ids.add(12); ids.add(13); mapper.deleteBatchIds(ids); ```
自动填充
文章目录