MyBatis-Plus是一个MyBatis的增强工具
,在MyBatis的基础上只做增强不做改变
,为简化开发、提高效率而生
。MyBatis-Plus提供了通用的mapper和service,可以在不编写任何SQL语句的情况下,快速的实现对单表
的CRUD、批量、逻辑删除、分页等操作。
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
dependencies>
spring:
# 配置数据源信息
datasource:
# 配置数据源类型(spring默认的数据源)
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库信息(配置驱动类,版本为8)
driver-class-name: com.mysql.cj.jdbc.Driver
#mysql8版本的写法
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: yuan159951.
# mysql5版本的写法
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
import lombok.Data;
@Data //lombok注解
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
import com.yunfeng.mybatisplus.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper<User> {
}
@SpringBootApplication
//扫描mapper接口所在的包
@MapperScan("com.yunfeng.mybatisplus.mapper")
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
import com.yunfeng.mybatisplus.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectList(){
//selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有
userMapper.selectList(null).forEach(System.out::println);
}
}
# 配置MyBatis日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Test
public void testInsert(){
User user = new User();
user.setName("张三");
user.setAge(18);
user.setEmail("302976975@qq.com");
int row = userMapper.insert(user);
System.out.println("row = " + row);
System.out.println(user.getId());
}
@Test
public void testSelectById(){
// User user = userMapper.selectById(1L);
// System.out.println(user);
// List longs = Arrays.asList(1l, 2l, 3l);
// List users = userMapper.selectBatchIds(longs);
// System.out.println("users = " + users);
// Map map = new HashMap<>();
// map.put("name","jack");
// map.put("age",20);
// List users = userMapper.selectByMap(map);
// System.out.println("users = " + users);
List<User> users = userMapper.selectList(null);
System.out.println("users = " + users);
}
@Test
public void testUpdateById(){
User user = new User();
user.setId(4L);
user.setName("李四");
int result = userMapper.updateById(user);
System.out.println("受影响行数:"+result);
}
@Test
public void testDeleteById(){
// int result = userMapper.deleteById(1595594324846141442L);
// System.out.println("受影响行数:"+result);
// Map,Object> map = new HashMap<>();
// map.put("name","张三");
// map.put("age",18);
// userMapper.deleteByMap(map);
List> longs = Arrays.asList(1l, 2l, 3l);
int rows = userMapper.deleteBatchIds(longs);
System.out.println("rows = " + rows);
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yunfeng.mybatisplus.mapper.UserMapper">
mapper>
Map<String,Object> selectMapById(Long id);
<select id="selectMapById" resultType="map">
select * from user where id = #{id}
select>
Map<String, Object> map = userMapper.selectMapById(1l);
System.out.println("map = " + map);
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{
}
@Test
public void testGetCount(){
long count = userService.count();
System.out.println("count = " + count);
}
@Test
public void testSaveBatch(){
// SQL长度有限制,海量数据插入单条SQL无法实行,
// 因此MP将批量插入放在了通用Service中实现,而不是通用Mapper
ArrayList<User> users = new ArrayList<>();
for (int i = 0; i < 5; i++) {
User user = new User();
user.setName("ybc" + i);
user.setAge(20 + i);
users.add(user);
}
//SQL:INSERT INTO t_user ( username, age ) VALUES ( ?, ? )
boolean bool = userService.saveBatch(users);
System.out.println("bool = " + bool);
}
@Data //lombok注解
@TableName("user")
public class User {
//IdType.AUTO就是自增
@TableId(value = "id",type = IdType.AUTO)
private Long id;
@TableField("name")
private String name;
private Integer age;
private String email;
//逻辑删除
@TableLogic
private Integer isDeleted;
}
# 配置MyBatis
mybatis-plus:
configuration:
# 日志信息
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
# 配置MyBatis-Plus的主键策略
id-type: auto
# 配置MyBatis-Plus操作表的默认前缀
# table-prefix:
# 配置类型别名所对应的包
type-aliases-package: com.yunfeng.mybatisplus.pojo
# 配置扫描通用枚举
type-enums-package: com.yunfeng.mybatisplus.enums
雪花算法是由Twitter公布的分布式主键生成算法,它能够保证不同表的主键的不重复性,以及相同表的
主键的有序性。
@Test
public void test01(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","a")
.between("age",20,30)
.isNotNull("email");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
@Test
public void test02(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("age")
.orderByAsc("id");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
@Test
public void test03(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("email");
int row = userMapper.delete(queryWrapper);
System.out.println("row = " + row);
}
@Test
public void test04(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age",20)
.like("name","a")
.or()
.isNull("email");
User user = new User();
user.setName("张三");
int rows = userMapper.update(user, queryWrapper);
System.out.println("rows = " + rows);
}
@Test
public void test05(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//lambda中的条件优先执行
queryWrapper.like("name","a")
.and(i -> i.gt("age",20).or().isNull("email"));
User user = new User();
user.setName("李四");
int rows = userMapper.update(user, queryWrapper);
System.out.println("rows = " + rows);
}
//查询部分字段
@Test
public void test06(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name","age");
List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
maps.forEach(System.out::println);
}
// 子查询
@Test
public void test07(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("age","select age from user where age < 20");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
@Test
public void test08(){
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("name","a")
.and( i -> i.gt("age",20).or().isNull("email"));
updateWrapper.set("name","王五");
int rows = userMapper.update(null, updateWrapper);
System.out.println("rows = " + rows);
}
@Test
public void test10(){
String name = "";
Integer ageBegin = 20;
Integer ageEnd = 30;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(name),"name",name)
.ge(ageBegin != null,"age",ageBegin)
.gt(ageEnd != null,"age",ageEnd);
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
@Test
public void test11(){
String name = "";
Integer ageBegin = 20;
Integer ageEnd = 30;
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(name),User::getName,name)
.ge(ageBegin != null,User::getAge,ageBegin)
.gt(ageEnd != null,User::getAge,ageEnd);
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
@Test
public void test12(){
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(User::getName,"a")
.and( i -> i.gt(User::getAge,20).or().isNull(User::getEmail));
updateWrapper.set(User::getName,"赵六");
int rows = userMapper.update(null, updateWrapper);
System.out.println("rows = " + rows);
}
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//扫描mapper接口所在的包
@MapperScan("com.atguigu.mybatisplus.mapper")
public class MyBatisPlusConfig {
//分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
@Test
public void testPage(){
Page<User> page = new Page<>(2,3);
userMapper.selectPage(page, null);
System.out.println("page = " + page);
}
@Data
public class Product {
private Long id;
private String name;
private Integer price;
@Version
private Integer version;
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//添加乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.31version>
dependency>
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false", "root", "yuan159951.")
.globalConfig(builder -> {
builder.author("logic") // 设置作者
//.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D://mybatis_plus"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.yunfeng") // 设置父包名
.moduleName("mybatisplus") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://mybatis_plus")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("user") // 设置需要生成的表名
.addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker 引擎模板,默认的是Velocity引擎模板
.execute();
}