springboot+mybatis+freemarker+shiro多模块项目搭建笔记(二)--集成mybatis plus
2. 开始把mybatis plus放进去
前面项目建完了,父项目的pom也把springboot主要的包都引进来了,接下来的3%就开始慢慢爬坑吧
先找到lee_common_entity,这个模块里就是我们要放entity的地方啦,先把pom弄一下
里面先把lee_common包含进来
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>lizi</artifactId> <groupId>lee.lizi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>lee_common_entity</artifactId> <dependencies> <dependency> <groupId>lee.lizi</groupId> <artifactId>lee_common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
然后先放着,我们还要往下赶呢
lee_common_dao的pom里也先把pom弄一下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>lizi</artifactId> <groupId>lee.lizi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>lee_common_dao</artifactId> <dependencies> <dependency> <groupId>lee.lizi</groupId> <artifactId>lee_common_entity</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
同理lee_common_service的pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>lizi</artifactId> <groupId>lee.lizi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>lee_common_service</artifactId> <dependencies> <dependency> <groupId>lee.lizi</groupId> <artifactId>lee_common_dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
这几个其实就是每一个模块把上一个模块的包引进来,然后上一个模块的又把上一个的引进去了,所以·····就这样了,是不是很水·····
下面开始鼓捣admin这个模块,
lee_admin这个一开始是这样的:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>lizi</artifactId> <groupId>lee.lizi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>lee_admin</artifactId> <dependencies> <dependency> <groupId>lee.lizi</groupId> <artifactId>lee_common_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
然后新建一个AdminApplication类
代码敲进去:
package lee.admin; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication.class,args); } }
然后先run一把,看看效果
哎呦,竟然跑起来了,我的乖乖,默认端口8080,打开能看到一个黑白的页面,反正跑起来就行了。然后再在resources文件夹里建一个application.yml文件,改个端口先
server: port: 8888
然后再跑一遍试试
看样子是成功了,接着开始把mybatis plus放进去~
emm…考虑到好几个模块都需要用到mybatis plus的依赖,所以我把它放到父项目的pom里:(敲黑板-父项目)
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency>
这样得话不管是entity模块还是其他模块,都可以直接用mybatisplus的依赖了。
回到lee_admin这个子模块,先在pom里把数据库连接和代码生成什么的依赖加进去
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!-- mybatis plus 代码生成器依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.1.0</version> </dependency> <!-- 代码生成器模板 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency>
把代码生成器类放进去
MysqlGenerator.java类的内容如下:
package lee.generator; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableFill; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; public class MysqlGenerator { public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/lee_admin/src/main/java"); // TODO 设置用户名 gc.setAuthor("lee"); gc.setOpen(true); // service 命名方式 gc.setServiceName("%sService"); // service impl 命名方式 gc.setServiceImplName("%sServiceImpl"); // 自定义文件命名,注意 %s 会自动填充表实体属性! gc.setMapperName("%sMapper"); gc.setXmlName("%sMapper"); gc.setFileOverride(true); gc.setActiveRecord(true); // XML 二级缓存 gc.setEnableCache(false); // XML ResultMap gc.setBaseResultMap(true); // XML columList gc.setBaseColumnList(false); mpg.setGlobalConfig(gc); // TODO 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/ui-find?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // TODO 包配置 PackageConfig pc = new PackageConfig(); //pc.setModuleName(scanner("模块名")); pc.setParent("lee.admin"); pc.setEntity("entity"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setController("controller"); pc.setMapper("mapper"); mpg.setPackageInfo(pc); // 自定义需要填充的字段 List<TableFill> tableFillList = new ArrayList<>(); //如 每张表都有一个创建时间、修改时间 //而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改 //修改时,修改时间会修改, //虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了, //使用公共字段填充功能,就可以实现,自动按场景更新了。 //如下是配置 TableFill createField = new TableFill("create_time", FieldFill.INSERT); TableFill modifiedField = new TableFill("update_time", FieldFill.INSERT_UPDATE); tableFillList.add(createField); tableFillList.add(modifiedField); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return projectPath + "/lee_admin/src/main/resources/mapper" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); // 设置逻辑删除键 strategy.setLogicDeleteFieldName("isdel"); // TODO 指定生成的bean的数据库表名 strategy.setInclude("sys_user"); //strategy.setSuperEntityColumns("id"); // 驼峰转连字符 strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有! mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
里面的东西需要自己读一读根据需要自己改变,分模块下可以设置生成到不同的位置。
找一个表试一下,run一下,发现文件就自己生成完了。
这样都生成到lee_admin这个里面去了,可以趁机在单模块里测试一下好不好使;
接着就开始配置application文件了
server: port: 8888spring: datasource: # 配置数据源 driver-class-name: com.mysql.cj.jdbc.Driver # 使用druid连接池 type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/ui-find?characterEncoding=utf8&serverTimezone=UTC username: root password: root #mybatis-plus配置 mybatis-plus: global-config: db-config: id-type: auto field-strategy: not_empty table-underline: true db-type: mysql logic-delete-value: 1 # 逻辑已删除值(默认为 1) logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) mapper-locations: classpath:mapper/*.xml
配置AdminApplication.java
把生成的controller填一下,跑起来
package lee.admin.controller; import lee.admin.entity.SysUser; import lee.admin.service.SysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * <p> * 前端控制器 * </p> * * @author lee * @since 2020-03-12 */ @RestController @RequestMapping("/admin/user") public class SysUserController { @Autowired SysUserService userService; @RequestMapping() public List<SysUser> list(){ return userService.list(); } }
然后运行起来,访问http://localhost:8888/admin/user
发现页面有东西了
说明单模块版的没有问题了,配置后台打印sql日志
发现木有问题
坑1. 这里后期遇到一个问题,假删除不起作用,在search了一下以后直接加上了,不懂~直接上代码:
package lee.admin.config; import com.baomidou.mybatisplus.core.injector.ISqlInjector; import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration public class MyBatisPlusConfig { /** * 配置分页插件 * @return */ @Bean public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); } /** * sql执行效率插件 * @return */ @Bean @Profile({"test"}) public PerformanceInterceptor performanceInterceptor(){ return new PerformanceInterceptor(); } /** * 逻辑删除用 * @return */ @Bean public ISqlInjector iSqlInjector(){ return new LogicSqlInjector(); } }
加上以后配合实体类里面的@TableLogic和application.yml里面的配置就起作用了,删除的时候不是物理删除而是通过设置的逻辑删除标志位控制,而且查询的时候也会自动忽略删除数据。
坑2. 数据库存储的是时间格式,显示的时候没有转换,通过配置文件设置自动转一下
spring: jackson: date-format: yyyy-MM-dd hh:mm:ss time-zone: GMT+8
下一步开始按照子项目拆分模块
lee_common_entity:
lee_common_dao
lee_common_service
lee_admin:
移动完了以后,可以改一下那个自动生成的类,让它以后自动生成到不同的子模块中,如果移动完了发现编译报错,就把idea关了再打开一下,坎坎坷坷坑~~~
最后修改一下AdminApplication的包扫描位置,Run一把试试
发现问题不大,哎嘿嘿,以后可以放心的改admin了,再也不去想那些前后台和前后端的问题了(┬_┬)
在网上看的时候好像有人提到这点:
备忘一下,以防万一。
- 点赞
- 收藏
- 分享
- 文章举报
- springboot+mybatis+freemarker+shiro多模块项目搭建笔记(三)--集成freemarker
- springboot+mybatis+freemarker+shiro多模块项目搭建笔记(四)--热部署配置
- SpringBoot+Shiro+MyBatisPlus搭建前后端分离的多模块项目
- 整合SpringBoot + MybatisPlus 搭建JAVA多模块项目基本骨架
- SpringBoot+Mybatis+Mybatis Plus+Shiro实现一个简单的项目架构
- 搭建spring boot集成mybatis项目入门
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- 基于springboot+mybatisplus+shiro+dubbo+layuicms2.0搭建权限管理系统
- SpringBoot+Mybatis多模块项目搭建教程
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- IDEA上搭建maven项目整合springboot+mybatis+shiro
- 使用SpringBoot搭建小型项目,集成mybatis,redis,swagger2,并部署在外部容器中。
- Springboot整合dubbo构建maven多模块项目(四) - 集成mybatis
- springboot整合mybatis-plus用AutoGenerator搭建项目
- 基于Maven的Springboot项目搭建学习笔记
- IDEA搭建SpringBoot+mybatis+thymeleaf项目
- Spring+SpringMvc+Mybatis框架集成搭建教程一(项目创建)
- 基于maven使用IDEA搭建和部署SpringBoot多模块项目(Multi-Module)
- idea+springboot+Mybatis搭建web项目
- java 搭建基于springboot的ssm(spring + springmvc + mybatis)的maven项目