符合seo的网站,关于建设网站的培训知识,html搭建网站,建网站的详细案例1. 概念 MyBatis-Plus#xff08;简称 MP#xff09;是一个MyBatis 的增强工具#xff0c;在 MyBatis 的基础上只做增强不做改变#xff0c;为简化开发、提高效率而生。其突出的特性如下#xff1a; * **无侵入**#xff1a;只做增强不做改变#xff0c;引入它不会对现有…1. 概念 MyBatis-Plus简称 MP是一个MyBatis 的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。其突出的特性如下 * **无侵入**只做增强不做改变引入它不会对现有工程产生影响如丝般顺滑 * **强大的 CRUD 操作**内置通用 Mapper、通用 Service提供了大量的通用的CRUD方法因此可以省去大量手写sql的语句的工作。 * **条件构造器**提供了强大的条件构造器可以构造各种复杂的查询条件以应对各种复杂查询。 * **内置分页插件**配置好插件之后写分页等同于普通 List 查询无需关注分页逻辑。 2. 与springboot集成
导入依赖
dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.3.2/version
/dependency 3. 创建实体类
Data
TableName(user)
public class User {TableId(value id, type IdType.AUTO)private Long id;TableField(name)private String name;TableField(age)private Integer age;TableField(email)private String email;
} * TableName表名注解用于标识实体类所对应的表 * value用于声明表名 * TableId主键注解用于标识主键字段 * value用于声明主键的字段名 * type用于声明主键的生成策略常用的策略有AUTO、ASSIGN_UUID、INPUT等等 * TableField普通字段注解用于标识属性所对应的表字段 * value用于声明普通字段的字段名 4. 通用Mapper 1. 创建Mapper接口并继承由Mybatis-plus提供的BaseMapperT接口
Mapper
public interface UserMapper extends BaseMapperUser {IPageUser selectUserPage(IPageUser page);
}若Mapper接口过多可不用逐一配置Mapper注解而使用MapperScan注解指定包扫描路径进行统一管理例如
SpringBootApplication
MapperScan(com.itgyl.mapper)
public class HelloMpApplication {public static void main(String[] args) {SpringApplication.run(HelloMpApplication.class, args);}}
2. 测试Mapper
SpringBootTest
class UserMapperTest {Autowiredprivate UserMapper userMapper;Testpublic void testSelectUser() {ListUser users userMapper.selectList(null);users.forEach(System.out::println);Long count userMapper.selectCount(null);System.out.println(count count);}
}
5. 通用Service 通用Service进一步封装了通用Mapper的CRUD方法并提供了例如saveOrUpdate、saveBatch等高级方法。
1.定义service接口
//定义业务接口需要继承IService类
public interface UserService extends IServiceUser {
}
2.创建service实体类
//服务层
Service
//mybatis-plus业务实现类需继承ServiceImpl类并将Mapper接口类和pojo类传入
//底层是将Mapper类进一步封装而来使得业务层直接调用
//并且实现业务接口类即使得控制层调用业务方法也形成规范
public class UserServiceImpl extends ServiceImplUserMapper, User implements UserService {}
测试
SpringBootTest
class UserServiceImplTest {Autowiredprivate UserServiceImpl userService;Testpublic void testUserService() {OptionalUser optById userService.getOptById(1);System.out.println(optById);}
}
6. 条件构造器 SpringBootTest
public class WrapperTest {AutowiredUserService userService;Autowiredprivate ServiceImpl service;//QueryWrapper条件构造器通过该构造器构造查询的过滤条件Testpublic void testQueryWrapper() {//wrapper会将过滤条件添加到sql语句中//1.查询nameTom的数据QueryWrapperUser queryWrapper1 new QueryWrapper();queryWrapper1.eq(name, Tom);//2.模糊匹配QueryWrapperUser queryWrapper2 new QueryWrapper();queryWrapper2.like(email, .com);//3.根据年龄降序排序并将结果打印QueryWrapperUser queryWrapper3 new QueryWrapper();queryWrapper3.orderByDesc(age);//4.查询年龄大于等于20小于等于30QueryWrapperUser queryWrapper4 new QueryWrapper();queryWrapper4.ge(age, 20).le(age, 30);//5.查询年龄小于20或者大于30的用户数据QueryWrapperUser queryWrapper5 new QueryWrapper();queryWrapper5.le(age, 20).or().ge(age, 30);//6.查询以.com结尾 and (age 20 or age 30)//and里面传递参数为要过滤的条件QueryWrapperUser queryWrapper new QueryWrapper();queryWrapper.like(email, .com).and(QueryWrapper - QueryWrapper.le(age, 20).or().ge(age, 30));List list service.list(queryWrapper);list.forEach(System.out::println);}//UpdateWrapper更新删除的条件构造器Testpublic void testUpdateWrapper() {UpdateWrapperUser updateWrapper new UpdateWrapper();updateWrapper.eq(name, Tom).set(name, ESon);boolean update service.update(updateWrapper);System.out.println(update update);}//LambdaQueryWrapper构造器里面通过方法引用传递参数//优点减少手敲字段带来不必要的错误提高开发效率Testpublic void testLambdaQueryWrapper() {LambdaQueryWrapperUser lambdaQueryWrapper new LambdaQueryWrapper();lambdaQueryWrapper.eq(User :: getName, ESon);ListUser list userService.list(lambdaQueryWrapper);list.forEach(System.out :: println);}//LambdaUpdateWrapper构造器Testpublic void testLambdaUpdateWrapper() {LambdaUpdateWrapperUser lambdaUpdateWrapper new LambdaUpdateWrapper();lambdaUpdateWrapper.eq(User :: getName, ESon).set(User :: getName, Tom);boolean update userService.update(lambdaUpdateWrapper);System.out.println(update update);}
} 7. 分页插件 1.配置分页插件 Configuration
public class MPConfiguration {Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
} 核心 | 属性名 | 类型 | 默认值 | 描述 | | ------- | ---- | --------- | ---------------------- | | records | List | emptyList | 查询数据列表 | | total | Long | 0 | 查询列表总记录数 | | size | Long | 10 | 每页显示条数默认10 | | current | Long | 1 | 当前页 | 分页对象既作为分页查询的参数也作为分页查询的返回结果当作为查询参数时通常只需提供current和size属性如下 java IPageT page new Page(current, size); 注IPage为分页接口Page为IPage接口的一个实现类。 自定义分页查询
在Mapper接口中定义分页查询方法
Mapper
public interface UserMapper extends BaseMapperUser {IPageUser selectUserPage(IPageUser page);
} 创建resources/mapper/UserMapper.xml文件内容如下
?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itgyl.mapper.UserMapperselect idselectUserPage resultTypecom.itgyl.entity.Userselect *from user/select
/mapper **注意**Mybatis-Plus中Mapper.xml文件路径默认为classpath*:/mapper/**/*.xml可在application.yml中配置以下参数进行修改 mybatis-plus:mapper-locations: classpath*:/mapper/**/*.xml 2.测试 SpringBootTest
public class PageTest {Autowiredprivate UserMapper userMapper;Autowiredprivate UserService userService;Testpublic void testPageService() {//创建分页对象将按照传递的参数进行分页此时展示第二页的两条数据current展示第几页 size每页多少条数据PageUser userPage new Page(2, 2);//调用通用service层方法进行分页查询最终将分页结果返回PageUser result userService.page(userPage);result.getRecords().forEach(System.out::println);}Testpublic void testPageMapper() {PageUser userPage new Page(2, 2);PageUser result userMapper.selectPage(userPage, null);result.getRecords().forEach(System.out::println);}//自定义分页查询可用于处理复杂业务Testpublic void testSelectPage() {PageUser userPage new Page(2,2);IPageUser result userMapper.selectUserPage(userPage);result.getRecords().forEach(System.out::println);}
}