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

清河做网站报价你访问的网站正在建设

清河做网站报价,你访问的网站正在建设,教程seo推广排名网站,创建自己的网站需要准备什么SpringBoot第28讲#xff1a;SpringBoot集成MySQL - MyBatis-Plus方式 本文是SpringBoot第28讲#xff0c;MyBatis-Plus#xff08;简称 MP#xff09;是一个 MyBatis的增强工具#xff0c;在 MyBatis 的基础上只做增强不做改变#xff0c;为简化开发、提高效率而生。MyB…SpringBoot第28讲SpringBoot集成MySQL - MyBatis-Plus方式 本文是SpringBoot第28讲MyBatis-Plus简称 MP是一个 MyBatis的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。MyBatis-Plus在国内也有很多的用户本文主要介绍MyBatis-Plus和SpringBoot的集成。 文章目录 SpringBoot第28讲SpringBoot集成MySQL - MyBatis-Plus方式1、知识准备1.1、为什么会诞生MyBatis-Plus1.2、支持数据库1.3、整体架构 2、简单示例2.1、准备DB和依赖配置2.2、定义dao2.3、定义Service接口和实现类2.4、controller2.5、分页配置 3、进一步理解3.1、比较好的实践3.2、除了分页插件之外还提供了哪些插件 4、示例源码 1、知识准备 MyBatis-Plus简称 MP是一个 MyBatis的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。 1.1、为什么会诞生MyBatis-Plus 正如前文所述SpringBoot第24讲SpringBoot集成MySQL - MyBatis XML方式为了更高的效率出现了MyBatis-Plus这类工具对MyBatis进行增强。 考虑到MyBatis是半自动化ORMMyBatis-Plus 启动即会自动注入基本 CURD性能基本无损耗直接面向对象操作; 并且内置通用 Mapper、通用 Service仅仅通过少量配置即可实现单表大部分 CRUD 操作更有强大的条件构造器满足各类使用需求总体上让其支持全自动化的使用方式本质上借鉴了Hibernate思路考虑到Java8 Lambda函数式编程开始流行MyBatis-Plus支持 Lambda 表达式方便的编写各类查询条件无需再担心字段写错考虑到MyBatis还需要独立引入PageHelper分页插件MyBatis-Plus支持了内置分页插件同PageHelper一样基于 MyBatis 物理分页开发者无需关心具体操作配置好插件之后写分页等同于普通 List 查询考虑到自动化代码生成方式MyBatis-Plus也支持了内置代码生成器采用代码或者 Maven 插件可快速生成 Mapper、Model、 Service、Controller 层代码支持模板引擎更有超多自定义配置等您来使用考虑到SQL性能优化等问题MyBatis-Plus内置性能分析插件, 可输出 SQL 语句以及其执行时间建议开发测试时启用该功能能快速揪出慢查询(能对慢查询做到聚类吗)其它还有解决一些常见开发问题比如支持主键自动生成支持4 种主键策略内含分布式唯一 ID 生成器 - Sequence可自由配置完美解决主键问题以及内置全局拦截插件提供全表 delete 、 update 操作智能分析阻断也可自定义拦截规则预防误操作 1.2、支持数据库 任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库具体支持情况如下 MySQLOracleDB2H2HSQLSQLitePostgreSQLSQLServerPhoenixGauss ClickHouseSybaseOceanBaseFirebirdCubridGoldilockscsiidb达梦数据库虚谷数据库人大金仓数据库南大通用(华库)数据库南大通用数据库神通数据库瀚高数据库 1.3、整体架构 2、简单示例 这里沿用上一篇文章的数据库, 向你展示SpringBoot MyBatis-Plus的使用等。 2.1、准备DB和依赖配置 创建MySQL的schema test_db, 导入SQL 文件如下 DROP TABLE IF EXISTS tb_role; /*!40101 SET saved_cs_client character_set_client */; /*!40101 SET character_set_client utf8 */; CREATE TABLE tb_role (id int(11) NOT NULL AUTO_INCREMENT,name varchar(255) NOT NULL,role_key varchar(255) NOT NULL,description varchar(255) DEFAULT NULL,create_time datetime DEFAULT NULL,update_time datetime DEFAULT NULL,PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT2 DEFAULT CHARSETutf8; /*!40101 SET character_set_client saved_cs_client */;-- -- Dumping data for table tb_role --LOCK TABLES tb_role WRITE; /*!40000 ALTER TABLE tb_role DISABLE KEYS */; INSERT INTO tb_role VALUES (1,admin,admin,admin,2021-09-08 17:09:15,2021-09-08 17:09:15); /*!40000 ALTER TABLE tb_role ENABLE KEYS */; UNLOCK TABLES;-- -- Table structure for table tb_user --DROP TABLE IF EXISTS tb_user; /*!40101 SET saved_cs_client character_set_client */; /*!40101 SET character_set_client utf8 */; CREATE TABLE tb_user (id int(11) NOT NULL AUTO_INCREMENT,user_name varchar(45) NOT NULL,password varchar(45) NOT NULL,email varchar(45) DEFAULT NULL,phone_number int(11) DEFAULT NULL,description varchar(255) DEFAULT NULL,create_time datetime DEFAULT NULL,update_time datetime DEFAULT NULL,PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT2 DEFAULT CHARSETutf8; /*!40101 SET character_set_client saved_cs_client */;-- -- Dumping data for table tb_user --LOCK TABLES tb_user WRITE; /*!40000 ALTER TABLE tb_user DISABLE KEYS */; INSERT INTO tb_user VALUES (1,qiwenjie,123456,1172814226qq.com,1212121213,afsdfsaf,2021-09-08 17:09:15,2021-09-08 17:09:15); /*!40000 ALTER TABLE tb_user ENABLE KEYS */; UNLOCK TABLES;-- -- Table structure for table tb_user_role --DROP TABLE IF EXISTS tb_user_role; /*!40101 SET saved_cs_client character_set_client */; /*!40101 SET character_set_client utf8 */; CREATE TABLE tb_user_role (user_id int(11) NOT NULL,role_id int(11) NOT NULL ) ENGINEInnoDB DEFAULT CHARSETutf8; /*!40101 SET character_set_client saved_cs_client */;-- -- Dumping data for table tb_user_role --LOCK TABLES tb_user_role WRITE; /*!40000 ALTER TABLE tb_user_role DISABLE KEYS */; INSERT INTO tb_user_role VALUES (1,1); /*!40000 ALTER TABLE tb_user_role ENABLE KEYS */; UNLOCK TABLES;引入maven依赖 dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.28/version /dependency dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.1/version /dependency增加yml配置 spring:datasource:url: jdbc:mysql://localhost:3306/db_user?useSSLfalseautoReconnecttruecharacterEncodingutf8driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: qwj930828mybatis-plus:configuration:# 是否开启二级缓存cache-enabled: trueuse-generated-keys: truedefault-executor-type: REUSEuse-actual-param-name: true2.2、定义dao RoleDao package springboot.mysql.mybatisplus.anno.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import springboot.mysql.mybatisplus.anno.entity.Role;/*** author qiwenjie*/ public interface IRoleDao extends BaseMapperRole { }UserDao package springboot.mysql.mybatisplus.anno.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import springboot.mysql.mybatisplus.anno.entity.User; import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean;import java.util.List;/*** author qiwenjie*/ public interface IUserDao extends BaseMapperUser {ListUser findList(UserQueryBean userQueryBean); }这里你也同时可以支持 BaseMapper 方式和自己定义的xml的方法比较适用于关联查询比如 findList 是自定义xml配置 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacespringboot.mysql.mybatisplus.anno.dao.IUserDaoresultMap typespringboot.mysql.mybatisplus.anno.entity.User idUserResultid propertyid columnid /result propertyuserName columnuser_name /result propertypassword columnpassword /result propertyemail columnemail /result propertyphoneNumber columnphone_number /result propertydescription columndescription /result propertycreateTime columncreate_time /result propertyupdateTime columnupdate_time /collection propertyroles ofTypespringboot.mysql.mybatisplus.anno.entity.Roleresult propertyid columnid /result propertyname columnname /result propertyroleKey columnrole_key /result propertydescription columndescription /result propertycreateTime columncreate_time /result propertyupdateTime columnupdate_time //collection/resultMapsql idselectUserSqlselect u.id, u.password, u.user_name, u.email, u.phone_number, u.description, u.create_time, u.update_time, r.name, r.role_key, r.description, r.create_time, r.update_timefrom tb_user uleft join tb_user_role ur on u.idur.user_idinner join tb_role r on ur.role_idr.id/sqlselect idfindList parameterTypespringboot.mysql.mybatisplus.anno.entity.query.UserQueryBean resultMapUserResultinclude refidselectUserSql/where u.id ! 0if testuserName ! null and userName ! AND u.user_name like concat(%, #{user_name}, %)/ifif testdescription ! null and description ! AND u.description like concat(%, #{description}, %)/ifif testphoneNumber ! null and phoneNumber ! AND u.phone_number like concat(%, #{phoneNumber}, %)/ifif testemail ! null and email ! AND u.email like concat(%, #{email}, %)/if/select /mapper 2.3、定义Service接口和实现类 UserService接口 package springboot.mysql.mybatisplus.anno.service;import com.baomidou.mybatisplus.extension.service.IService; import springboot.mysql.mybatisplus.anno.entity.User; import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean; import java.util.List;/*** author qiwenjie*/ public interface IUserService extends IServiceUser {ListUser findList(UserQueryBean userQueryBean); }User Service的实现类 package springboot.mysql.mybatisplus.anno.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import springboot.mysql.mybatisplus.anno.dao.IUserDao; import springboot.mysql.mybatisplus.anno.entity.User; import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean; import springboot.mysql.mybatisplus.anno.service.IUserService;import java.util.List;Service public class UserDoServiceImpl extends ServiceImplIUserDao, User implements IUserService {Overridepublic ListUser findList(UserQueryBean userQueryBean) {return baseMapper.findList(userQueryBean);} }Role Service 接口 package springboot.mysql.mybatisplus.anno.service;import com.baomidou.mybatisplus.extension.service.IService; import springboot.mysql.mybatisplus.anno.entity.Role; import springboot.mysql.mybatisplus.anno.entity.query.RoleQueryBean;import java.util.List;public interface IRoleService extends IServiceRole {ListRole findList(RoleQueryBean roleQueryBean); }Role Service 实现类 package springboot.mysql.mybatisplus.anno.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import springboot.mysql.mybatisplus.anno.dao.IRoleDao; import springboot.mysql.mybatisplus.anno.entity.Role; import springboot.mysql.mybatisplus.anno.entity.query.RoleQueryBean; import springboot.mysql.mybatisplus.anno.service.IRoleService; import java.util.List;Service public class RoleDoServiceImpl extends ServiceImplIRoleDao, Role implements IRoleService {Overridepublic ListRole findList(RoleQueryBean roleQueryBean) {return lambdaQuery().like(StringUtils.isNotEmpty(roleQueryBean.getName()), Role::getName, roleQueryBean.getName()).like(StringUtils.isNotEmpty(roleQueryBean.getDescription()), Role::getDescription, roleQueryBean.getDescription()).like(StringUtils.isNotEmpty(roleQueryBean.getRoleKey()), Role::getRoleKey, roleQueryBean.getRoleKey()).list();} }2.4、controller User Controller package springboot.mysql.mybatisplus.anno.controller;import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import springboot.mysql.mybatisplus.anno.entity.User; import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean; import springboot.mysql.mybatisplus.anno.entity.response.ResponseResult; import springboot.mysql.mybatisplus.anno.service.IUserService; import java.time.LocalDateTime; import java.util.List;/*** author qiwenjie*/ RestController RequestMapping(/user) public class UserController {Autowiredprivate IUserService userService;/*** param user user param* return user*/ApiOperation(Add/Edit User)PostMapping(add)public ResponseResultUser add(User user) {if (user.getId() null) {user.setCreateTime(LocalDateTime.now());}user.setUpdateTime(LocalDateTime.now());userService.save(user);return ResponseResult.success(userService.getById(user.getId()));}/*** return user list*/ApiOperation(Query User One)GetMapping(edit/{userId})public ResponseResultUser edit(PathVariable(userId) Long userId) {return ResponseResult.success(userService.getById(userId));}/*** return user list*/ApiOperation(Query User List)GetMapping(list)public ResponseResultListUser list(UserQueryBean userQueryBean) {return ResponseResult.success(userService.findList(userQueryBean));} }Role Controller package springboot.mysql.mybatisplus.anno.controller;import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import springboot.mysql.mybatisplus.anno.entity.Role; import springboot.mysql.mybatisplus.anno.entity.query.RoleQueryBean; import springboot.mysql.mybatisplus.anno.entity.response.ResponseResult; import springboot.mysql.mybatisplus.anno.service.IRoleService; import java.util.List;/*** author qiwenjie*/ RestController RequestMapping(/role) public class RoleController {Autowiredprivate IRoleService roleService;/*** return role list*/ApiOperation(Query Role List)GetMapping(list)public ResponseResultListRole list(RoleQueryBean roleQueryBean) {return ResponseResult.success(roleService.findList(roleQueryBean));} }2.5、分页配置 通过配置内置的MybatisPlusInterceptor拦截器。 package springboot.mysql.mybatisplus.anno.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** MyBatis-plus configuration, add pagination interceptor.** author qiwenjie*/ Configuration public class MyBatisConfig {/*** inject pagination interceptor.** return pagination*/Beanpublic PaginationInnerInterceptor paginationInnerInterceptor() {return new PaginationInnerInterceptor();}/*** add pagination interceptor.** return MybatisPlusInterceptor*/Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor mybatisPlusInterceptor new MybatisPlusInterceptor();mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor());return mybatisPlusInterceptor;} }3、进一步理解 MyBatis-plus学习梳理 官方文档官方案例官方源码仓库Awesome Mybatis-Plus 3.1、比较好的实践 总结下开发的过程中比较好的实践 1、Mapper层继承BaseMapper public interface IRoleDao extends BaseMapperRole { }2、Service层继承ServiceImpl并实现对应接口 public class RoleDoServiceImpl extends ServiceImplIRoleDao, Role implements IRoleService {}3、Lambda函数式查询 Override public ListRole findList(RoleQueryBean roleQueryBean) {return lambdaQuery().like(StringUtils.isNotEmpty(roleQueryBean.getName()), Role::getName, roleQueryBean.getName()).like(StringUtils.isNotEmpty(roleQueryBean.getDescription()), Role::getDescription, roleQueryBean.getDescription()).like(StringUtils.isNotEmpty(roleQueryBean.getRoleKey()), Role::getRoleKey, roleQueryBean.getRoleKey()).list(); }4、分页采用内置MybatisPlusInterceptor /*** inject pagination interceptor.* return pagination*/ Bean public PaginationInnerInterceptor paginationInnerInterceptor() {return new PaginationInnerInterceptor(); }/*** add pagination interceptor.* return MybatisPlusInterceptor*/ Bean public MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor mybatisPlusInterceptor new MybatisPlusInterceptor();mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor());return mybatisPlusInterceptor; }5、对于复杂的关联查询 可以配置原生xml方式, 在其中自定义ResultMap ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacespringboot.mysql.mybatisplus.anno.dao.IUserDaoresultMap typespringboot.mysql.mybatisplus.anno.entity.User idUserResultid propertyid columnid /result propertyuserName columnuser_name /result propertypassword columnpassword /result propertyemail columnemail /result propertyphoneNumber columnphone_number /result propertydescription columndescription /result propertycreateTime columncreate_time /result propertyupdateTime columnupdate_time /collection propertyroles ofTypespringboot.mysql.mybatisplus.anno.entity.Roleresult propertyid columnid /result propertyname columnname /result propertyroleKey columnrole_key /result propertydescription columndescription /result propertycreateTime columncreate_time /result propertyupdateTime columnupdate_time //collection/resultMapsql idselectUserSqlselect u.id, u.password, u.user_name, u.email, u.phone_number, u.description, u.create_time, u.update_time, r.name, r.role_key, r.description, r.create_time, r.update_timefrom tb_user uleft join tb_user_role ur on u.idur.user_idinner join tb_role r on ur.role_idr.id/sqlselect idfindList parameterTypespringboot.mysql.mybatisplus.anno.entity.query.UserQueryBean resultMapUserResultinclude refidselectUserSql/where u.id ! 0if testuserName ! null and userName ! AND u.user_name like concat(%, #{user_name}, %)/ifif testdescription ! null and description ! AND u.description like concat(%, #{description}, %)/ifif testphoneNumber ! null and phoneNumber ! AND u.phone_number like concat(%, #{phoneNumber}, %)/ifif testemail ! null and email ! AND u.email like concat(%, #{email}, %)/if/select /mapper 3.2、除了分页插件之外还提供了哪些插件 插件都是基于拦截器实现的MyBatis-Plus提供了如下插件 自动分页: PaginationInnerInterceptor多租户: TenantLineInnerInterceptor动态表名: DynamicTableNameInnerInterceptor乐观锁: OptimisticLockerInnerInterceptorsql 性能规范: IllegalSQLInnerInterceptor防止全表更新与删除: BlockAttackInnerInterceptor 4、示例源码 todo
http://www.hkea.cn/news/14510105/

相关文章:

  • 陕西网站推广费用wordpress 翻译 _e
  • 东庄水利建设公司网站做网站商城怎么样
  • 接给别人做网站的活建网站怎么上线
  • 网站建设与管理 吴代文深圳工业设计师工资一般多少
  • 企业做网站的坏处精准营销推广软件
  • 网站排名top排行榜做网站网上商城多少钱
  • 专业制作网站推荐阿里云代理网站怎么做
  • 51网站哪里去了成都微信小程序
  • 沈阳营销网站建设搜索网站有哪几个
  • 可以做哪些网站有哪些内容html代码在线
  • 做网站开发使用百分比的好处网页设计课程期末总结
  • 集团网站方案策划书wordpress 网页内嵌
  • 那家专门做特卖的网站网站个人备案麻烦吗
  • 做网站和做产品wordpress官网中文版下载
  • 怎样建设电子商务网站手工制作大全视频教程
  • wordpress网站变灰河南建站网站
  • 58做网站一年多少钱做企业网站到哪里找
  • 织梦做中英文网站步骤网站界面设计套题
  • 提高网站规范化建设南山区公司网站制作
  • 福田企业建站推广服务公司网站开发费分摊多少年
  • 建设信用卡中心网站首页附近最近的广告公司
  • 网站方案书山东农业工程学院教务网络管理系统
  • 浙江省住房和城乡建设局网站营销型网站模板下载
  • 装修设计网站免费大型网站开发工具
  • 在线免费视频网站推广网站用户账号ip查询
  • 网站内容搜索IT周末做网站违反制度么
  • 织梦婚纱网站模板网站建设图片素材
  • asp三层架构做网站企业网站宣传方案
  • 北京做兼职从哪个网站好网站 字体
  • 网站模板怎么用法济南网站推广建设有限公司