当前位置: 首页 > news >正文

长沙网站优化页面湖南省建设工程造价管理总站网站

长沙网站优化页面,湖南省建设工程造价管理总站网站,php源码下载网站,收录提交大全文章目录 前言一、Mybatis-Plus简介二、框架结构三、SpringBoot整合Mybatis-Plus1.依赖2.配置文件设置 四、前期准备4.1数据库信息4.2dao类4.3pojo类 五、常用注解5.1 TableName(value )5.2 TableId(value,type IdType.XXX)5.3 TableField(TableName(value )5.2 TableId(value,type IdType.XXX)5.3 TableField()5.4TableLogic(value 0,delval 1) 六、方法演示6.1 新增6.2 根据ID修改6.3 根据名字修改6.4 根据ID查询6.4 根据ID批量查询6.5 查询count6.6 查询List6.7 lambda表达式按照条件动态查询6.8 查询字段6.9 聚合查询6.10 分组查询6.11 分页查询6.12 逻辑删除 总结 前言 本文章将演示SpringBoot整合Mybatis-Plus以及常用常用注解方法 一、Mybatis-Plus简介 MyBatis-Plus (opens new window)简称 MP是一个 MyBatis (opens new window) 的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。 特性 无侵入只做增强不做改变引入它不会对现有工程产生影响如丝般顺滑损耗小启动即会自动注入基本 CURD性能基本无损耗直接面向对象操作强大的 CRUD 操作内置通用 Mapper、通用 Service仅仅通过少量配置即可实现单表大部分 CRUD 操作更有强大的条件构造器满足各类使用需求支持 Lambda 形式调用通过 Lambda 表达式方便的编写各类查询条件无需再担心字段写错支持主键自动生成支持多达 4 种主键策略内含分布式唯一 ID 生成器 - Sequence可自由配置完美解决主键问题支持 ActiveRecord 模式支持 ActiveRecord 形式调用实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作支持全局通用方法注入 Write once, use anywhere 内置代码生成器采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码支持模板引擎更有超多自定义配置等您来使用内置分页插件基于 MyBatis 物理分页开发者无需关心具体操作配置好插件之后写分页等同于普通 List 查询分页插件支持多种数据库支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库内置性能分析插件可输出 SQL 语句以及其执行时间建议开发测试时启用该功能能快速揪出慢查询内置全局拦截插件提供全表 delete 、 update 操作智能分析阻断也可自定义拦截规则预防误操作 二、框架结构 通过结构图可以看出MyBatisPlus专门提供了mybatis-plus-boot-starter供SpringBoot使用 三、SpringBoot整合Mybatis-Plus 1.依赖 dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.3/version /dependency注此依赖整合了Mybatis和Mybatis-Plus,所以尽量不要再加mybatis的依赖以免冲突 2.配置文件设置 mybatis:configuration://默认是不允许自动转换驼峰命名得自己设置为truemap-underscore-to-camel-case: true//起别名type-aliases-package: com.apesource.pojo mybatis-plus:configuration://默认是允许自动转换驼峰命名map-underscore-to-camel-case: true//开启日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl注mybatis和mybatis-plusd 配置是相互独立且生效的 四、前期准备 4.1数据库信息 4.2dao类 Mapper public interface StudentMapper extends BaseMapperStudent { }dao层的接口使用Mapper声明可以被自动注入并且继承Mybatis-Plus提供的BaseMapper类实现通过mapper对象调用CDUR方法 4.3pojo类 Data NoArgsConstructor AllArgsConstructor public class Student implements Serializable {private int stuId;private String stuName;private String stuNick;private String stuHobby;private int stuAge;private int deleted;public Student(String stuName, String stuNick, String stuHobby, int stuAge) {this.stuName stuName;this.stuNick stuNick;this.stuHobby stuHobby;this.stuAge stuAge;}public Student(String stuName, String stuNick, String stuHobby) {this.stuName stuName;this.stuNick stuNick;this.stuHobby stuHobby;} }五、常用注解 5.1 TableName(value “”) 作用将类对应到数据库的某个表上 位置类 参数value表的字段名 5.2 TableId(value“”,type IdType.XXX) 作用将数据库的主键字段映射到类的属性中 位置成员变量 参数value表的字段名typeAUTO(0),NONE(1),INPUT(2),ASSIGN_ID(3),ASSIGN_UUID(4); 5.3 TableField(“”) 作用将类的属性映射到表的字段 位置成员变量 参数value表的字段名 5.4TableLogic(value “0”,delval “1”) 作用开启逻辑删除0表示未删除1表示以及删除 位置成员变量 参数value正常状态的值delval被删除后的值 注此注解声明的成员变量只有MybatisPlus能识别如果使用mybatis的方法查询的话还是能查询到状态是已删除的信息 将以上注解用与Studen类中 TableName(value student) public class Student implements Serializable { TableId(valuestu_id,type IdType.AUTO)private int stuId;TableField(stu_name)private String stuName;TableField(stu_nick)private String stuNick;TableField(stu_hobby)private String stuHobby;TableField(stu_age)private int stuAge;TableField(value deleted)TableLogic(value 0,delval 1)private int deleted; }public Student(String stuName, String stuNick, String stuHobby, int stuAge) {this.stuName stuName;this.stuNick stuNick;this.stuHobby stuHobby;this.stuAge stuAge;}public Student(String stuName, String stuNick, String stuHobby) {this.stuName stuName;this.stuNick stuNick;this.stuHobby stuHobby;}六、方法演示 6.1 新增 在测试类中注入 Autowired StudentMapper mapper; Testpublic void test01() {Student student new Student(李逵, 黑旋风, 飞斧, 33);int i mapper.insert(student);System.out.println(i);}结果如图 6.2 根据ID修改 //修改IDTestpublic void test02() throws Exception {Student student new Student();student.setStuId(7);student.setStuName(鲁智深);student.setStuAge(38);int i mapper.updateById(student);System.out.println(i);}结果如图 6.3 根据名字修改 Testpublic void test03() throws Exception {//1.修改数据Student student new Student();student.setStuHobby(拔杨柳);//2.创建条件QueryWrapper wrapper new QueryWrapper();wrapper.eq(stu_name, 鲁智深);int update mapper.update(student, wrapper);System.out.println(update);}结果如图 6.4 根据ID查询 //根据ID查询Testpublic void test04() throws Exception {Student student mapper.selectById(3);System.out.println(student);}结果如图 6.4 根据ID批量查询 //根据ID批量查询Testpublic void test05() throws Exception {ListStudent students mapper.selectBatchIds(Arrays.asList(1, 3, 5, 6));students.forEach(System.out::println);}结果如图 6.5 查询count //查询countTestpublic void test06() throws Exception {QueryWrapperStudent wrapper new QueryWrapper();wrapper.eq(stu_hobby, csgo);Integer integer mapper.selectCount(wrapper);System.out.println(integer);}结果如图 6.6 查询List //查询listTestpublic void test07() throws Exception {QueryWrapperStudent wrapper new QueryWrapper(); // wrapper.eq(stu_hobby,csgo).or().eq(stu_age,21);wrapper.eq(stu_hobby, csgo);wrapper.eq(stu_age, 21);//根据wrapper里的条件查询返回一个集合ListStudent students mapper.selectList(wrapper);students.forEach(student - System.out.println(student));}结果如图 6.7 lambda表达式按照条件动态查询 //模拟动态查询Testpublic void test09() {//1.前端发送来的数据Integer num1 null;Integer num2 30;//1.查询条件LambdaQueryWrapperStudent wrapper new LambdaQueryWrapper();//2.判断 // if(null ! num2){ // wrapper.lt(Student::getStuAge,num2); // } // if(null ! num1){ // wrapper.gt(Student::getStuAge,num1); // }wrapper.gt(num1 ! null, Student::getStuAge, num1);wrapper.lt(num2 ! null, Student::getStuAge, num2);//3.查询ListStudent students mapper.selectList(wrapper);students.forEach(System.out::println);}结果如图 6.8 查询字段 Testpublic void test10() {//1.条件LambdaQueryWrapperStudent wrapper new LambdaQueryWrapper();wrapper.select(Student::getStuName, Student::getStuHobby);//2.查询ListStudent students mapper.selectList(wrapper);//4.遍历students.forEach(System.out::println);}结果如图 6.9 聚合查询 //投影查询-聚合查询Testpublic void test11() {//1.查询条件QueryWrapperStudent wrapper new QueryWrapperStudent();//2.判断wrapper.select(count(*) as total,max(stu_age) as maxage,sum(stu_age) as sumage);//3.查询ListMapString, Object list mapper.selectMaps(wrapper);//4.遍历list.forEach(Map::entrySet);}结果如图 6.10 分组查询 //分组查询Testpublic void test12() {//1.查询条件QueryWrapperStudent wrapper new QueryWrapperStudent();//2.判断wrapper.select(count(*) as num1,stu_hobby);wrapper.groupBy(stu_hobby);//3.查询ListMapString, Object list mapper.selectMaps(wrapper);//4.遍历list.forEach(System.out::println);}结果如图 6.11 分页查询 使用分页查询需要在配置类中注入分页类 Configuration public class SpringBootConfig {//注入mp拦截器Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){//1.实例化拦截器MybatisPlusInterceptor mybatisPlusInterceptor new MybatisPlusInterceptor();//2.分页拦截器mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return mybatisPlusInterceptor;} }/*** mp分页使用* 注意* 1.page.setCurrent(2);当前页码从1开始* 2.分页需要配置插件* 3.mp坐标版本3.1.1不能使用过高版本*/Testpublic void test13() throws Exception {//1.定义分页规则IPageStudent page new PageStudent();page.setSize(2);//每页记录数page.setCurrent(2);//当前页码//2.查询条件(可选)IPageStudent iPage mapper.selectPage(page, null);ListStudent records iPage.getRecords();//分页结果records.forEach(System.out::println);System.out.println(每页容量 iPage.getSize());System.out.println(总记录数 iPage.getTotal());System.out.println(总记页数 iPage.getPages());}结果如图 6.12 逻辑删除 /*** 物理删除:业务数据从数据库中丢弃执行的是delete操作* 逻辑删除:为数据设置是否可用状态字段删除时设置状态字段为不可用状态* 数据保留在数据库中执行的是update操作* 实现步骤* 步骤1:修改数据库表添加deleted列比如0代表正常1代表删除可以在添加列的同时设置其默认值为0正常。* 步骤2:实体类添加属性以及注解** TableLogic(value0,delval1) private Integer deleted;* value为正常数据的值delval为删除数据的值*///逻辑删除Testpublic void test14() {mapper.deleteById(6);}结果如图 逻辑删除不会真正的将数据从数据库删除而是对此时使用修改的方式对 TableLogic声明的属性修改值但是使用mybatis-plus的查询方法无法将它查到 总结 由上可知SpringBoot整合Mybatis-Plus后对数据库操作有多方便因为Mybatis-Plus有强大的条件构造器,满足各类使用需求内置的Mapper,通用的Service,少量配置即可实现单表大部分CRUD操作支持Lambda形式调用提供了基本的CRUD功能,连SQL语句都不需要编写自动解析实体关系映射转换为MyBatis内部对象注入容器
http://www.dnsts.com.cn/news/116418.html

相关文章:

  • php网站建设案例教程wordpress 如何仿站
  • 英语网站建设费用网站建设 提案 框架
  • 青岛网站定制多少钱权4网站怎么做
  • 五莲县城乡建设局网站首页用别人公司名字做网站违法么
  • 黄页游戏引流推广网站html5高端酒水饮料企业网站模版
  • dedecms购物网站模板如何使用表格做网站
  • 北京行业网站制作低代码平台开发
  • 贵州企业网站建设设计搜索引擎营销的英文简称
  • 建设的网站打开速度很慢成都哪里好玩
  • 建设一个下载网站做网站谁家做的好
  • 西宁的网站建设wordpress中文cms主题模板下载
  • 电商网站建设关键词优化网站第三方评价如何做
  • 旅游网站设计的目的有什么做服装的网站
  • 网站源码下载 app哈尔滨寸金网站建设价钱
  • 网站建设协议书是否贴花百度图在图不留网站方
  • 免费空间注册网站最新营销模式
  • 网站建设案例哪家好刚刚发生了什么大事
  • 大兴营销型网站建设高校网站建设费用
  • 四川建设厅招投标官方网站天水建设银行网站
  • 招聘网站建设人员郑州建设教育培训中心
  • python+网站开发实例教程网络营销策划书800字
  • python网站开发架构潍坊网站建设套餐
  • 网站如何添加数据c2c网站的功能
  • 网站换主题最佳的网络营销策划是
  • wordpress 回复显示不出来上海营销型网站seo
  • 红酒论坛网站建设wordpress 分类输出
  • 民和网站建设公司属于网络营销站点推广的是
  • 买的虚拟主机怎么做网站苏州外贸公司网站建设流程
  • 网站后台管理系统展望修邦建设网站
  • 购买空间后怎么上传网站纪念平台网站建设