表结构:
实体类:
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
#datasource
spring:
datasource:
url: jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#mybatis-plus配置控制台打印完整带参数SQL语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
BaseMapper
@Mapper
public interface UserDao extends BaseMapper<User> {
}
查询所有
@SpringBootTest
class Mybatispluse01ApplicationTests {
@Autowired
private UserDao userDao;
@Test
void contextLoads() {
List<User> users = userDao.selectList(null);
System.out.println(users);
}
}
MybatisPlus官网:https://baomidou.com
@Autowired
private UserDao userDao;
//查全部
@Test
void testSelectAll() {
List<User> users = userDao.selectList(null);
System.out.println(users);
}
//插入
@Test
void testInsert(){
User user=new User();
user.setName("Jack");
user.setAge(18);
user.setPassword("jack");
user.setTel("183764849479");
userDao.insert(user);
}
//修改
@Test
void testUpdate(){
User user=new User();
user.setId(1L);
user.setName("Tom666");
userDao.updateById(user);
}
//根据id查询
@Test
void testSelectById(){
User user = userDao.selectById(1L);
System.out.println(user);
}
//根据id删除
@Test
void testDelete(){
userDao.deleteById(1551479559101206529L);
}
}
下载lomback的jar包
org.projectlombok
lombok
@Data注解(包含了set,get方法,无参构造方法,hashCode方法,equals方法)
@Data
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
lombock中的注解@Builder
实体类:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("books")
public class BookEntity {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private String description;
private Integer price;
}
使用.bulid()方法快速构造实体类
@Test
public void test01(){
BookEntity entity = BookEntity.builder()
.name("MP从入门到精通")
.description("牛")
.price(100).build();
bookMapper.insert(entity);
System.out.println(entity.getId());
}
@TableId注解作用:
@TableId使用:
添加在实体类的主键对应的成员属性上即可;
@TableName("tb_user") // 指定表名
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
@TableId(value="id")//字段不一致时,通过value值指定table中主键字段名称
private Long userId;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
}
主键生成策略:
是指为数据库生成主键的方式,我们前面学的数据库主键自增也是一种生成主键的策略,当然除了数据库服务端帮助我们维护主键之外,客户端也可以对主键值进行生成维护。
MP主键生成策略示例:
//指定主键自增的生成策略
@TableId(value = "user_id",type = IdType.AUTO)
private Integet userId;
MP提供的常用主键生成策略如下:
生成策略 | 应用场景 | 特点 |
---|---|---|
IdType.AUTO | 数据库主键自增(确保数据库设置了 主键自增 否则无效) | 1.使用数据库自带的主键自增值; 2.数据库自增的主键值会回填到实体类中; 3.数据库服务端生成的; |
IdType.ASSIGN_ID | 主键类型为number类型或数字类型String | 1.MP客户端生成的主键值; 2.生成的主键值是数字形式的字符串 3.主键对应的类型可以是数字类型或者数字类型的字符串 4.底层基于雪花算法,让数据库的唯一标识也参与id的生成运算,保证id在分布式环境下,全局唯一(避免id的主键冲突问题); |
IdType.ASSIGN_UUID | 主键类型为 string(包含数字和字母组成) | 1.生成的主键值包含数字和字母组成的字符串; 2.注意事项:如果数据库中主键值是number类型的,可不可用 |
注解@TableField作用:
@TableField(exist = false)
);以下情况可以省略:
/**
* 实体类的属性名和数据库的字段名自动映射:
* 1. 名称一样
* 2. 数据库字段使用_分割,实体类属性名使用驼峰名称
*/
@TableName("tb_user")
@Data
public class User {
//设置id生成策略:AUTO 数据库自增
@TableId(type = IdType.AUTO)
private Long id;
//@TableField("user_name")
private String userName;
private String password;
@TableField("t_name")
private String name;
private Integer age;
private String email;
//增删改查操作时,忽略该属性
@TableField(exist = false)
private String address;
}
BaseMaper定义的常用删除方法:
①delete:根据条件删除
②deleteBatchIds:根据id数组进行批量删除
③deleteById:根据id删除
④deleteByMap:根据map集合删除(不常用)
int count = userMapper.deleteById(8L);
List ids = new ArrayList();
ids.add(6);
ids.add(7);
userMapper.deleteBatchIds(ids);
QueryWrapper后面查询时会提到
@Test
public void test02(){
QueryWrapper<BookEntity> queryWrapper=new QueryWrapper<>();
//构造删除条件---id为2,且name=三国演义
queryWrapper.eq("id",2);
queryWrapper.eq("name","三国演义");
bookMapper.delete(queryWrapper);
}
Map<String, Object> map = new HashMap<>();
//delete from tb_user where user_name = ? and age = ?
map.put("user_name","itcast");
map.put("age","18");
userMapper.deleteByMap(map);
根据实体对象中的id更新数据
注意事项:只更新实体类中存在的数据,如果对应的属性为null,不更新;
@Test
public void testUpdateById() {
User user = new User();
user.setId(2L);
user.setPassword("1111111");
int count = userMapper.updateById(user);
}
QueryWrapper常用API
eq( ) : 等于 =
ne( ) : 不等于 <> 或者 !=
gt( ) : 大于 >
ge( ) : 大于等于 >=
lt( ) : 小于 <
le( ) : 小于等于 <=
between ( ) : BETWEEN 值1 AND 值2
notBetween ( ) : NOT BETWEEN 值1 AND 值2
in( ) : in
notIn( ) :not in
sql中反向查询eg:not like != 等等,查询时是不会走索引的;
@Test
void testSelectAll() {
//方式一:
QueryWrapper queryWrapper=new QueryWrapper();
//年龄小于18
queryWrapper.lt("age",18);
queryWrapper.eq("name","张三")
//查询逻辑都是and
List<User> users = userDao.selectList(queryWrapper);
System.out.println(users);
}
OR查询说明
代码示例:
业务要求:查询用户名为"lisi"或者年龄大于23的用户信息;
@Test
public void testWrapper2(){
//1.创建查询条件构建器
QueryWrapper<User> wrapper = new QueryWrapper<>();
//2.设置条件
wrapper.eq("user_name","lisi")
.or()
.lt("age",23);
/*
select * from tb_user where user_name = ? or age < ?
*/
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
模糊查询常用方法
/**
* 模糊查询
*/
@Test
public void testWrapper3(){
//1.创建查询条件构建器
QueryWrapper<User> wrapper = new QueryWrapper<>();
//2.设置条件
wrapper.likeLeft("user_name","zhang");
/*
SELECT id,user_name,password,name,age,email
from tb_user
where user_name like ?
%zhang
*/
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
核心方法
①select方法说明
MP查询时,默认将表中所有字段数据映射查询,但是有时我们仅仅需要查询部分字段信息,这是可以使用select()方法限定返回的字段信息,避免I/O资源的浪费;
wrapper.select("字段1","字段2",......)
示例:
@Test
public void test02(){
QueryWrapper<BookEntity> queryWrapper=new QueryWrapper<>();
//构造删除条件---id为2,且name=三国演义
//select中填入数据库字段名
queryWrapper.select("id","name as bookName ")//此时@TableField中起别名就不起作用了,需要自己手动as一下
.eq("name","MP从入门到入土");
List<BookEntity> bookEntities = bookMapper.selectList(queryWrapper);
System.out.println(bookEntities);
}
②使用注解@TableField(value = “pwd”,select = false)
隐藏字段
@Data
@TableName("tbl_user")
public class User {
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private String online;
}
@Test
void testByPage(){
IPage page=new Page(1,2 );//当前页码值,每页显示多少条数据
userDao.selectPage(page,null);
System.out.println("当前页码值:"+page.getCurrent());
System.out.println("每页显示数:"+page.getSize());
System.out.println("一共多少页:"+page.getPages());
System.out.println("一共多少条数据:"+page.getTotal());
System.out.println("数据:"+page.getRecords());
}
将所有的数据都查询出来了
完善:
增加一个拦截器
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,-1不受限制
paginationInterceptor.setMaxLimit(-1L);
interceptor.addInnerInterceptor(paginationInterceptor);
return interceptor;
}
}
测试:
@Test
public void test03(){
IPage<BookEntity> iPage=new Page<>(1,3);//当前页码,每页显示条数
bookMapper.selectPage(iPage,null);
List<BookEntity> records = iPage.getRecords();
System.out.println(records);//具体记录
System.out.println(iPage.getPages());//总页数
System.out.println(iPage.getCurrent());//当前页
System.out.println(iPage.getTotal());//总数据条数
System.out.println(iPage.getSize());//当前页数据条数=records.length
}
分页+条件查询
@Test
public void testWrapper6(){
int current = 1;//当前页码
int size = 2;//每页显示条数
//1. 构建分页对象
Page<User> page = new Page<>(current,size);
//2. 构建条件对象
QueryWrapper<User> wrapper = new QueryWrapper();
wrapper.lt("age",23);
userMapper.selectPage(page,wrapper);//page和QueryWrapper
List<User> records = page.getRecords();
long total = page.getTotal();
long pages = page.getPages();
System.out.println(records);
System.out.println(total);//2
System.out.println(pages);//1
}
使用QueryWrapper开发存在的问题:
LambdaQueryWrapper可以解决上述出现问题,开发推荐使用;
示例:
@Test
public void testWrapper4() throws Exception{
//创建LambdaQueryWrapper的两种方式
// LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
wrapper.like(User::getUserName, "%张%")
.eq(User::getPassword, "123456")
.ge(User::getAge, 28)
.between(User::getAge, 29, 39)
.orderByDesc(User::getAge)
.select(User::getId, User::getUserName);
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
@Test
public void test07(){
LambdaQueryWrapper<BookEntity> bookEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
bookEntityLambdaQueryWrapper.eq(BookEntity::getId,3);
bookMapper.delete(bookEntityLambdaQueryWrapper);
}
两种方式:
@Test
public void test06(){
//更新的条件
LambdaQueryWrapper<BookEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(BookEntity::getId,3);
//更新的值
BookEntity bookEntity = new BookEntity();
bookEntity.setBookName("三国");
bookMapper.update(bookEntity,lambdaQueryWrapper);
}
@Test
public void test05(){
LambdaUpdateWrapper<BookEntity> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(BookEntity::getId,3)//设置修改条件
.set(BookEntity::getBookName,"水浒传");//set设置修改内容
bookMapper.update(null,lambdaUpdateWrapper);
}
1、在bookmapper接口中,创建一个方法,根据id进行分页
IPage<BookEntity> pageById(IPage<BookEntity> page,@Param("id") Integer id);
2、在resource目录下创建BookMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.iflytek.mapper.BookMapper">
<select id="pageById" resultType="com.iflytek.pojo.BookEntity">
select id,name as bookName,description,price from books
where id >#{id}
</select>
</mapper>
3、测试
@Test
public void test08(){
IPage<BookEntity> ipage=new Page<>(1,3);
bookMapper.pageById(ipage, 1);
List<BookEntity> records = ipage.getRecords();
System.out.println(records);
System.out.println("共"+ipage.getPages()+"页");
System.out.println("共"+ipage.getTotal()+"条数据");
System.out.println("当前是"+ipage.getCurrent()+"页");
}
我们只需要传入Ipage,mp会自动给我们的sql后面添加分页
代码实现:
定义服务扩展接口
//在公共接口的基础上扩展
public interface UserService extends IService<User> {
}
定义服务实现
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}
测试
/**
* @Description 测试条件查询,且仅返回一个
* getOne:sql查询的结果必须为1条或者没有,否则报错 !!!!
*/
@Test
public void test2(){
LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
wrapper.gt(User::getAge,20);
User one = userService.getOne(wrapper);
System.out.println(one);
}
/**
* @Description 根据条件批量查询
*/
@Test
public void test3(){
LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
wrapper.gt(User::getAge,20);
List<User> list = userService.list(wrapper);
System.out.println(list);
}
/**
* @Description 根据条件批量查询并分页
*/
@Test
public void test4(){
LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
wrapper.gt(User::getAge,20);
//构建分页对象
IPage<User> page=new Page<>(2,3);
userService.page(page,wrapper);
System.out.println(page.getRecords());
System.out.println(page.getPages());
System.out.println(page.getTotal());
}
/**
* @Description 测试服务层save保存单条操作
*/
@Test
public void test5(){
User user1 = User.builder().name("wangwu").userName("laowang4").
email("[email protected]").age(20).password("333").build();
boolean isSuccess = userService.save(user1);
System.out.println(isSuccess?"保存成功":"保存失败");
}
/**
* @Description 测试服务层批量保存
*/
@Test
public void test6(){
User user2 = User.builder().name("wangwu2").userName("laowang2").
email("[email protected]").age(20).password("333").build();
User user3 = User.builder().name("wangwu3").userName("laowang3").
email("[email protected]").age(20).password("333").build();
boolean isSuccess = userService.saveBatch(Arrays.asList(user2, user3));
System.out.println(isSuccess?"保存成功":"保存失败");
}
/**
* @Description 根据id删除操作
*/
@Test
public void test7(){
boolean isSuccess = userService.removeById(17l);
System.out.println(isSuccess?"保存成功":"保存失败");
}
/**
* @Description 根据条件批量删除
*/
@Test
public void test8(){
LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
wrapper.gt(User::getId,12)
.gt(User::getAge,20);
boolean remove = userService.remove(wrapper);
System.out.println(remove);
}
/**
* @Description 测试根据id更新数据
*/
@Test
public void test9(){
//UPDATE tb_user SET password=?, t_name=? WHERE id=?
User user2 = User.builder().name("wangwu2").password("333").id(3l).build();
boolean success = userService.updateById(user2);
System.out.println(success);
}
/**
* @Description 测试根据条件批量更新
*/
@Test
public void test10(){
LambdaUpdateWrapper<User> wrapper = Wrappers.lambdaUpdate(User.class);
//UPDATE tb_user SET age=? WHERE (id IN (?,?,?))
wrapper.in(User::getId,Arrays.asList(1l,3l,5l)).set(User::getAge,40);
boolean update = userService.update(wrapper);
System.out.println(userService);
}
1.修改表结构增加字段表示是否被删除
2.修改实体类,增加标记
//逻辑删除字段,标记当前字段是否被删除
@TableLogic(value = "0",delval = "1")
private Integer deleted;
第二种方法:
在配置文件中设置标记
@TableLogic
private Integer deleted;
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted #指定数据库中逻辑删除的表的column
logic-delete-value: 1 #表示被删除
logic-not-delete-value: 0 #0表示未被删除
3.执行删除操作
@Test
void testDelete(){
List<Long>list=new ArrayList<>();
list.add(5L);
userDao.deleteBatchIds(list);
}
使用场景:
1.业务操作周期长,如果业务整个加入事务,导致数据库资源锁定周期过长,性能降低;
2.如果资源争抢过于激烈,会导致失败重试次数过多,导致性能降低;
操作步骤:
数据库中的表中新加一个字段version
实体类中定义该属性使用@Version注解
@Version
private Integer version;
开启拦截器
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
//分页拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//添加乐观锁拦截器
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
当执行update操作时,必须收集version
@Test
void testUpdate(){
User user=new User();
user.setVersion(1);
user.setId(1L);
user.setName("Tom666");
userDao.updateById(user);
}
也可以先查询
@Test
void testUpdate(){
//先查询,user里面就携带了version
User user = userDao.selectById(3L);
user.setId(1L);
user.setName("Tom666");
userDao.updateById(user);
}
每执行一次更新操作,version就会自动加1
使用mp的乐观锁,需要先自己根据主键id查询用户信息,信息中包含了此时的version数据,然后再更新,更新时会将查询的version值作为更新条件取更新;
比如数据库中表字段有create_time和update_time
需要我们插入数据 时更新create_time和update_time
更新数据时create_time保持不变,update_time进行更新
1、添加实体类属性 使用@TableField注解指明需要更新的字段
public class BookEntity {
@TableId(type = IdType.AUTO)
private Long id;
@TableField(value = "name")
private String bookName;
private String description;
private Integer price;
@TableLogic
private Integer deleted;
@Version
private Integer version;
@TableField(fill = FieldFill.INSERT)//插入时更新
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)//插入和修改时更新
private LocalDateTime updateTime;
}
2、添加拦截器
@Component
public class FillDataHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime",LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
metaObject.setValue("updateTime", LocalDateTime.now());
}
}