aspcms 生成网站地图,推广自己的网站需要怎么做,云南省建设工程网站,网站空间需要备案吗相关依赖 MyBatis-Plus涉及的依赖主要是Mybatis-start、和分页插件的依赖#xff0c;不考虑使用额外分页插件的前提下#xff0c;只需要mybatis-plus-boot-starter一个依赖即可与SpringBoot集成#xff1a;
!--Mybatis-plugs--dependencygroupIdco…相关依赖 MyBatis-Plus涉及的依赖主要是Mybatis-start、和分页插件的依赖不考虑使用额外分页插件的前提下只需要mybatis-plus-boot-starter一个依赖即可与SpringBoot集成
!--Mybatis-plugs--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.2/version/dependencySpringBoot基本引入Mybatis-Plus用法
Mybatis-plus的配置详解
SpringBoot引入Mybatis-Plus遵循start原则配置化继承自Mybatis关于数据库配置保持一致即可
spring:#数据库连接配置datasource:url: jdbc:mysql://localhost:3306/db_blog?useUnicodetruecharacterEncodingutf8serverTimezoneAsia/Shanghaiusername: rootpassword: 1234driver-class-name: com.mysql.cj.jdbc.Driver
以下是Mybatis-Plus独有的配置
# MyBatis-Plus 配置
mybatis-plus:# MyBatis-Plus 的全局配置global-config:#banner图是否在日志中输出banner: off# 数据库相关配置db-config:# 主键类型id-type: auto # 可选值auto, assign_id, assign_uuid, input# MyBatis-Plus 的配置configuration:# 是否开启驼峰命名转换map-underscore-to-camel-case: true# 日志实现log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制台输出 SQL 日志# 类路径下的 Mapper XML 文件位置mapper-locations: classpath*:mapper/*.xml# 类型别名包type-aliases-package: com.example.myapp.entity # 实体类所在的包默认不需要额外配置 如无特殊要求情况下只需要简化配置即可 其他配置项会自动不使用或者使用缺省值以下为一个简化配置样例
mybatis-plus:global-config:banner: offmapper-locations: classpath*:mapper/*.xml#configuration:#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 调试环境下打开开启sql日志打印
基本代码结构
实体类
实体类定义实体类主要与表或者业务概念对应一般与表对应时可配合使用相关的Table家族注解
Data
//指定数据库中的表
TableName(tb_artitle_data)
public class ArticleEntity implements Serializable {TableIdprivate Int id;private String artName;private String artDesc;private String artUrl;private String artTime;//表中无此字段Mapper处理时可略过此字段TableField(exist false)private String auth;
}
创建Mapper相比于Mybatis的Mapper需要与XML关联Mybatis-Plus的优势在于对于基础的增删改查可以不需要额外创建XML如下
Mapper
//只需要Mapper注解然后继承BaseMapper即可泛型指定为对应表的实体类
public interface ArticleMapper extends BaseMapperArticleEntity {
} 简单使用直接在Service代码中引入Mapper使用BaseMapper中已经定义好的方法结合QueryWrapper方法省略SQL
Service
public class ArticleServiceImpl implements ArticleService {Autowiredprivate ArticleMapper articleMapper;// 新增文章Overridepublic boolean addArticle(ArticleEntity article) {return articleMapper.insert(article) 0;}// 删除文章Overridepublic boolean deleteArticle(String id) {return articleMapper.deleteById(id) 0;}// 更新文章Overridepublic boolean updateArticle(ArticleEntity article) {return articleMapper.updateById(article) 0;}// 根据ID查询文章Overridepublic ArticleEntity getArticleById(String id) {return articleMapper.selectById(id);}// 查询所有文章Overridepublic ListArticleEntity getAllArticles() {return articleMapper.selectList(null);}// 分页查询文章Overridepublic IPageArticleEntity getArticlesByPage(int currentPage, int pageSize) {PageArticleEntity page new Page(currentPage, pageSize);return articleMapper.selectPage(page, null);}// 多条件组合查询文章public ListArticleEntity getArticlesByMultipleConditions(String artName, String artDesc, String artUrl, String startTime, String endTime) {QueryWrapperArticleEntity queryWrapper new QueryWrapper();if (artName ! null !artName.isEmpty()) {queryWrapper.eq(art_name, artName);}if (artDesc ! null !artDesc.isEmpty()) {queryWrapper.like(art_desc, artDesc);}if (artUrl ! null !artUrl.isEmpty()) {queryWrapper.eq(art_url, artUrl);}if (startTime ! null !startTime.isEmpty() endTime ! null !endTime.isEmpty()) {queryWrapper.between(art_time, startTime, endTime);}return articleMapper.selectList(queryWrapper);}
}
还可以基于Mybatis-Plus自带的IService风格进行开发
public interface ArticleService extends IServiceArticleEntity {// 多条件查询文章ListArticleEntity getArticlesByMultipleConditions(String artName, String artDesc, String artUrl, String startTime, String endTime);
}Service
public class ArticleServiceImpl extends ServiceImplArticleMapper, ArticleEntity implements ArticleService {// 新增文章Overridepublic boolean save(ArticleEntity entity) {return super.save(entity);}// 删除文章Overridepublic boolean removeById(String id) {return super.removeById(id);}// 更新文章Overridepublic boolean updateById(ArticleEntity entity) {return super.updateById(entity);}// 根据ID查询文章Overridepublic ArticleEntity getById(String id) {return super.getById(id);}// 查询所有文章public ListArticleEntity listAll() {return super.list();}// 分页查询文章public IPageArticleEntity pageList(PageArticleEntity page) {return super.page(page);}// 多条件查询文章Overridepublic ListArticleEntity getArticlesByMultipleConditions(String artName, String artDesc, String artUrl, String startTime, String endTime) {QueryWrapperArticleEntity queryWrapper new QueryWrapper();// 动态添加查询条件if (artName ! null !artName.isEmpty()) {queryWrapper.eq(art_name, artName);}if (artDesc ! null !artDesc.isEmpty()) {queryWrapper.like(art_desc, artDesc);}if (artUrl ! null !artUrl.isEmpty()) {queryWrapper.eq(art_url, artUrl);}if (startTime ! null !startTime.isEmpty() endTime ! null !endTime.isEmpty()) {queryWrapper.between(art_time, startTime, endTime);}return list(queryWrapper);}
}
Mapper层加强
基于注解编写复杂SQL
MyBatis-Plus 提供了注解方式可以在 Mapper 层编写复杂的 SQL 语句。这种方式省略掉了繁重的XML文件
Mapper
public interface ArticleMapper extends BaseMapperArticleEntity {// 根据多个条件查询文章Select({script,SELECT * FROM tb_article_data,WHERE 11,if testartName ! null and !artName.isEmpty() AND art_name #{artName} /if,if testartDesc ! null and !artDesc.isEmpty() AND art_desc LIKE CONCAT(%, #{artDesc}, %) /if,if testartUrl ! null and !artUrl.isEmpty() AND art_url #{artUrl} /if,if teststartTime ! null and !startTime.isEmpty() and endTime ! null and !endTime.isEmpty() AND art_time BETWEEN #{startTime} AND #{endTime} /if,/script})ListArticleEntity selectByMultipleConditions(Param(artName) String artName,Param(artDesc) String artDesc,Param(artUrl) String artUrl,Param(startTime) String startTime,Param(endTime) String endTime);// 插入文章并返回自动生成的IDInsert({INSERT INTO tb_article_data (art_name, art_desc, art_url, art_time),VALUES (#{artName}, #{artDesc}, #{artUrl}, #{artTime}),RETURNING id})Options(useGeneratedKeys true, keyProperty id, keyColumn id)int insertAndReturnId(Param(article) ArticleEntity article);// 更新文章信息Update({script,UPDATE tb_article_data,SET art_name #{artName}, art_desc #{artDesc}, art_url #{artUrl}, art_time #{artTime},WHERE id #{id},/script})int updateArticle(Param(article) ArticleEntity article);// 删除文章Delete(DELETE FROM tb_article_data WHERE id #{id})int deleteArticleById(Param(id) String id);// 使用存储过程Select(CALL GetArticleById(#{id}))Results({Result(property id, column id, jdbcType JdbcType.VARCHAR),Result(property artName, column art_name, jdbcType JdbcType.VARCHAR),Result(property artDesc, column art_desc, jdbcType JdbcType.VARCHAR),Result(property artUrl, column art_url, jdbcType JdbcType.VARCHAR),Result(property artTime, column art_time, jdbcType JdbcType.VARCHAR)})ArticleEntity callGetArticleById(Param(id) String id);// 使用XML中的SQL片段Select(${sqlFragment})ListArticleEntity selectBySqlFragment(Param(sqlFragment) String sqlFragment);//查询文章数量Select(SELECT COUNT(*) FROM tb_article_data)int countAllArticles();
}
基于XML编写复杂SQL
也可以沿用Mybatis的方式使用XML进行复杂SQL的编写
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.example.mapper.ArticleMapper!-- 根据多个条件查询文章 --select idselectByMultipleConditions resultTypecom.example.entity.ArticleEntitySELECT * FROM tb_article_datawhereif testartName ! null and artName ! AND art_name #{artName}/ifif testartDesc ! null and artDesc ! AND art_desc LIKE CONCAT(%, #{artDesc}, %)/ifif testartUrl ! null and artUrl ! AND art_url #{artUrl}/ifif teststartTime ! null and startTime ! and endTime ! null and endTime ! AND art_time BETWEEN #{startTime} AND #{endTime}/if/where/select!-- 插入文章并返回自动生成的ID --insert idinsertAndReturnId useGeneratedKeystrue keyPropertyidINSERT INTO tb_article_data (art_name, art_desc, art_url, art_time)VALUES (#{artName}, #{artDesc}, #{artUrl}, #{artTime})/insert!-- 更新文章信息 --update idupdateArticleUPDATE tb_article_dataSETart_name #{artName},art_desc #{artDesc},art_url #{artUrl},art_time #{artTime}WHERE id #{id}/update!-- 删除文章 --delete iddeleteArticleByIdDELETE FROM tb_article_data WHERE id #{id}/delete!-- 使用存储过程 --select idcallGetArticleById resultTypecom.example.entity.ArticleEntityCALL GetArticleById(#{id})/select!-- 查询文章数量 --select idcountAllArticles resultTypeintSELECT COUNT(*) FROM tb_article_data/select/mapper
Mybatis-Plus的分页实现
内置的分页拦截器
MyBatis-Plus 提供了内置的分页拦截器可以通过配置来启用分页功能。
Configuration
public class MyBatisPlusConfigure {/*** 分页插件自动识别数据库类型*/Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();// 添加分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}}
使用IPage进行分页参数设置和结果获取
Service
public class ArticleServiceImpl extends ServiceImplArticleMapper, ArticleEntity implements ArticleService {// 分页查询文章public IPageArticleEntity pageList(int currentPage, int pageSize) {PageArticleEntity page new Page(currentPage, pageSize);return baseMapper.selectPage(page, null);}// 多条件分页查询文章public IPageArticleEntity pageListByMultipleConditions(int currentPage, int pageSize, String artName, String artDesc, String artUrl, String startTime, String endTime) {PageArticleEntity page new Page(currentPage, pageSize);QueryWrapperArticleEntity queryWrapper new QueryWrapper();if (artName ! null !artName.isEmpty()) {queryWrapper.eq(art_name, artName);}if (artDesc ! null !artDesc.isEmpty()) {queryWrapper.like(art_desc, artDesc);}if (artUrl ! null !artUrl.isEmpty()) {queryWrapper.eq(art_url, artUrl);}if (startTime ! null !startTime.isEmpty() endTime ! null !endTime.isEmpty()) {queryWrapper.between(art_time, startTime, endTime);}return baseMapper.selectPage(page, queryWrapper);}
} 使用XML的SQL分页需要将Page作为参数给到Mapper层的方法
public interface ArticleMapper extends BaseMapperArticleEntity {// 查询所有文章ListArticleEntity selectAllArticles();// 根据多个条件查询文章ListArticleEntity selectByMultipleConditions(Param(page) PageAtricleEntity page,Param(artName) String artName,Param(artDesc) String artDesc,Param(artUrl) String artUrl,Param(startTime) String startTime,Param(endTime) String endTime);
}
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.example.mapper.ArticleMapper!-- 查询所有文章 --select idselectAllArticles resultTypecom.example.entity.ArticleEntitySELECT * FROM tb_article_data/select!-- 根据多个条件查询文章 --select idselectByMultipleConditions resultTypecom.example.entity.ArticleEntitySELECT * FROM tb_article_datawhereif testartName ! null and artName ! AND art_name #{artName}/ifif testartDesc ! null and artDesc ! AND art_desc LIKE CONCAT(%, #{artDesc}, %)/ifif testartUrl ! null and artUrl ! AND art_url #{artUrl}/ifif teststartTime ! null and startTime ! and endTime ! null and endTime ! AND art_time BETWEEN #{startTime} AND #{endTime}/if/where/select/mapper
Service
public class ArticleServiceImpl extends ServiceImplArticleMapper, ArticleEntity implements ArticleService {// 使用 XML 定义的多条件查询方法进行分页查询public IPageArticleEntity pageListByMultipleConditionsUsingXml(int currentPage, int pageSize, String artName, String artDesc, String artUrl, String startTime, String endTime) {PageArticleEntity page new Page(currentPage, pageSize);IpageArticleEntity data baseMapper.selectByMultipleConditions(page ,artName, artDesc, artUrl, startTime, endTime);return data;}
}
使用第三方的分页插件
PageHelper 是一个非常流行的 MyBatis 分页插件具有易用简便的特点且与Mybatis-Plus无缝连接。
引入依赖 dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion5.2.0/version/dependency
在 application.yml 中配置 PageHelper
pagehelper:helperDialect: mysqlreasonable: truesupportMethodsArguments: trueparams: countcountSql
使用PageHelper
Service
public class ArticleServiceImpl extends ServiceImplArticleMapper, ArticleEntity implements ArticleService {// 分页查询文章public PageInfoArticleEntity pageListWithPageHelper(int currentPage, int pageSize) {PageHelper.startPage(currentPage, pageSize);ListArticleEntity articles baseMapper.selectList(null);return new PageInfo(articles);}// 多条件分页查询文章public PageInfoArticleEntity pageListByMultipleConditionsWithPageHelper(int currentPage, int pageSize, String artName, String artDesc, String artUrl, String startTime, String endTime) {PageHelper.startPage(currentPage, pageSize);QueryWrapperArticleEntity queryWrapper new QueryWrapper();if (artName ! null !artName.isEmpty()) {queryWrapper.eq(art_name, artName);}if (artDesc ! null !artDesc.isEmpty()) {queryWrapper.like(art_desc, artDesc);}if (artUrl ! null !artUrl.isEmpty()) {queryWrapper.eq(art_url, artUrl);}if (startTime ! null !startTime.isEmpty() endTime ! null !endTime.isEmpty()) {queryWrapper.between(art_time, startTime, endTime);}ListArticleEntity articles baseMapper.selectList(queryWrapper);return new PageInfo(articles);}
}