mybatiplus的apply_mybatis-plus⼊门
mybatis-plus 介绍
mybatis-plus 简称MP,MP 使⽤类似于JPA Dao层接⼝继承BaseMapper ⾥⾯封装了Crud的⼀套⽅法 继承该接⼝,既可以调⽤⽅法进⾏基础的Crud,可以不⽤写sql ⽤过Hibernate 的都明⽩。
MP要求编程⼈员 要熟悉mybatis ⾃然也要熟悉sql 还有lambda 表达式 才能很好的学习掌握MP
下⾯是我写的测试项⽬ ,⽤于测试MP的⽅法 ,只是学习作⽤。
创建springboot项⽬ 添加jar坐标
com.baomidou
mybatis-plus-boot-starter
3.1.0
spring:
datasource:
driver-class-name: sql.jdbc.Driver
url: jdbc:mysql://localhost:3306/kgc?useSSL=false&ServerTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: root
logging:
level:
root: warn
com.mp.dao: trace 打印⽇志 只输出dao层 这⾥是输出sql语句
pattern:
console: '%p%m%n'
实体类
Data
@TableName("user")//该注解指定映射数据库表名 数据库名与实体类名⼀样时可以不写
public class User {
@TableId//这个注解是指定id的映射 MP 默认id做主键 字段不是id 就需写上该注解
private Long id;
@TableField("name")//注解是指定⾮字段名映射 字段名不相同时使⽤
private String name;
private Integer age;
private String email;
//直接上级
private Long managerId;
//创建时间
private LocalDateTime createTime;
//备注 这⾥是程序运⾏的时需要的字段并⾮数据库字段
//MP 对于这种字段有三中解决⽅案 新增时 MP会⾃动过滤该属性不会添加到数据库 sql语句回忽略掉
//1 private transient String remark; 表⽰不参与数据序列化 如果该类都要序列化 可以采取第⼆种⽅案
//2 private static String remark 声明为静态变量(注意我这⽤的是Lombok 注解 静态变量不⽣成get、set 需要⼿动⽣成) //3 @TableField(exist = false) exist 默认是true 表⽰该属性是数据库字段 false 表⽰⾮数据库字段
// private static String remark;
@TableField(exist = false)
private static String remark;
public static String getRemark() {
return remark;
}
public static void setRemark(String remark) {
}
}
测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectTest() {
List list = userMapper.selectList(null);
Assert.assertEquals(5, list.size());
list.forEach(System.out::println);
}
@Test
public void insertTest() {
User user = new User( );
user.setAge(22);
user.setName("刘强");
user.setManagerId(1088248166370832385L);
user.w());
user.setRemark("程序组装字段");
int insert = userMapper.insert(user);
System.out.println("影向记录数"+insert);
List idlist = Arrays.asList(1088248166370832385L,1088250446457389058L,1094590409767661570L); //⽣成的sql为SELECT id,name,age,email,manager_id,create_time FROM user WHERE id IN ( ? , ? , ? )
List userList = userMapper.selectBatchIds(idlist);
userList.forEach(System.out::println);
}
@Test
public void selectByMap() {
Map map = new HashMap<>();
//⽣成的sql SELECT id,name,age,email,manager_id,create_time FROM user WHERE name = ? AND age = ?
//map 中的key值是数据库的列 ⽤字符串类型
map.put("name", "王天风");
map.put("age", 25);
List users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
多条件查询使⽤条件构造器多条件条件链式添加
下⾯是⼏个⽰例:
/**
jpa mybatis* 查询名字包含⾬并且年龄⼩⾬40;
*/
@Test
public void selectByWrapper() {
//创建条件构造器对象 有下⾯两种
QueryWrapper queryWrapper = new QueryWrapper<>();
// QueryWrapper query = Wrappers.query();
//封装条件
/
/⽣成的sql: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ? QueryWrapper lt = queryWrapper.like("name", "⾬").lt("age", 40);
List users = userMapper.selectList(lt);
users.forEach(System.out::println);
}
/**
* 查询名字包含⾬并且年龄⼤于等于20且⼩于等于40并且email不为空;
* name like'%⾬%' and age between 20 and 40 email is not null
*/
@Test
public void selectByWrapper2() {
//创建条件构造器对象
QueryWrapper queryWrapper = new QueryWrapper<>();
//⽣成的sql语句为: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL
QueryWrapper wrapper = queryWrapper.like("name", "⾬").between("age", 20, 40).isNotNull("email");
List users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
/**
* 查询名字为王姓或者年龄⼤于等于25,按照年龄降序排列,年龄相同按照ID 升序排序
* name like'⾬%' or age>= 25 order by age desc,id asc
*/
@Test
public void selectByWrapper3() {
//创建条件构造器对象
QueryWrapper queryWrapper = new QueryWrapper<>();
//sql语句为: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? OR age >= ? ORDER BY age DESC , id ASC
QueryWrapper wrapper = queryWrapper.likeRight("name", "王")
.or().ge("age", 25)
.orderByDesc("age")
.orderByAsc("id");
List users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
/**
* 查询创建⽇期为2019年2⽉14⽇ 并且直属上级的名字为王姓
* date_format(create_time,'%Y-%m-%d')and manager_id in (select id from user where name like '王%')
*/
@Test
public void selectByWrapper4() {
//⽣成的Sql语句:SELECT id,name,age,email,manager_id,create_time FROM user WHERE date_format(create_time,'%y-%m-%d')=? AND manager_id IN (select id from user where name like '王%')
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14")
.
inSql("manager_id", "select id from user where name like '王%'");
//queryWrapper.apply("date_format(create_time,'%Y-%m-%d')='2019-02-14'") 也可以直接写⼊值 相当于mybatis ${} 有sql注⼊风险
// .inSql("manager_id", "select id from user where name like '王%'");
List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
/**
* 名字为王姓并且(年龄⼩于40并且年龄⼤于20并且邮箱不为空)
* name like '王%' and (age <40 or email in not null)
*/
@Test
public void selectByWrapper6() {
//⽣成的Sql语句:SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND ( age < ? AND age > ? AND email IS NOT NULL )
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name", "王")
.and(wq->wq.lt("age", 40).gt("age", 20).isNotNull("email"));
List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
/**
* (年龄⼩于40或邮箱不为空) 并且名字为王姓
* (age < 40 or email is not null ) and name liek '王%'
*/
@Test
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论