MyBatis-Plus是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,使用MyBatis-Plus时,不会影响原来Mybatis方式的使用。
SpringBoot+MyBatis-Plus环境搭建
SQL脚本:
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`real_name` varchar(20) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO tb_user (real_name,email,phone) VALUES ('小花','cf1@qq.com','18711111111');
INSERT INTO tb_user (real_name,email,phone) VALUES ('小菊','cf2@qq.com','18322222222');
INSERT INTO tb_user (real_name,email,phone) VALUES ('小熊','cf3@qq.com','18133333333');
INSERT INTO tb_user (real_name,email,phone) VALUES ('小虎','cf4@qq.com','18344444444');
INSERT INTO tb_user (real_name,email,phone) VALUES ('小猪','cf5@qq.com','18723232323');
添加依赖如下(不需要额外添加Mybatis的依赖):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
application.yml:
spring:
datasource:
url: jdbc:mysql://localhost/plusdb?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#在控制台打印SQL语句,com.cf.mpdm.mapper为存放mapper接口的包名
logging:
level:
com.cf.mpdm.mapper: debug
主程序类:
package com.cf.mpdm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//指定扫描mapper接口的包路径
@MapperScan("com.cf.mpdm.mapper")
@SpringBootApplication
public class MpdmApplication {
public static void main(String[] args) {
SpringApplication.run(MpdmApplication.class, args);
}
}
UserMapper接口:
package com.cf.mpdm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cf.mpdm.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
<font color="red">UserMapper接口需要继承BaseMapper接口,并且指定泛型为表对应的实体类。</font>
实体类User:
package com.cf.mpdm.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
//表名tb_user带tb_前缀,和实体类类名User不匹配,所以需要使用@TableName指定表名
@TableName("tb_user")
public class User {
//指定主键生成策略为自动增长
@TableId(type = IdType.AUTO)
private Integer id;
private String realName;
private String email;
private String phone;
//get/set ...
}
<font color="red">MyBatis-Plus默认使用下划线命名到驼峰命名的映射规则来处理表和实体类类名以及表的列名和实体类字段的映射。</font>当默认规则不适用时,则需要通过额外的配置来处理映射,如可以使用注解@TableName来指定表名,使用@TableId和@TableField来指定主键列和非主键列的列名。
@TableId还可以指定主键的生成策略,默认生成策略为NONE,表示无状态,未设置主键类型。其他策略详见IdType。
当多数表具有相同的前缀或者相同的主键生成策略时,可以使用全局配置指定表前缀和主键策略,而无需一个个手动添加注解,当然少数特殊的还是可以通过注解的方式来指定,application.yml:
mybatis-plus:
global-config:
db-config:
# 指定主键生成策略为自动增长
idType: AUTO
# 指定表前缀
tablePrefix: tb_
基本的CRUD操作
package com.cf.mpdm;
import com.cf.mpdm.entity.User;
import com.cf.mpdm.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class MpdmApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testInsert(){
//INSERT INTO tb_user ( real_name, email, phone ) VALUES ( ?, ?, ? )
User user = new User();
user.setRealName("小兰");
user.setEmail("xiaolan@qq.com");
user.setPhone("18700000000");
userMapper.insert(user);
//插入成功时,默认会将主键的值回写到实体类中去
System.out.println(user.getId());
}
@Test
public void testDelete(){
//根据主键ID删除记录
//DELETE FROM tb_user WHERE id=?
userMapper.deleteById(6);
//根据id列表批量删除记录
//DELETE FROM tb_user WHERE id IN ( ? , ? )
/*List<Integer> idList = new ArrayList<>();
idList.add(12);
idList.add(13);
userMapper.deleteBatchIds(idList);*/
//根据条件来删除记录,如根据real_name来删除记录,columnMap的Key必须为表中的字段名,而不是实体类中的字段名。
//DELETE FROM tb_user WHERE real_name = ?
/*Map<String, Object> columnMap = new HashMap<>();
columnMap.put("real_name", "小花");
userMapper.deleteByMap(columnMap);*/
}
@Test
public void testUpdate(){
//根据主键id更新记录
//UPDATE tb_user SET real_name=? WHERE id=?
User user = new User();
user.setId(2);
user.setRealName("欧皓辰");
userMapper.updateById(user);
}
@Test
public void testSelect(){
//根据主键id查询记录
//SELECT id,real_name,email,phone FROM tb_user WHERE id=?
/*User user = userMapper.selectById(3);
System.out.println(user.getRealName());*/
//根据id列表查询对应记录
//SELECT id,real_name,email,phone FROM tb_user WHERE id IN ( ? , ? )
/*List<Integer> idList = new ArrayList<>();
idList.add(3);
idList.add(4);
List<User> userList = userMapper.selectBatchIds(idList);
System.out.println(userList.size());*/
//根据条件来查询对应记录
//SELECT id,real_name,email,phone FROM tb_user WHERE real_name = ?
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("real_name", "小兰");
List<User> userList = userMapper.selectByMap(columnMap);
System.out.println(userList.size());
}
}
关于使用mapper.xml的一些配置
mybatis-plus:
#指定Mapper所对应的XML文件位置
mapperLocations: classpath:mapper/*.xml
#指定别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名
typeAliasesPackage: com.cf.mpdm.entity
原文出处:https://www.cnblogs.com/seve/p/12246815.html
来源:oschina
链接:https://my.oschina.net/u/4349840/blog/3241266