diff --git a/agri-admin/src/main/resources/application.yml b/agri-admin/src/main/resources/application.yml index 6cc7909..8af8bd2 100644 --- a/agri-admin/src/main/resources/application.yml +++ b/agri-admin/src/main/resources/application.yml @@ -106,8 +106,8 @@ token: # 令牌有效期(默认30分钟) expireTime: 30 -# MyBatis配置 -mybatis: +# MyBatis Plus配置 +mybatis-plus: # 搜索指定包别名 typeAliasesPackage: com.agri.**.domain # 配置mapper的扫描,找到所有的mapper.xml映射文件 diff --git a/agri-common/pom.xml b/agri-common/pom.xml index b84f9f0..af02191 100644 --- a/agri-common/pom.xml +++ b/agri-common/pom.xml @@ -126,7 +126,12 @@ javax.servlet javax.servlet-api + + + com.baomidou + mybatis-plus-boot-starter + \ No newline at end of file diff --git a/agri-framework/src/main/java/com/agri/framework/config/MyBatisConfig.java b/agri-framework/src/main/java/com/agri/framework/config/MyBatisConfig.java deleted file mode 100644 index f906f23..0000000 --- a/agri-framework/src/main/java/com/agri/framework/config/MyBatisConfig.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.agri.framework.config; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import javax.sql.DataSource; -import org.apache.ibatis.io.VFS; -import org.apache.ibatis.session.SqlSessionFactory; -import org.mybatis.spring.SqlSessionFactoryBean; -import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; -import org.springframework.core.io.DefaultResourceLoader; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.core.type.classreading.CachingMetadataReaderFactory; -import org.springframework.core.type.classreading.MetadataReader; -import org.springframework.core.type.classreading.MetadataReaderFactory; -import org.springframework.util.ClassUtils; -import com.agri.common.utils.StringUtils; - -/** - * Mybatis支持*匹配扫描包 - * - * @author ruoyi - */ -@Configuration -public class MyBatisConfig -{ - @Autowired - private Environment env; - - static final String DEFAULT_RESOURCE_PATTERN = "**/*.class"; - - public static String setTypeAliasesPackage(String typeAliasesPackage) - { - ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); - MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver); - List allResult = new ArrayList(); - try - { - for (String aliasesPackage : typeAliasesPackage.split(",")) - { - List result = new ArrayList(); - aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX - + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN; - Resource[] resources = resolver.getResources(aliasesPackage); - if (resources != null && resources.length > 0) - { - MetadataReader metadataReader = null; - for (Resource resource : resources) - { - if (resource.isReadable()) - { - metadataReader = metadataReaderFactory.getMetadataReader(resource); - try - { - result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName()); - } - catch (ClassNotFoundException e) - { - e.printStackTrace(); - } - } - } - } - if (result.size() > 0) - { - HashSet hashResult = new HashSet(result); - allResult.addAll(hashResult); - } - } - if (allResult.size() > 0) - { - typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0])); - } - else - { - throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包"); - } - } - catch (IOException e) - { - e.printStackTrace(); - } - return typeAliasesPackage; - } - - public Resource[] resolveMapperLocations(String[] mapperLocations) - { - ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); - List resources = new ArrayList(); - if (mapperLocations != null) - { - for (String mapperLocation : mapperLocations) - { - try - { - Resource[] mappers = resourceResolver.getResources(mapperLocation); - resources.addAll(Arrays.asList(mappers)); - } - catch (IOException e) - { - // ignore - } - } - } - 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(); - } -} \ No newline at end of file diff --git a/agri-framework/src/main/java/com/agri/framework/config/MybatisPlusConfig.java b/agri-framework/src/main/java/com/agri/framework/config/MybatisPlusConfig.java new file mode 100644 index 0000000..7a8603c --- /dev/null +++ b/agri-framework/src/main/java/com/agri/framework/config/MybatisPlusConfig.java @@ -0,0 +1,62 @@ +package com.agri.framework.config; + +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(); + } +} \ No newline at end of file diff --git a/agri-system/src/main/java/com/agri/system/controller/SysStudentController.java b/agri-system/src/main/java/com/agri/system/controller/SysStudentController.java new file mode 100644 index 0000000..a7e5b1e --- /dev/null +++ b/agri-system/src/main/java/com/agri/system/controller/SysStudentController.java @@ -0,0 +1,104 @@ +package com.agri.system.controller; + +import com.agri.common.annotation.Log; +import com.agri.common.core.controller.BaseController; +import com.agri.common.core.domain.AjaxResult; +import com.agri.common.core.page.TableDataInfo; +import com.agri.common.enums.BusinessType; +import com.agri.common.utils.poi.ExcelUtil; +import com.agri.system.domain.SysStudent; +import com.agri.system.service.ISysStudentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +/** + * 学生信息Controller + * + * @author ruoyi + */ +@RestController +@RequestMapping("/system/student") +public class SysStudentController extends BaseController +{ + @Autowired + private ISysStudentService sysStudentService; + + /** + * 查询学生信息列表 + */ + @PreAuthorize("@ss.hasPermi('system:student:list')") + @GetMapping("/list") + public TableDataInfo list(SysStudent sysStudent) + { + startPage(); + List list = sysStudentService.queryList(sysStudent); + return getDataTable(list); + } + + /** + * 导出学生信息列表 + */ + @PreAuthorize("@ss.hasPermi('system:student:export')") + @Log(title = "学生信息", businessType = BusinessType.EXPORT) + @GetMapping("/export") + public AjaxResult export(SysStudent sysStudent) + { + List list = sysStudentService.queryList(sysStudent); + ExcelUtil util = new ExcelUtil(SysStudent.class); + return util.exportExcel(list, "student"); + } + + /** + * 获取学生信息详细信息 + */ + @PreAuthorize("@ss.hasPermi('system:student:query')") + @GetMapping(value = "/{studentId}") + public AjaxResult getInfo(@PathVariable("studentId") Long studentId) + { + return AjaxResult.success(sysStudentService.getById(studentId)); + } + + /** + * 新增学生信息 + */ + @PreAuthorize("@ss.hasPermi('system:student:add')") + @Log(title = "学生信息", businessType = BusinessType.INSERT) + @PostMapping + public AjaxResult add(@RequestBody SysStudent sysStudent) + { + return toAjax(sysStudentService.save(sysStudent)); + } + + /** + * 修改学生信息 + */ + @PreAuthorize("@ss.hasPermi('system:student:edit')") + @Log(title = "学生信息", businessType = BusinessType.UPDATE) + @PutMapping + public AjaxResult edit(@RequestBody SysStudent sysStudent) + { + return toAjax(sysStudentService.updateById(sysStudent)); + } + + /** + * 删除学生信息 + */ + @PreAuthorize("@ss.hasPermi('system:student:remove')") + @Log(title = "学生信息", businessType = BusinessType.DELETE) + @DeleteMapping("/{studentIds}") + public AjaxResult remove(@PathVariable Long[] studentIds) + { + return toAjax(sysStudentService.removeByIds(Arrays.asList(studentIds))); + } +} \ No newline at end of file diff --git a/agri-system/src/main/java/com/agri/system/domain/SysStudent.java b/agri-system/src/main/java/com/agri/system/domain/SysStudent.java new file mode 100644 index 0000000..972ce54 --- /dev/null +++ b/agri-system/src/main/java/com/agri/system/domain/SysStudent.java @@ -0,0 +1,131 @@ +package com.agri.system.domain; + +import com.agri.common.annotation.Excel; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.annotation.JsonFormat; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +import java.io.Serializable; +import java.util.Date; + +/** + * 学生信息对象 sys_student + * + * @author ruoyi + */ +@TableName(value = "sys_student") +public class SysStudent implements Serializable +{ + @TableField(exist = false) + private static final long serialVersionUID = 1L; + + /** 编号 */ + @TableId(type = IdType.AUTO) + private Long studentId; + + /** 学生名称 */ + @Excel(name = "学生名称") + private String studentName; + + /** 年龄 */ + @Excel(name = "年龄") + private Integer studentAge; + + /** 爱好(0代码 1音乐 2电影) */ + @Excel(name = "爱好", readConverterExp = "0=代码,1=音乐,2=电影") + private String studentHobby; + + /** 性别(0男 1女 2未知) */ + @Excel(name = "性别", readConverterExp = "0=男,1=女,2=未知") + private String studentSex; + + /** 状态(0正常 1停用) */ + @Excel(name = "状态", readConverterExp = "0=正常,1=停用") + private String studentStatus; + + /** 生日 */ + @JsonFormat(pattern = "yyyy-MM-dd") + @Excel(name = "生日", width = 30, dateFormat = "yyyy-MM-dd") + private Date studentBirthday; + + public void setStudentId(Long studentId) + { + this.studentId = studentId; + } + + public Long getStudentId() + { + return studentId; + } + public void setStudentName(String studentName) + { + this.studentName = studentName; + } + + public String getStudentName() + { + return studentName; + } + public void setStudentAge(Integer studentAge) + { + this.studentAge = studentAge; + } + + public Integer getStudentAge() + { + return studentAge; + } + public void setStudentHobby(String studentHobby) + { + this.studentHobby = studentHobby; + } + + public String getStudentHobby() + { + return studentHobby; + } + public void setStudentSex(String studentSex) + { + this.studentSex = studentSex; + } + + public String getStudentSex() + { + return studentSex; + } + public void setStudentStatus(String studentStatus) + { + this.studentStatus = studentStatus; + } + + public String getStudentStatus() + { + return studentStatus; + } + public void setStudentBirthday(Date studentBirthday) + { + this.studentBirthday = studentBirthday; + } + + public Date getStudentBirthday() + { + return studentBirthday; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("studentId", getStudentId()) + .append("studentName", getStudentName()) + .append("studentAge", getStudentAge()) + .append("studentHobby", getStudentHobby()) + .append("studentSex", getStudentSex()) + .append("studentStatus", getStudentStatus()) + .append("studentBirthday", getStudentBirthday()) + .toString(); + } +} \ No newline at end of file diff --git a/agri-system/src/main/java/com/agri/system/mapper/SysStudentMapper.java b/agri-system/src/main/java/com/agri/system/mapper/SysStudentMapper.java new file mode 100644 index 0000000..f18269f --- /dev/null +++ b/agri-system/src/main/java/com/agri/system/mapper/SysStudentMapper.java @@ -0,0 +1,14 @@ +package com.agri.system.mapper; + +import com.agri.system.domain.SysStudent; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** + * 学生信息Mapper接口 + * + * @author ruoyi + */ +public interface SysStudentMapper extends BaseMapper +{ + +} \ No newline at end of file diff --git a/agri-system/src/main/java/com/agri/system/service/ISysStudentService.java b/agri-system/src/main/java/com/agri/system/service/ISysStudentService.java new file mode 100644 index 0000000..e8a3690 --- /dev/null +++ b/agri-system/src/main/java/com/agri/system/service/ISysStudentService.java @@ -0,0 +1,22 @@ +package com.agri.system.service; + +import com.agri.system.domain.SysStudent; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.List; + +/** + * 学生信息Service接口 + * + * @author ruoyi + */ +public interface ISysStudentService extends IService +{ + /** + * 查询学生信息列表 + * + * @param sysStudent 学生信息 + * @return 学生信息集合 + */ + public List queryList(SysStudent sysStudent); +} \ No newline at end of file diff --git a/agri-system/src/main/java/com/agri/system/service/impl/SysStudentServiceImpl.java b/agri-system/src/main/java/com/agri/system/service/impl/SysStudentServiceImpl.java new file mode 100644 index 0000000..3c0ab74 --- /dev/null +++ b/agri-system/src/main/java/com/agri/system/service/impl/SysStudentServiceImpl.java @@ -0,0 +1,43 @@ +package com.agri.system.service.impl; + +import com.agri.common.utils.StringUtils; +import com.agri.system.domain.SysStudent; +import com.agri.system.mapper.SysStudentMapper; +import com.agri.system.service.ISysStudentService; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 学生信息Service业务层处理 + * + * @author ruoyi + */ +@Service +public class SysStudentServiceImpl extends ServiceImpl implements ISysStudentService +{ + @Override + public List queryList(SysStudent sysStudent) + { + // 注意:mybatis-plus lambda 模式不支持 eclipse 的编译器 + // LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + // queryWrapper.eq(SysStudent::getStudentName, sysStudent.getStudentName()); + QueryWrapper queryWrapper = Wrappers.query(); + if (StringUtils.isNotEmpty(sysStudent.getStudentName())) + { + queryWrapper.eq("student_name", sysStudent.getStudentName()); + } + if (StringUtils.isNotNull(sysStudent.getStudentAge())) + { + queryWrapper.eq("student_age", sysStudent.getStudentAge()); + } + if (StringUtils.isNotEmpty(sysStudent.getStudentHobby())) + { + queryWrapper.eq("student_hobby", sysStudent.getStudentHobby()); + } + return this.list(queryWrapper); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4684661..7e02f36 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ 5.7.14 5.3.39 3.0.3 + 3.5.1 @@ -225,6 +226,12 @@ agri-api ${agri.version} + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} +