Commit 5aa1d56b authored by 陈晓晋's avatar 陈晓晋

升级若依mybatis到mybatis-plus以及网站管理测试用例20230627

parent 80e253d0
......@@ -30,6 +30,7 @@
<poi.version>4.1.2</poi.version>
<velocity.version>2.3</velocity.version>
<jwt.version>0.9.1</jwt.version>
<mybatis-plus.version>3.5.2</mybatis-plus.version>
</properties>
<!-- 依赖声明 -->
......@@ -177,6 +178,25 @@
<version>${ruoyi.version}</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
......
......@@ -101,13 +101,21 @@ token:
expireTime: 30
# MyBatis配置
mybatis:
# 搜索指定包别名
typeAliasesPackage: com.ruoyi.**.domain
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath*:mapper/**/*Mapper.xml
# 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml
#mybatis:
# ## # 搜索指定包别名
# ## typeAliasesPackage: com.ruoyi.**.domain
# ## # 配置mapper的扫描,找到所有的mapper.xml映射文件
# ## mapperLocations: classpath*:mapper/**/*Mapper.xml
# ## # 加载全局的配置文件
# ## configLocation: classpath:mybatis/mybatis-config.xml
# MyBatis Plus配置
mybatis-plus:
# 搜索指定包别名
typeAliasesPackage: com.ruoyi.**.domain
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath*:mapper/**/*Mapper.xml
# 加载全局的配置文件
# configLocation: classpath:mybatis/mybatis-config.xml
# PageHelper分页插件
pagehelper:
......
......@@ -58,13 +58,13 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- 动态数据源 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!-- 阿里JSON解析器 -->
<dependency>
......@@ -126,6 +126,19 @@
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.ruoyi.common.core.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* Entity基类
*
* @author ruoyi
*/
@Data
public class BaseEntityClean implements Serializable
{
private static final long serialVersionUID = 1L;
/** 创建者 */
private String createBy;
/** 创建时间 */
@TableField(value = "create_time", fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新者 */
private String updateBy;
/** 更新时间 */
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/** 备注 */
private String remark;
}
......@@ -26,7 +26,7 @@ import com.ruoyi.common.utils.StringUtils;
/**
* Mybatis支持*匹配扫描包
*
*
* @author ruoyi
*/
@Configuration
......@@ -113,20 +113,20 @@ public class MyBatisConfig
return resources.toArray(new Resource[resources.size()]);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
{
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
String mapperLocations = env.getProperty("mybatis.mapperLocations");
String configLocation = env.getProperty("mybatis.configLocation");
typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
VFS.addImplClass(SpringBootVFS.class);
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
return sessionFactory.getObject();
}
//@Bean
//public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
//{
// String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
// String mapperLocations = env.getProperty("mybatis.mapperLocations");
// String configLocation = env.getProperty("mybatis.configLocation");
// typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
// VFS.addImplClass(SpringBootVFS.class);
//
// final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
// sessionFactory.setDataSource(dataSource);
// sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
// sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
// sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
// return sessionFactory.getObject();
//}
}
\ No newline at end of file
package com.ruoyi.framework.config;
/**
* @description:
* @author: pyz
* @create: 2021-12-28 15:04
**/
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* Mybatis Plus 配置
*
* @author ruoyi
*/
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig
{
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor()
{
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor());
// 乐观锁插件
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
// 阻断插件
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
return interceptor;
}
/**
* 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
*/
public PaginationInnerInterceptor paginationInnerInterceptor()
{
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置数据库类型为mysql
paginationInnerInterceptor.setDbType(DbType.MYSQL);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L);
return paginationInnerInterceptor;
}
/**
* 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
*/
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
{
return new OptimisticLockerInnerInterceptor();
}
/**
* 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
*/
public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
{
return new BlockAttackInnerInterceptor();
}
}
......@@ -19,7 +19,7 @@ import com.ruoyi.common.core.domain.TreeEntity;
* @date ${datetime}
*/
#if($table.crud || $table.sub)
#set($Entity="BaseEntity")
#set($Entity="BaseEntityClean")
#elseif($table.tree)
#set($Entity="TreeEntity")
#end
......
......@@ -2,6 +2,7 @@ package ${packageName}.mapper;
import java.util.List;
import ${packageName}.domain.${ClassName};
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
#if($table.sub)
import ${packageName}.domain.${subClassName};
#end
......@@ -12,7 +13,7 @@ import ${packageName}.domain.${subClassName};
* @author ${author}
* @date ${datetime}
*/
public interface ${ClassName}Mapper
public interface ${ClassName}Mapper extends BaseMapper<${ClassName}>
{
/**
* 查询${functionName}
......
......@@ -2,14 +2,14 @@ package ${packageName}.service;
import java.util.List;
import ${packageName}.domain.${ClassName};
import com.baomidou.mybatisplus.extension.service.IService;
/**
* ${functionName}Service接口
*
* @author ${author}
* @date ${datetime}
*/
public interface I${ClassName}Service
public interface I${ClassName}Service extends IService<${ClassName}>
{
/**
* 查询${functionName}
......
......@@ -18,7 +18,7 @@ import ${packageName}.domain.${subClassName};
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
* ${functionName}Service业务层处理
*
......@@ -26,7 +26,7 @@ import ${packageName}.service.I${ClassName}Service;
* @date ${datetime}
*/
@Service
public class ${ClassName}ServiceImpl implements I${ClassName}Service
public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}> implements I${ClassName}Service
{
@Autowired
private ${ClassName}Mapper ${className}Mapper;
......
package com.ruoyi.system.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Websites;
import com.ruoyi.system.service.IWebsitesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
* 网站管理Controller
*
* @author ruoyi
* @date 2023-06-27
*/
@RestController
@RequestMapping("/system/websites")
public class WebsitesController extends BaseController
{
@Autowired
private IWebsitesService websitesService;
/**
* 查询网站管理列表
*/
@PreAuthorize("@ss.hasPermi('system:websites:list')")
@GetMapping("/list")
public TableDataInfo list(Websites websites)
{
startPage();
List<Websites> list = websitesService.selectWebsitesList(websites);
return getDataTable(list);
}
/**
* 导出网站管理列表
*/
@PreAuthorize("@ss.hasPermi('system:websites:export')")
@Log(title = "网站管理", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, Websites websites)
{
List<Websites> list = websitesService.selectWebsitesList(websites);
ExcelUtil<Websites> util = new ExcelUtil<Websites>(Websites.class);
util.exportExcel(response, list, "网站管理数据");
}
/**
* 获取网站管理详细信息
*/
@PreAuthorize("@ss.hasPermi('system:websites:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return success(websitesService.selectWebsitesById(id));
}
/**
* 新增网站管理
*/
@PreAuthorize("@ss.hasPermi('system:websites:add')")
@Log(title = "网站管理", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody Websites websites)
{
return toAjax(websitesService.insertWebsites(websites));
}
/**
* 修改网站管理
*/
@PreAuthorize("@ss.hasPermi('system:websites:edit')")
@Log(title = "网站管理", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody Websites websites)
{
return toAjax(websitesService.updateWebsites(websites));
}
/**
* 删除网站管理
*/
@PreAuthorize("@ss.hasPermi('system:websites:remove')")
@Log(title = "网站管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids)
{
return toAjax(websitesService.deleteWebsitesByIds(ids));
}
}
package com.ruoyi.system.domain;
import com.ruoyi.common.core.domain.BaseEntityClean;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
/**
* 网站管理对象 websites
*
* @author ruoyi
* @date 2023-06-27
*/
public class Websites extends BaseEntityClean
{
private static final long serialVersionUID = 1L;
/** 主键 */
private Long id;
/** 站点名称 */
@Excel(name = "站点名称")
private String name;
/** 网址 */
@Excel(name = "网址")
private String url;
/** Alexa 排名 */
@Excel(name = "Alexa 排名")
private Long alexa;
/** 国家 */
@Excel(name = "国家")
private String country;
public void setId(Long id)
{
this.id = id;
}
public Long getId()
{
return id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setUrl(String url)
{
this.url = url;
}
public String getUrl()
{
return url;
}
public void setAlexa(Long alexa)
{
this.alexa = alexa;
}
public Long getAlexa()
{
return alexa;
}
public void setCountry(String country)
{
this.country = country;
}
public String getCountry()
{
return country;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("name", getName())
.append("url", getUrl())
.append("alexa", getAlexa())
.append("country", getCountry())
.toString();
}
}
package com.ruoyi.system.mapper;
import java.util.List;
import com.ruoyi.system.domain.Websites;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* 网站管理Mapper接口
*
* @author ruoyi
* @date 2023-06-27
*/
public interface WebsitesMapper extends BaseMapper<Websites>
{
/**
* 查询网站管理
*
* @param id 网站管理主键
* @return 网站管理
*/
public Websites selectWebsitesById(Long id);
/**
* 查询网站管理列表
*
* @param websites 网站管理
* @return 网站管理集合
*/
public List<Websites> selectWebsitesList(Websites websites);
/**
* 新增网站管理
*
* @param websites 网站管理
* @return 结果
*/
public int insertWebsites(Websites websites);
/**
* 修改网站管理
*
* @param websites 网站管理
* @return 结果
*/
public int updateWebsites(Websites websites);
/**
* 删除网站管理
*
* @param id 网站管理主键
* @return 结果
*/
public int deleteWebsitesById(Long id);
/**
* 批量删除网站管理
*
* @param ids 需要删除的数据主键集合
* @return 结果
*/
public int deleteWebsitesByIds(Long[] ids);
}
package com.ruoyi.system.service;
import java.util.List;
import com.ruoyi.system.domain.Websites;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* 网站管理Service接口
*
* @author ruoyi
* @date 2023-06-27
*/
public interface IWebsitesService extends IService<Websites>
{
/**
* 查询网站管理
*
* @param id 网站管理主键
* @return 网站管理
*/
public Websites selectWebsitesById(Long id);
/**
* 查询网站管理列表
*
* @param websites 网站管理
* @return 网站管理集合
*/
public List<Websites> selectWebsitesList(Websites websites);
/**
* 新增网站管理
*
* @param websites 网站管理
* @return 结果
*/
public int insertWebsites(Websites websites);
/**
* 修改网站管理
*
* @param websites 网站管理
* @return 结果
*/
public int updateWebsites(Websites websites);
/**
* 批量删除网站管理
*
* @param ids 需要删除的网站管理主键集合
* @return 结果
*/
public int deleteWebsitesByIds(Long[] ids);
/**
* 删除网站管理信息
*
* @param id 网站管理主键
* @return 结果
*/
public int deleteWebsitesById(Long id);
}
package com.ruoyi.system.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WebsitesMapper;
import com.ruoyi.system.domain.Websites;
import com.ruoyi.system.service.IWebsitesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
* 网站管理Service业务层处理
*
* @author ruoyi
* @date 2023-06-27
*/
@Service
public class WebsitesServiceImpl extends ServiceImpl<WebsitesMapper, Websites> implements IWebsitesService
{
@Autowired
private WebsitesMapper websitesMapper;
/**
* 查询网站管理
*
* @param id 网站管理主键
* @return 网站管理
*/
@Override
public Websites selectWebsitesById(Long id)
{
return websitesMapper.selectWebsitesById(id);
}
/**
* 查询网站管理列表
*
* @param websites 网站管理
* @return 网站管理
*/
@Override
public List<Websites> selectWebsitesList(Websites websites)
{
return websitesMapper.selectWebsitesList(websites);
}
/**
* 新增网站管理
*
* @param websites 网站管理
* @return 结果
*/
@Override
public int insertWebsites(Websites websites)
{
return websitesMapper.insertWebsites(websites);
}
/**
* 修改网站管理
*
* @param websites 网站管理
* @return 结果
*/
@Override
public int updateWebsites(Websites websites)
{
return websitesMapper.updateWebsites(websites);
}
/**
* 批量删除网站管理
*
* @param ids 需要删除的网站管理主键
* @return 结果
*/
@Override
public int deleteWebsitesByIds(Long[] ids)
{
return websitesMapper.deleteWebsitesByIds(ids);
}
/**
* 删除网站管理信息
*
* @param id 网站管理主键
* @return 结果
*/
@Override
public int deleteWebsitesById(Long id)
{
return websitesMapper.deleteWebsitesById(id);
}
}
<?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.ruoyi.system.mapper.WebsitesMapper">
<resultMap type="Websites" id="WebsitesResult">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="url" column="url" />
<result property="alexa" column="alexa" />
<result property="country" column="country" />
</resultMap>
<sql id="selectWebsitesVo">
select id, name, url, alexa, country from websites
</sql>
<select id="selectWebsitesList" parameterType="Websites" resultMap="WebsitesResult">
<include refid="selectWebsitesVo"/>
<where>
<if test="name != null and name != ''"> and name like concat('%', #{name}, '%')</if>
<if test="url != null and url != ''"> and url = #{url}</if>
<if test="alexa != null "> and alexa = #{alexa}</if>
<if test="country != null and country != ''"> and country = #{country}</if>
</where>
</select>
<select id="selectWebsitesById" parameterType="Long" resultMap="WebsitesResult">
<include refid="selectWebsitesVo"/>
where id = #{id}
</select>
<insert id="insertWebsites" parameterType="Websites" useGeneratedKeys="true" keyProperty="id">
insert into websites
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">name,</if>
<if test="url != null and url != ''">url,</if>
<if test="alexa != null">alexa,</if>
<if test="country != null and country != ''">country,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">#{name},</if>
<if test="url != null and url != ''">#{url},</if>
<if test="alexa != null">#{alexa},</if>
<if test="country != null and country != ''">#{country},</if>
</trim>
</insert>
<update id="updateWebsites" parameterType="Websites">
update websites
<trim prefix="SET" suffixOverrides=",">
<if test="name != null and name != ''">name = #{name},</if>
<if test="url != null and url != ''">url = #{url},</if>
<if test="alexa != null">alexa = #{alexa},</if>
<if test="country != null and country != ''">country = #{country},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteWebsitesById" parameterType="Long">
delete from websites where id = #{id}
</delete>
<delete id="deleteWebsitesByIds" parameterType="String">
delete from websites where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment