From 5771fed48a84fc0e3e731476b530e2ae5da6c3f7 Mon Sep 17 00:00:00 2001 From: williamjava Date: Mon, 7 Jan 2019 11:22:28 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=87=86=E5=A4=87=E4=BD=BF=E7=94=A8mybatis?= =?UTF-8?q?-plus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star/pom.xml | 25 ++- star/star-web/pom.xml | 12 +- .../java/com/gui/star/web/Application.java | 5 +- .../star/web/config/MybatisPlusConfig.java | 80 ++++++++ .../gui/star/web/config/MysqlGenerator.java | 181 +++++++++++++++++ .../src/main/resources/application.yml | 6 +- .../resources/template/controller.java.vm | 187 ++++++++++++++++++ .../main/resources/template/entity.java.vm | 164 +++++++++++++++ .../main/resources/template/mapper.java.vm | 38 ++++ .../src/main/resources/template/mapper.xml.vm | 63 ++++++ .../main/resources/template/service.java.vm | 33 ++++ .../resources/template/serviceImpl.java.vm | 37 ++++ 12 files changed, 825 insertions(+), 6 deletions(-) create mode 100644 star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java create mode 100644 star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java create mode 100644 star/star-web/src/main/resources/template/controller.java.vm create mode 100644 star/star-web/src/main/resources/template/entity.java.vm create mode 100644 star/star-web/src/main/resources/template/mapper.java.vm create mode 100644 star/star-web/src/main/resources/template/mapper.xml.vm create mode 100644 star/star-web/src/main/resources/template/service.java.vm create mode 100644 star/star-web/src/main/resources/template/serviceImpl.java.vm diff --git a/star/pom.xml b/star/pom.xml index 12a27dd..f0d980c 100644 --- a/star/pom.xml +++ b/star/pom.xml @@ -80,7 +80,7 @@ com.alibaba - druid + druid-spring-boot-starter 1.1.10 @@ -144,6 +144,29 @@ commons-lang3 3.0 + + + + com.baomidou + mybatisplus-spring-boot-starter + 1.0.5 + + + com.baomidou + mybatis-plus + 2.1.8 + + + org.springframework.boot + spring-boot-starter-jdbc + + + + + org.apache.velocity + velocity-engine-core + 2.0 + \ No newline at end of file diff --git a/star/star-web/pom.xml b/star/star-web/pom.xml index 4e04819..5f7307a 100644 --- a/star/star-web/pom.xml +++ b/star/star-web/pom.xml @@ -46,7 +46,7 @@ com.alibaba - druid + druid-spring-boot-starter @@ -62,6 +62,16 @@ io.springfox springfox-swagger-ui + + + com.baomidou + mybatisplus-spring-boot-starter + + + + com.baomidou + mybatis-plus + star-web diff --git a/star/star-web/src/main/java/com/gui/star/web/Application.java b/star/star-web/src/main/java/com/gui/star/web/Application.java index aaef832..29957d4 100644 --- a/star/star-web/src/main/java/com/gui/star/web/Application.java +++ b/star/star-web/src/main/java/com/gui/star/web/Application.java @@ -1,13 +1,12 @@ package com.gui.star.web; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; -import tk.mybatis.spring.annotation.MapperScan; - /** * SpringBoot启动类 * @@ -18,7 +17,7 @@ */ @SpringBootApplication @ComponentScan(basePackages = "com.gui") -@MapperScan(basePackages = "com.gui.star.dal.mapper") +@MapperScan(basePackages = "com.gui.star.dal.dao") @EnableAutoConfiguration @EnableScheduling public class Application { diff --git a/star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java b/star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java new file mode 100644 index 0000000..d2f25ac --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java @@ -0,0 +1,80 @@ +package com.gui.star.web.config; +import javax.sql.DataSource; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; +import com.baomidou.mybatisplus.plugins.PaginationInterceptor; +import com.baomidou.mybatisplus.plugins.PerformanceInterceptor; + +/** + * code is far away from bug with the animal protecting + * ┏┓   ┏┓ + * ┏┛┻━━━┛┻┓ + * ┃       ┃ + * ┃   ━   ┃ + * ┃ ┳┛ ┗┳ ┃ + * ┃       ┃ + * ┃   ┻   ┃ + * ┃       ┃ + * ┗━┓   ┏━┛ + *   ┃   ┃神兽保佑 + *   ┃   ┃代码无BUG! + *   ┃   ┗━━━┓ + *   ┃       ┣┓ + *   ┃       ┏┛ + *   ┗┓┓┏━┳┓┏┛ + *    ┃┫┫ ┃┫┫ + *    ┗┻┛ ┗┻┛ + * + * @Description : MybatisPlus配置 + * --------------------------------- + * @Author : Liang.Guangqing + * @Date : Create in 2017/9/19 13:54 + */ +@Configuration +public class MybatisPlusConfig { + + /*** + * plus 的性能优化 + * @return + */ + @Bean + public PerformanceInterceptor performanceInterceptor() { + PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); + /**/ + performanceInterceptor.setMaxTime(1000); + /**/ + performanceInterceptor.setFormat(true); + return performanceInterceptor; + } + + /** + * @Description : mybatis-plus分页插件 + * --------------------------------- + * @Author : Liang.Guangqing + * @Date : Create in 2017/9/19 13:59 + */ + @Bean + public PaginationInterceptor paginationInterceptor() { + return new PaginationInterceptor(); + } + + + /** + * @Description : druid注入 + * --------------------------------- + * @Author : Liang.Guangqing + * @Date : Create in 2018/1/3 17:38 + */ + @Bean + @ConfigurationProperties("spring.datasource.druid." ) + public DataSource dataSource() { + return DruidDataSourceBuilder + .create() + .build(); + } + +} \ No newline at end of file diff --git a/star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java b/star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java new file mode 100644 index 0000000..f3dc258 --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java @@ -0,0 +1,181 @@ +package com.gui.star.web.config; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.baomidou.mybatisplus.enums.FieldFill; +import com.baomidou.mybatisplus.generator.AutoGenerator; +import com.baomidou.mybatisplus.generator.InjectionConfig; +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; +import com.baomidou.mybatisplus.generator.config.FileOutConfig; +import com.baomidou.mybatisplus.generator.config.GlobalConfig; +import com.baomidou.mybatisplus.generator.config.PackageConfig; +import com.baomidou.mybatisplus.generator.config.StrategyConfig; +import com.baomidou.mybatisplus.generator.config.TemplateConfig; +import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert; +import com.baomidou.mybatisplus.generator.config.po.TableFill; +import com.baomidou.mybatisplus.generator.config.po.TableInfo; +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; +import com.baomidou.mybatisplus.generator.config.rules.DbType; +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; + +/** + *code is far away from bug with the animal protecting + * ┏┓   ┏┓ + *┏┛┻━━━┛┻┓ + *┃       ┃   + *┃   ━   ┃ + *┃ ┳┛ ┗┳ ┃ + *┃       ┃ + *┃   ┻   ┃ + *┃       ┃ + *┗━┓   ┏━┛ + *  ┃   ┃神兽保佑 + *  ┃   ┃代码无BUG! + *  ┃   ┗━━━┓ + *  ┃       ┣┓ + *  ┃       ┏┛ + *  ┗┓┓┏━┳┓┏┛ + *   ┃┫┫ ┃┫┫ + *   ┗┻┛ ┗┻┛ + *   + * @Description : MybatisPlus代码生成器 + * --------------------------------- + * @Author : Liang.Guangqing + * @Date : Create in 2017/9/19 14:48  + */ +public class MysqlGenerator { + private static String packageName="web"; //文件路径 + private static String authorName="WuHoujian"; //作者 + private static String table="user"; //table名字 + private static String prefix=""; //table前缀 + private static File file = new File(packageName); + private static String path = file.getAbsolutePath(); + + public static void main(String[] args) { + // 自定义需要填充的字段 + List tableFillList = new ArrayList<>(); + tableFillList.add(new TableFill("ASDD_SS", FieldFill.INSERT_UPDATE)); + // 代码生成器 + AutoGenerator mpg = new AutoGenerator().setGlobalConfig( + // 全局配置 + new GlobalConfig() + .setOutputDir(path+"/src/main/java")//输出目录 + .setFileOverride(true)// 是否覆盖文件 + .setActiveRecord(true)// 开启 activeRecord 模式 + .setEnableCache(false)// XML 二级缓存 + .setBaseResultMap(true)// XML ResultMap + .setBaseColumnList(true)// XML columList + .setOpen(false)//生成后打开文件夹 + .setAuthor(authorName) + // 自定义文件命名,注意 %s 会自动填充表实体属性! + .setMapperName("%sMapper") + .setXmlName("%sMapper") + .setServiceName("%sService") + .setServiceImplName("%sServiceImpl") + .setControllerName("%sController") + ).setDataSource( + // 数据源配置 + new DataSourceConfig() + .setDbType(DbType.MYSQL)// 数据库类型 + .setTypeConvert(new MySqlTypeConvert() { + // 自定义数据库表字段类型转换【可选】 + @Override + public DbColumnType processTypeConvert(String fieldType) { + System.out.println("转换类型:" + fieldType); + // if ( fieldType.toLowerCase().contains( "tinyint" ) ) { + // return DbColumnType.BOOLEAN; + // } + return super.processTypeConvert(fieldType); + } + }) + .setDriverName("com.mysql.jdbc.Driver") + .setUsername("root") + .setPassword("root") + .setUrl("jdbc:mysql://127.0.0.1:3306/star?characterEncoding=utf8") + ).setStrategy( + // 策略配置 + new StrategyConfig() + // .setCapitalMode(true)// 全局大写命名 + //.setDbColumnUnderline(true)//全局下划线命名 + .setTablePrefix(new String[]{prefix})// 此处可以修改为您的表前缀 + .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略 + .setInclude(new String[] { table }) // 需要生成的表 + .setRestControllerStyle(true) + //.setExclude(new String[]{"test"}) // 排除生成的表 + // 自定义实体父类 + // .setSuperEntityClass("com.baomidou.demo.TestEntity") + // 自定义实体,公共字段 + //.setSuperEntityColumns(new String[]{"test_id"}) + .setTableFillList(tableFillList) + // 自定义 mapper 父类 + // .setSuperMapperClass("com.baomidou.demo.TestMapper") + // 自定义 service 父类 + // .setSuperServiceClass("com.baomidou.demo.TestService") + // 自定义 service 实现类父类 + // .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl") + // 自定义 controller 父类 + .setSuperControllerClass("com.gui.star."+packageName+".controller.AbstractController") + // 【实体】是否生成字段常量(默认 false) + // public static final String ID = "test_id"; + // .setEntityColumnConstant(true) + // 【实体】是否为构建者模型(默认 false) + // public User setName(String name) {this.name = name; return this;} + // .setEntityBuilderModel(true) + // 【实体】是否为lombok模型(默认 false)document + // .setEntityLombokModel(true) + // Boolean类型字段是否移除is前缀处理 + // .setEntityBooleanColumnRemoveIsPrefix(true) + // .setRestControllerStyle(true) + // .setControllerMappingHyphenStyle(true) + ).setPackageInfo( + // 包配置 + new PackageConfig() + //.setModuleName("User") + .setParent("com.gui.star."+packageName)// 自定义包路径 + .setController("controller")// 这里是控制器包名,默认 web + .setEntity("entity") + .setMapper("dao") + .setService("service") + .setServiceImpl("service.impl") + //.setXml("mapper") + ).setCfg( + // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 + new InjectionConfig() { + @Override + public void initMap() { + Map map = new HashMap<>(); + map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); + this.setMap(map); + } + }.setFileOutConfigList(Collections.singletonList(new FileOutConfig("/templates/mapper.xml.vm") { + // 自定义输出文件目录 + @Override + public String outputFile(TableInfo tableInfo) { + return path+"/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml"; + } + })) + ).setTemplate( + // 关闭默认 xml 生成,调整生成 至 根目录 + new TemplateConfig().setXml(null) + // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy + // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置: + // .setController("..."); + // .setEntity("..."); + // .setMapper("..."); + // .setXml("..."); + // .setService("..."); + // .setServiceImpl("..."); + ); + + // 执行生成 + mpg.execute(); + + // 打印注入设置,这里演示模板里面怎么获取注入内容【可无】 + System.err.println(mpg.getCfg().getMap().get("abc")); + } +} \ No newline at end of file diff --git a/star/star-web/src/main/resources/application.yml b/star/star-web/src/main/resources/application.yml index 2390eef..39ce2a9 100644 --- a/star/star-web/src/main/resources/application.yml +++ b/star/star-web/src/main/resources/application.yml @@ -11,4 +11,8 @@ spring: mybatis: mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 - type-aliases-package: com.gui.star.dal.model # 注意:对应实体类的路径 \ No newline at end of file + type-aliases-package: com.gui.star.dal.model # 注意:对应实体类的路径 + +mybatis-plus: + mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 + typeAliasesPackage: com.gui.star.dal.model # 注意:对应实体类的路径 \ No newline at end of file diff --git a/star/star-web/src/main/resources/template/controller.java.vm b/star/star-web/src/main/resources/template/controller.java.vm new file mode 100644 index 0000000..d070747 --- /dev/null +++ b/star/star-web/src/main/resources/template/controller.java.vm @@ -0,0 +1,187 @@ +package ${package.Controller}; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +#if(${restControllerStyle}) +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +#else +import org.springframework.stereotype.Controller; +#end +#if(${superControllerClassPackage}) +import ${superControllerClassPackage}; +#end +import org.springframework.beans.factory.annotation.Autowired; +import com.baomidou.mybatisplus.mapper.EntityWrapper; +import com.baomidou.mybatisplus.plugins.Page; +import ${package.Service}.${table.serviceName}; +import com.lgq.boot.util.TableJSON; +import com.lgq.boot.util.JSONResult; +import ${package.Entity}.${entity}; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import io.swagger.annotations.*; +import java.util.Date; + +/** + *code is far away from bug with the animal protecting + * ┏┓   ┏┓ + *┏┛┻━━━┛┻┓ + *┃       ┃   + *┃   ━   ┃ + *┃ ┳┛ ┗┳ ┃ + *┃       ┃ + *┃   ┻   ┃ + *┃       ┃ + *┗━┓   ┏━┛ + *  ┃   ┃神兽保佑 + *  ┃   ┃代码无BUG! + *  ┃   ┗━━━┓ + *  ┃       ┣┓ + *  ┃       ┏┛ + *  ┗┓┓┏━┳┓┏┛ + *   ┃┫┫ ┃┫┫ + *   ┗┻┛ ┗┻┛ + *   + * @description : ${entity} 控制器 + * --------------------------------- + * @author ${author} + * @since ${date} + */ +#if(${restControllerStyle}) +@RestController +#else +@Controller +#end +@Api(value="#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end", description="${entity} 控制器") +@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end") +#if(${superControllerClass}) +public class ${table.controllerName} extends ${superControllerClass} { +#else +public class ${table.controllerName} { +#end + private final Logger logger = LoggerFactory.getLogger(${table.controllerName}.class); + + @Autowired + public ${table.serviceName} ${table.entityPath}Service; + + /** + * @description : 获取分页列表 + * --------------------------------- + * @author : ${author} + * @since : Create in ${date} + */ + @GetMapping("/get${entity}List") + @ApiOperation(value="/get${entity}List", notes="获取分页列表") + public TableJSON<${entity}> get${entity}List(@ApiParam(name="${entity}",value="${entity} 实体类")${entity} param , + @ApiParam(name="length",value="页大小",required=true,defaultValue = "10")Integer length, + @ApiParam(name="pageNo",value="页数",required=true,defaultValue = "1")Integer pageNo) { + TableJSON<${entity}> resJson=new TableJSON<>(); + if(param.getBeginDate() == null) { + if (param.getEndDate() == null) { + param.setBeginDate(getMonthFirstDay(new Date())); + } else { param.setBeginDate(getMonthFirstDay(param.getEndDate())); } + } + if(param.getEndDate() == null) { + param.setEndDate(getMonthLastDay(new Date())); + } + try { + Page<${entity}> page=new Page<${entity}>(pageNo,length); + ${table.entityPath}Service.selectPageWithParam(page, param); + resJson.setRecordsTotal(page.getTotal()); + resJson.setData(page.getRecords()); + resJson.setSuccess(true); + }catch (Exception e){ + resJson.setSuccess(false); + resJson.setMessage("异常信息:"+e.getClass().getName()); + logger.info("异常信息:{}",e.getMessage()); + } + return resJson; + } + + /** + * @description : 通过id获取${entity} + * --------------------------------- + * @author : ${author} + * @since : Create in ${date} + */ + @GetMapping("/get${entity}ById") + @ApiOperation(value="/get${entity}ById", notes="通过id获取${entity}") + public JSONResult<${entity}> get${entity}ById(@ApiParam(name="id",value="${entity}ID",required=true)Long id) { + JSONResult<${entity}> resJson = new JSONResult<>(); + try { + ${entity} param= ${table.entityPath}Service.selectOneByObj(id); + resJson.setData(param); + resJson.setSuccess(true); + }catch (Exception e) { + resJson.setSuccess(false); + resJson.setMessage("异常信息:"+e.getClass().getName()); + logger.info("异常信息:{}",e.getMessage()); + } + return resJson; + } + + /** + * @description : 通过id删除${entity} + * --------------------------------- + * @author : ${author} + * @since : Create in ${date} + */ + @DeleteMapping("/delete${entity}ById") + @ApiOperation(value="/delete${entity}ById", notes="通过id删除${entity}") + public JSONResult<${entity}> delete${entity}ById(@ApiParam(name="id",value="${entity}ID",required=true)Long id) { + JSONResult<${entity}> resJson = new JSONResult<>(); + try{ + resJson.setSuccess(${table.entityPath}Service.deleteById(id)); + }catch (Exception e) { + resJson.setSuccess(false); + resJson.setMessage("异常信息:"+e.getClass().getName()); + logger.info("异常信息:{}",e.getMessage()); + } + return resJson; + } + + /** + * @description : 通过id更新${entity} + * --------------------------------- + * @author : ${author} + * @since : Create in ${date} + */ + @PutMapping("/update${entity}ById") + @ApiOperation(value="/update${entity}ById", notes="通过id更新${entity}") + public JSONResult<${entity}> update${entity}ById(@ApiParam(name="${entity}",value="${entity} 实体类")${entity} param) { + JSONResult<${entity}> resJson = new JSONResult<>(); + try{ + resJson.setSuccess(${table.entityPath}Service.updateById(param)); + }catch (Exception e) { + resJson.setSuccess(false); + resJson.setMessage("异常信息:"+e.getClass().getName()); + logger.info("异常信息:{}",e.getMessage()); + } + return resJson; + } + + /** + * @description : 添加${entity} + * --------------------------------- + * @author : ${author} + * @since : Create in ${date} + */ + @PutMapping("/add${entity}") + @ApiOperation(value="/add${entity}", notes="添加${entity}") + public JSONResult<${entity}> add${entity}(@ApiParam(name="${entity}",value="${entity} 实体类")${entity} param) { + JSONResult<${entity}> resJson = new JSONResult<>(); + try{ + resJson.setSuccess(${table.entityPath}Service.insert(param)); + }catch (Exception e) { + resJson.setSuccess(false); + resJson.setMessage("异常信息:"+e.getClass().getName()); + logger.info("异常信息:{}",e.getMessage()); + } + return resJson; + } +} diff --git a/star/star-web/src/main/resources/template/entity.java.vm b/star/star-web/src/main/resources/template/entity.java.vm new file mode 100644 index 0000000..e015fdc --- /dev/null +++ b/star/star-web/src/main/resources/template/entity.java.vm @@ -0,0 +1,164 @@ +package ${package.Entity}; + +#if(${activeRecord}) +import java.io.Serializable; + +#end +#foreach($pkg in ${table.importPackages}) +import ${pkg}; +#end +#if(${entityLombokModel}) + +import com.baomidou.mybatisplus.annotations.Version; + +import lombok.Data; +import lombok.experimental.Accessors; +#end +import com.fasterxml.jackson.annotation.JsonFormat; +import org.springframework.format.annotation.DateTimeFormat; +import io.swagger.annotations.ApiModelProperty; + +/** + *code is far away from bug with the animal protecting + * ┏┓   ┏┓ + *┏┛┻━━━┛┻┓ + *┃       ┃   + *┃   ━   ┃ + *┃ ┳┛ ┗┳ ┃ + *┃       ┃ + *┃   ┻   ┃ + *┃       ┃ + *┗━┓   ┏━┛ + *  ┃   ┃神兽保佑 + *  ┃   ┃代码无BUG! + *  ┃   ┗━━━┓ + *  ┃       ┣┓ + *  ┃       ┏┛ + *  ┗┓┓┏━┳┓┏┛ + *   ┃┫┫ ┃┫┫ + *   ┗┻┛ ┗┻┛ + *   + * @description : ${entity} 实体类 + * --------------------------------- + * @author ${author} + * @since ${date} + */ +#if(${entityLombokModel}) +@Data +@Accessors(chain = true) +#end +#if(${table.convert}) +@TableName("${table.name}") +#end +#if(${superEntityClass}) +public class ${entity} extends ${superEntityClass}#*#if(${activeRecord})<${entity}>#end*# { +#elseif(${activeRecord}) +public class ${entity} extends Model<${entity}> { +#else +public class ${entity} implements Serializable { +#end + + private static final long serialVersionUID = 1L; + +## ---------- BEGIN 字段循环遍历 ---------- +#foreach($field in ${table.fields}) +#if(${field.keyFlag}) +#set($keyPropertyName=${field.propertyName}) +#end +#if("$!field.comment" != "") + /** + * ${field.comment} + */ + @ApiModelProperty("${field.comment}") +#end +#if(${field.keyFlag}) +## 主键 +#if(${field.keyIdentityFlag}) + @TableId(value="${field.name}", type= IdType.AUTO) +#elseif(${field.convert}) + @TableId("${field.name}") +#end +## 普通字段 +#elseif(${field.fill}) +## ----- 存在字段填充设置 ----- +#if(${field.convert}) + @TableField(value = "${field.name}", fill = FieldFill.${field.fill}) +#else + @TableField(fill = FieldFill.${field.fill}) +#end +#elseif(${field.convert}) + @TableField("${field.name}") +#end +## 乐观锁注解 +#if(${versionFieldName}==${field.name}) + @Version +#end +## 逻辑删除注解 +#if(${logicDeleteFieldName}==${field.name}) + @TableLogic +#end +#if(${field.propertyType}=="Date") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") +#end + private ${field.propertyType} ${field.propertyName}; +#end +## ---------- END 字段循环遍历 ---------- + +#if(!${entityLombokModel}) +#foreach($field in ${table.fields}) +#if(${field.propertyType.equals("boolean")}) +#set($getprefix="is") +#else +#set($getprefix="get") +#end + + public ${field.propertyType} ${getprefix}${field.capitalName}() { + return ${field.propertyName}; + } + +#if(${entityBuilderModel}) + public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { +#else + public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { +#end + this.${field.propertyName} = ${field.propertyName}; +#if(${entityBuilderModel}) + return this; +#end + } +#end +#end + +#if(${entityColumnConstant}) +#foreach($field in ${table.fields}) + public static final String ${field.name.toUpperCase()} = "${field.name}"; + +#end +#end +#*#if(${activeRecord}) + @Override + protected Serializable pkVal() { +#if(${keyPropertyName}) + return this.${keyPropertyName}; +#else + return this.id; +#end + } + +#end*# +#if(!${entityLombokModel}) + @Override + public String toString() { + return "${entity}{" + +#foreach($field in ${table.fields}) +#if($!{velocityCount}==1) + "${field.propertyName}=" + ${field.propertyName} + +#else + ", ${field.propertyName}=" + ${field.propertyName} + +#end +#end + "}"; + } +#end +} \ No newline at end of file diff --git a/star/star-web/src/main/resources/template/mapper.java.vm b/star/star-web/src/main/resources/template/mapper.java.vm new file mode 100644 index 0000000..5fe0886 --- /dev/null +++ b/star/star-web/src/main/resources/template/mapper.java.vm @@ -0,0 +1,38 @@ +package ${package.Mapper}; + +import ${package.Entity}.${entity}; +import ${superMapperClassPackage}; +import org.springframework.stereotype.Repository; +import com.baomidou.mybatisplus.plugins.Page; + +import java.util.List; + +/** + *code is far away from bug with the animal protecting + * ┏┓   ┏┓ + *┏┛┻━━━┛┻┓ + *┃       ┃   + *┃   ━   ┃ + *┃ ┳┛ ┗┳ ┃ + *┃       ┃ + *┃   ┻   ┃ + *┃       ┃ + *┗━┓   ┏━┛ + *  ┃   ┃神兽保佑 + *  ┃   ┃代码无BUG! + *  ┃   ┗━━━┓ + *  ┃       ┣┓ + *  ┃       ┏┛ + *  ┗┓┓┏━┳┓┏┛ + *   ┃┫┫ ┃┫┫ + *   ┗┻┛ ┗┻┛ + *   + * @description : ${entity} Mapper 接口 + * --------------------------------- + * @author ${author} + * @since ${date} + */ +@Repository +public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { + +} \ No newline at end of file diff --git a/star/star-web/src/main/resources/template/mapper.xml.vm b/star/star-web/src/main/resources/template/mapper.xml.vm new file mode 100644 index 0000000..10d2df6 --- /dev/null +++ b/star/star-web/src/main/resources/template/mapper.xml.vm @@ -0,0 +1,63 @@ + + + + +#if(${enableCache}) + + + +#end +#if(${baseResultMap}) + + +#foreach($field in ${table.fields}) +#if(${field.keyFlag})##生成主键排在第一位 + +#end +#end +#foreach($field in ${table.commonFields})##生成公共字段 + +#end +#foreach($field in ${table.fields}) +#if(!${field.keyFlag})##生成普通字段 + +#end +#end + + +#end +#if(${baseColumnList}) + + +#foreach($field in ${table.commonFields}) + #if(${field.name} == ${field.propertyName})${field.name}#else${field.name} AS ${field.propertyName}#end, +#end + ${table.fieldNames} + + +#end + + + + \ No newline at end of file diff --git a/star/star-web/src/main/resources/template/service.java.vm b/star/star-web/src/main/resources/template/service.java.vm new file mode 100644 index 0000000..46670a8 --- /dev/null +++ b/star/star-web/src/main/resources/template/service.java.vm @@ -0,0 +1,33 @@ +package ${package.Service}; + +import ${package.Entity}.${entity}; +import ${superServiceClassPackage}; + +/** + *code is far away from bug with the animal protecting + * ┏┓   ┏┓ + *┏┛┻━━━┛┻┓ + *┃       ┃   + *┃   ━   ┃ + *┃ ┳┛ ┗┳ ┃ + *┃       ┃ + *┃   ┻   ┃ + *┃       ┃ + *┗━┓   ┏━┛ + *  ┃   ┃神兽保佑 + *  ┃   ┃代码无BUG! + *  ┃   ┗━━━┓ + *  ┃       ┣┓ + *  ┃       ┏┛ + *  ┗┓┓┏━┳┓┏┛ + *   ┃┫┫ ┃┫┫ + *   ┗┻┛ ┗┻┛ + *   + * @description : ${entity} 服务接口 + * --------------------------------- + * @author ${author} + * @since ${date} + */ +public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { + +} \ No newline at end of file diff --git a/star/star-web/src/main/resources/template/serviceImpl.java.vm b/star/star-web/src/main/resources/template/serviceImpl.java.vm new file mode 100644 index 0000000..e04848e --- /dev/null +++ b/star/star-web/src/main/resources/template/serviceImpl.java.vm @@ -0,0 +1,37 @@ +package ${package.ServiceImpl}; + +import ${package.Entity}.${entity}; +import ${package.Mapper}.${table.mapperName}; +import ${package.Service}.${table.serviceName}; +import ${superServiceImplClassPackage}; +import org.springframework.stereotype.Service; + +/** + *code is far away from bug with the animal protecting + * ┏┓   ┏┓ + *┏┛┻━━━┛┻┓ + *┃       ┃   + *┃   ━   ┃ + *┃ ┳┛ ┗┳ ┃ + *┃       ┃ + *┃   ┻   ┃ + *┃       ┃ + *┗━┓   ┏━┛ + *  ┃   ┃神兽保佑 + *  ┃   ┃代码无BUG! + *  ┃   ┗━━━┓ + *  ┃       ┣┓ + *  ┃       ┏┛ + *  ┗┓┓┏━┳┓┏┛ + *   ┃┫┫ ┃┫┫ + *   ┗┻┛ ┗┻┛ + *   + * @description : ${entity} 服务实现类 + * --------------------------------- + * @author ${author} + * @since ${date} + */ +@Service +public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { + +} \ No newline at end of file From 37707b5014eb611a77028244551961c7aebb55c2 Mon Sep 17 00:00:00 2001 From: wuhoujian Date: Tue, 16 Apr 2019 18:14:11 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star/pom.xml | 13 +- .../com/gui/star/biz/common/BaseService.java | 16 -- .../gui/star/biz/common/BaseServiceImpl.java | 35 ---- .../listener/ContainerEventListener.java | 2 +- .../biz/service/impl/UserServiceImpl.java | 30 +-- star/star-dal/pom.xml | 10 +- .../com/gui/star/dal/common/CommonMapper.java | 7 - .../com/gui/star/dal/mapper/MenuMapper.java | 4 +- .../gui/star/dal/mapper/MenuRoleMapper.java | 4 +- .../gui/star/dal/mapper/PermissionMapper.java | 4 +- .../star/dal/mapper/PermissionRoleMapper.java | 4 +- .../com/gui/star/dal/mapper/RoleMapper.java | 4 +- .../com/gui/star/dal/mapper/UserMapper.java | 4 +- .../gui/star/dal/mapper/UserRoleMapper.java | 4 +- .../java/com/gui/star/dal/model/Menu.java | 3 + .../java/com/gui/star/dal/model/MenuRole.java | 56 +----- .../com/gui/star/dal/model/Permission.java | 56 +----- .../gui/star/dal/model/PermissionRole.java | 56 +----- .../java/com/gui/star/dal/model/Role.java | 48 +---- .../java/com/gui/star/dal/model/User.java | 78 +------- .../java/com/gui/star/dal/model/UserRole.java | 56 +----- star/star-web/pom.xml | 10 - .../java/com/gui/star/web/Application.java | 4 +- .../star/web/config/MybatisPlusConfig.java | 80 -------- .../gui/star/web/config/MysqlGenerator.java | 181 ------------------ .../src/main/resources/application.yml | 10 +- 26 files changed, 86 insertions(+), 693 deletions(-) delete mode 100644 star/star-biz/src/main/java/com/gui/star/biz/common/BaseService.java delete mode 100644 star/star-biz/src/main/java/com/gui/star/biz/common/BaseServiceImpl.java delete mode 100644 star/star-dal/src/main/java/com/gui/star/dal/common/CommonMapper.java delete mode 100644 star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java delete mode 100644 star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java diff --git a/star/pom.xml b/star/pom.xml index f0d980c..74be9b1 100644 --- a/star/pom.xml +++ b/star/pom.xml @@ -148,17 +148,8 @@ com.baomidou - mybatisplus-spring-boot-starter - 1.0.5 - - - com.baomidou - mybatis-plus - 2.1.8 - - - org.springframework.boot - spring-boot-starter-jdbc + mybatis-plus-boot-starter + 2.2.0 diff --git a/star/star-biz/src/main/java/com/gui/star/biz/common/BaseService.java b/star/star-biz/src/main/java/com/gui/star/biz/common/BaseService.java deleted file mode 100644 index b006c9b..0000000 --- a/star/star-biz/src/main/java/com/gui/star/biz/common/BaseService.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.gui.star.biz.common; - -import java.util.List; - -public interface BaseService { - - List findAll(); - - T findById(Long id); - - int save(T t); - - int update(T t); - - int remove(Long id); -} diff --git a/star/star-biz/src/main/java/com/gui/star/biz/common/BaseServiceImpl.java b/star/star-biz/src/main/java/com/gui/star/biz/common/BaseServiceImpl.java deleted file mode 100644 index a906cdc..0000000 --- a/star/star-biz/src/main/java/com/gui/star/biz/common/BaseServiceImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.gui.star.biz.common; - -import java.util.List; - -import com.gui.star.dal.common.CommonMapper; - -public abstract class BaseServiceImpl implements BaseService { - - public abstract CommonMapper getMapper(); - - @Override - public List findAll() { - return getMapper().selectAll(); - } - - @Override - public T findById(Long id) { - return getMapper().selectByPrimaryKey(id); - } - - @Override - public int save(T t) { - return getMapper().insert(t); - } - - @Override - public int remove(Long id) { - return getMapper().deleteByPrimaryKey(id); - } - - @Override - public int update(T t) { - return getMapper().updateByPrimaryKey(t); - } -} diff --git a/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java b/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java index 1ce4230..3fb011e 100644 --- a/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java +++ b/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java @@ -37,7 +37,7 @@ public class ContainerEventListener { * 3.容器启动完成和我们定义的Application启动类极其包含的main方法完全是两个概念。前者的时间发生在前面,Application.main()的作用在于启动容器里面的应用程序(比如我们的saturn) * * - * @param ContextRefreshedEvent + * @param event */ @Async @EventListener diff --git a/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java b/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java index edc4965..7f91bdc 100644 --- a/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java +++ b/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java @@ -1,30 +1,23 @@ package com.gui.star.biz.service.impl; -import java.util.ArrayList; -import java.util.List; - -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.gui.star.biz.common.BaseServiceImpl; +import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.gui.star.biz.service.UserService; import com.gui.star.biz.vo.UserVo; import com.gui.star.common.form.UserForm; import com.gui.star.common.util.DomainUtil; import com.gui.star.common.util.ObjectUtil; -import com.gui.star.dal.common.CommonMapper; import com.gui.star.dal.mapper.UserMapper; import com.gui.star.dal.model.User; - import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; @Service @Slf4j -public class UserServiceImpl extends BaseServiceImpl implements UserService { - @Autowired - private UserMapper userMapper; - +public class UserServiceImpl extends ServiceImpl implements UserService { /** * 查询用户信息列表 */ @@ -32,7 +25,7 @@ public class UserServiceImpl extends BaseServiceImpl implements UserServic public List queryList() { List userVoList = new ArrayList(); - List userList = userMapper.selectByExample(null); + List userList = this.selectList(null); for (User user : userList) { UserVo userVo = new UserVo(); BeanUtils.copyProperties(user, userVo); @@ -41,11 +34,6 @@ public List queryList() { return userVoList; } - @Override - public CommonMapper getMapper() { - return this.userMapper; - } - /** * 保存用户信息 */ @@ -53,7 +41,7 @@ public CommonMapper getMapper() { public void save(UserForm form) { User user = ObjectUtil.source2Target(form, User.class); DomainUtil.setCommonValueForCreate(user); - userMapper.insertSelective(user); + this.insert(user); log.info("用户ID为:{}", user.getId()); } diff --git a/star/star-dal/pom.xml b/star/star-dal/pom.xml index b00ac7b..ca5d30d 100644 --- a/star/star-dal/pom.xml +++ b/star/star-dal/pom.xml @@ -22,14 +22,8 @@ - org.mybatis.spring.boot - mybatis-spring-boot-starter - - - - - tk.mybatis - mapper-spring-boot-starter + com.baomidou + mybatis-plus-boot-starter diff --git a/star/star-dal/src/main/java/com/gui/star/dal/common/CommonMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/common/CommonMapper.java deleted file mode 100644 index a506399..0000000 --- a/star/star-dal/src/main/java/com/gui/star/dal/common/CommonMapper.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.gui.star.dal.common; - -import tk.mybatis.mapper.common.Mapper; - -public interface CommonMapper extends Mapper { - -} diff --git a/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuMapper.java index 955cd81..7a96beb 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuMapper.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuMapper.java @@ -1,8 +1,8 @@ package com.gui.star.dal.mapper; -import com.gui.star.dal.common.CommonMapper; +import com.baomidou.mybatisplus.mapper.BaseMapper; import com.gui.star.dal.model.Menu; -public interface MenuMapper extends CommonMapper{ +public interface MenuMapper extends BaseMapper { } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuRoleMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuRoleMapper.java index bf5e26d..3ad0bbd 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuRoleMapper.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/mapper/MenuRoleMapper.java @@ -1,8 +1,8 @@ package com.gui.star.dal.mapper; -import com.gui.star.dal.common.CommonMapper; +import com.baomidou.mybatisplus.mapper.BaseMapper; import com.gui.star.dal.model.MenuRole; -public interface MenuRoleMapper extends CommonMapper{ +public interface MenuRoleMapper extends BaseMapper { } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionMapper.java index 1ca9775..dfde81d 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionMapper.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionMapper.java @@ -1,8 +1,8 @@ package com.gui.star.dal.mapper; -import com.gui.star.dal.common.CommonMapper; +import com.baomidou.mybatisplus.mapper.BaseMapper; import com.gui.star.dal.model.Permission; -public interface PermissionMapper extends CommonMapper{ +public interface PermissionMapper extends BaseMapper { } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionRoleMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionRoleMapper.java index db4c4cc..47c6a9c 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionRoleMapper.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/mapper/PermissionRoleMapper.java @@ -1,8 +1,8 @@ package com.gui.star.dal.mapper; -import com.gui.star.dal.common.CommonMapper; +import com.baomidou.mybatisplus.mapper.BaseMapper; import com.gui.star.dal.model.PermissionRole; -public interface PermissionRoleMapper extends CommonMapper{ +public interface PermissionRoleMapper extends BaseMapper { } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/mapper/RoleMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/mapper/RoleMapper.java index c24e061..b447cf1 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/mapper/RoleMapper.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/mapper/RoleMapper.java @@ -1,8 +1,8 @@ package com.gui.star.dal.mapper; -import com.gui.star.dal.common.CommonMapper; +import com.baomidou.mybatisplus.mapper.BaseMapper; import com.gui.star.dal.model.Role; -public interface RoleMapper extends CommonMapper{ +public interface RoleMapper extends BaseMapper { } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserMapper.java index 55b6b59..29c324b 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserMapper.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserMapper.java @@ -1,8 +1,8 @@ package com.gui.star.dal.mapper; -import com.gui.star.dal.common.CommonMapper; +import com.baomidou.mybatisplus.mapper.BaseMapper; import com.gui.star.dal.model.User; -public interface UserMapper extends CommonMapper{ +public interface UserMapper extends BaseMapper { } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserRoleMapper.java b/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserRoleMapper.java index 504e9b6..7856415 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserRoleMapper.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/mapper/UserRoleMapper.java @@ -1,8 +1,8 @@ package com.gui.star.dal.mapper; -import com.gui.star.dal.common.CommonMapper; +import com.baomidou.mybatisplus.mapper.BaseMapper; import com.gui.star.dal.model.UserRole; -public interface UserRoleMapper extends CommonMapper{ +public interface UserRoleMapper extends BaseMapper { } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/model/Menu.java b/star/star-dal/src/main/java/com/gui/star/dal/model/Menu.java index 5ae63be..c4857d6 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/model/Menu.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/model/Menu.java @@ -1,7 +1,10 @@ package com.gui.star.dal.model; +import com.baomidou.mybatisplus.annotations.TableName; + import java.util.Date; +@TableName() public class Menu { private Long id; diff --git a/star/star-dal/src/main/java/com/gui/star/dal/model/MenuRole.java b/star/star-dal/src/main/java/com/gui/star/dal/model/MenuRole.java index 391e00d..369e30e 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/model/MenuRole.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/model/MenuRole.java @@ -1,8 +1,16 @@ package com.gui.star.dal.model; +import com.baomidou.mybatisplus.annotations.TableId; +import com.baomidou.mybatisplus.annotations.TableName; +import com.baomidou.mybatisplus.enums.IdType; +import lombok.Data; + import java.util.Date; +@TableName("menu_role") +@Data public class MenuRole { + @TableId(type = IdType.AUTO) private Long id; private Long menuId; @@ -14,52 +22,4 @@ public class MenuRole { private Date updatedAt; private Byte deleted; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getMenuId() { - return menuId; - } - - public void setMenuId(Long menuId) { - this.menuId = menuId; - } - - public Long getRoleId() { - return roleId; - } - - public void setRoleId(Long roleId) { - this.roleId = roleId; - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - public Byte getDeleted() { - return deleted; - } - - public void setDeleted(Byte deleted) { - this.deleted = deleted; - } } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/model/Permission.java b/star/star-dal/src/main/java/com/gui/star/dal/model/Permission.java index 6f2f830..6ade834 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/model/Permission.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/model/Permission.java @@ -1,8 +1,16 @@ package com.gui.star.dal.model; +import com.baomidou.mybatisplus.annotations.TableId; +import com.baomidou.mybatisplus.annotations.TableName; +import com.baomidou.mybatisplus.enums.IdType; +import lombok.Data; + import java.util.Date; +@TableName("permission") +@Data public class Permission { + @TableId(type = IdType.AUTO) private Long id; private String name; @@ -14,52 +22,4 @@ public class Permission { private Date updatedAt; private Byte deleted; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name == null ? null : name.trim(); - } - - public String getResValue() { - return resValue; - } - - public void setResValue(String resValue) { - this.resValue = resValue == null ? null : resValue.trim(); - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - public Byte getDeleted() { - return deleted; - } - - public void setDeleted(Byte deleted) { - this.deleted = deleted; - } } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/model/PermissionRole.java b/star/star-dal/src/main/java/com/gui/star/dal/model/PermissionRole.java index 811ecfc..a093883 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/model/PermissionRole.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/model/PermissionRole.java @@ -1,8 +1,16 @@ package com.gui.star.dal.model; +import com.baomidou.mybatisplus.annotations.TableId; +import com.baomidou.mybatisplus.annotations.TableName; +import com.baomidou.mybatisplus.enums.IdType; +import lombok.Data; + import java.util.Date; +@TableName("permission_role") +@Data public class PermissionRole { + @TableId(type = IdType.AUTO) private Long id; private Long permissionId; @@ -14,52 +22,4 @@ public class PermissionRole { private Date updatedAt; private Byte deleted; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getPermissionId() { - return permissionId; - } - - public void setPermissionId(Long permissionId) { - this.permissionId = permissionId; - } - - public Long getRoleId() { - return roleId; - } - - public void setRoleId(Long roleId) { - this.roleId = roleId; - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - public Byte getDeleted() { - return deleted; - } - - public void setDeleted(Byte deleted) { - this.deleted = deleted; - } } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/model/Role.java b/star/star-dal/src/main/java/com/gui/star/dal/model/Role.java index 2dba157..64fe7b4 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/model/Role.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/model/Role.java @@ -1,8 +1,16 @@ package com.gui.star.dal.model; +import com.baomidou.mybatisplus.annotations.TableId; +import com.baomidou.mybatisplus.annotations.TableName; +import com.baomidou.mybatisplus.enums.IdType; +import lombok.Data; + import java.util.Date; +@TableName("role") +@Data public class Role { + @TableId(type = IdType.AUTO) private Long id; private String name; @@ -12,44 +20,4 @@ public class Role { private Date updatedAt; private Byte deleted; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name == null ? null : name.trim(); - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - public Byte getDeleted() { - return deleted; - } - - public void setDeleted(Byte deleted) { - this.deleted = deleted; - } } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/model/User.java b/star/star-dal/src/main/java/com/gui/star/dal/model/User.java index f55e91c..4c64aa9 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/model/User.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/model/User.java @@ -1,14 +1,16 @@ package com.gui.star.dal.model; -import java.util.Date; +import com.baomidou.mybatisplus.annotations.TableId; +import com.baomidou.mybatisplus.annotations.TableName; +import com.baomidou.mybatisplus.enums.IdType; +import lombok.Data; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import java.util.Date; +@Data +@TableName("user") public class User { - @Id - @GeneratedValue(strategy=GenerationType.IDENTITY) + @TableId(type = IdType.AUTO) private Long id; private String loginname; @@ -24,68 +26,4 @@ public class User { private Date updatedAt; private Byte deleted; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getLoginname() { - return loginname; - } - - public void setLoginname(String loginname) { - this.loginname = loginname == null ? null : loginname.trim(); - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password == null ? null : password.trim(); - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email == null ? null : email.trim(); - } - - public String getPhone() { - return phone; - } - - public void setPhone(String phone) { - this.phone = phone == null ? null : phone.trim(); - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - public Byte getDeleted() { - return deleted; - } - - public void setDeleted(Byte deleted) { - this.deleted = deleted; - } } \ No newline at end of file diff --git a/star/star-dal/src/main/java/com/gui/star/dal/model/UserRole.java b/star/star-dal/src/main/java/com/gui/star/dal/model/UserRole.java index d1a7a6e..8494317 100644 --- a/star/star-dal/src/main/java/com/gui/star/dal/model/UserRole.java +++ b/star/star-dal/src/main/java/com/gui/star/dal/model/UserRole.java @@ -1,8 +1,16 @@ package com.gui.star.dal.model; +import com.baomidou.mybatisplus.annotations.TableId; +import com.baomidou.mybatisplus.annotations.TableName; +import com.baomidou.mybatisplus.enums.IdType; +import lombok.Data; + import java.util.Date; +@TableName("user_role") +@Data public class UserRole { + @TableId(type = IdType.AUTO) private Long id; private Long userId; @@ -14,52 +22,4 @@ public class UserRole { private Date updatedAt; private Byte deleted; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getUserId() { - return userId; - } - - public void setUserId(Long userId) { - this.userId = userId; - } - - public Long getRoleId() { - return roleId; - } - - public void setRoleId(Long roleId) { - this.roleId = roleId; - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - public Byte getDeleted() { - return deleted; - } - - public void setDeleted(Byte deleted) { - this.deleted = deleted; - } } \ No newline at end of file diff --git a/star/star-web/pom.xml b/star/star-web/pom.xml index 5f7307a..2ec4b65 100644 --- a/star/star-web/pom.xml +++ b/star/star-web/pom.xml @@ -62,16 +62,6 @@ io.springfox springfox-swagger-ui - - - com.baomidou - mybatisplus-spring-boot-starter - - - - com.baomidou - mybatis-plus - star-web diff --git a/star/star-web/src/main/java/com/gui/star/web/Application.java b/star/star-web/src/main/java/com/gui/star/web/Application.java index 29957d4..11ea4af 100644 --- a/star/star-web/src/main/java/com/gui/star/web/Application.java +++ b/star/star-web/src/main/java/com/gui/star/web/Application.java @@ -2,7 +2,6 @@ import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; @@ -17,8 +16,7 @@ */ @SpringBootApplication @ComponentScan(basePackages = "com.gui") -@MapperScan(basePackages = "com.gui.star.dal.dao") -@EnableAutoConfiguration +@MapperScan(basePackages = "com.gui.star.dal.mapper") @EnableScheduling public class Application { public static void main(String[] args) { diff --git a/star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java b/star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java deleted file mode 100644 index d2f25ac..0000000 --- a/star/star-web/src/main/java/com/gui/star/web/config/MybatisPlusConfig.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.gui.star.web.config; -import javax.sql.DataSource; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; -import com.baomidou.mybatisplus.plugins.PaginationInterceptor; -import com.baomidou.mybatisplus.plugins.PerformanceInterceptor; - -/** - * code is far away from bug with the animal protecting - * ┏┓   ┏┓ - * ┏┛┻━━━┛┻┓ - * ┃       ┃ - * ┃   ━   ┃ - * ┃ ┳┛ ┗┳ ┃ - * ┃       ┃ - * ┃   ┻   ┃ - * ┃       ┃ - * ┗━┓   ┏━┛ - *   ┃   ┃神兽保佑 - *   ┃   ┃代码无BUG! - *   ┃   ┗━━━┓ - *   ┃       ┣┓ - *   ┃       ┏┛ - *   ┗┓┓┏━┳┓┏┛ - *    ┃┫┫ ┃┫┫ - *    ┗┻┛ ┗┻┛ - * - * @Description : MybatisPlus配置 - * --------------------------------- - * @Author : Liang.Guangqing - * @Date : Create in 2017/9/19 13:54 - */ -@Configuration -public class MybatisPlusConfig { - - /*** - * plus 的性能优化 - * @return - */ - @Bean - public PerformanceInterceptor performanceInterceptor() { - PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); - /**/ - performanceInterceptor.setMaxTime(1000); - /**/ - performanceInterceptor.setFormat(true); - return performanceInterceptor; - } - - /** - * @Description : mybatis-plus分页插件 - * --------------------------------- - * @Author : Liang.Guangqing - * @Date : Create in 2017/9/19 13:59 - */ - @Bean - public PaginationInterceptor paginationInterceptor() { - return new PaginationInterceptor(); - } - - - /** - * @Description : druid注入 - * --------------------------------- - * @Author : Liang.Guangqing - * @Date : Create in 2018/1/3 17:38 - */ - @Bean - @ConfigurationProperties("spring.datasource.druid." ) - public DataSource dataSource() { - return DruidDataSourceBuilder - .create() - .build(); - } - -} \ No newline at end of file diff --git a/star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java b/star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java deleted file mode 100644 index f3dc258..0000000 --- a/star/star-web/src/main/java/com/gui/star/web/config/MysqlGenerator.java +++ /dev/null @@ -1,181 +0,0 @@ -package com.gui.star.web.config; - -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import com.baomidou.mybatisplus.enums.FieldFill; -import com.baomidou.mybatisplus.generator.AutoGenerator; -import com.baomidou.mybatisplus.generator.InjectionConfig; -import com.baomidou.mybatisplus.generator.config.DataSourceConfig; -import com.baomidou.mybatisplus.generator.config.FileOutConfig; -import com.baomidou.mybatisplus.generator.config.GlobalConfig; -import com.baomidou.mybatisplus.generator.config.PackageConfig; -import com.baomidou.mybatisplus.generator.config.StrategyConfig; -import com.baomidou.mybatisplus.generator.config.TemplateConfig; -import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert; -import com.baomidou.mybatisplus.generator.config.po.TableFill; -import com.baomidou.mybatisplus.generator.config.po.TableInfo; -import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; -import com.baomidou.mybatisplus.generator.config.rules.DbType; -import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; - -/** - *code is far away from bug with the animal protecting - * ┏┓   ┏┓ - *┏┛┻━━━┛┻┓ - *┃       ┃   - *┃   ━   ┃ - *┃ ┳┛ ┗┳ ┃ - *┃       ┃ - *┃   ┻   ┃ - *┃       ┃ - *┗━┓   ┏━┛ - *  ┃   ┃神兽保佑 - *  ┃   ┃代码无BUG! - *  ┃   ┗━━━┓ - *  ┃       ┣┓ - *  ┃       ┏┛ - *  ┗┓┓┏━┳┓┏┛ - *   ┃┫┫ ┃┫┫ - *   ┗┻┛ ┗┻┛ - *   - * @Description : MybatisPlus代码生成器 - * --------------------------------- - * @Author : Liang.Guangqing - * @Date : Create in 2017/9/19 14:48  - */ -public class MysqlGenerator { - private static String packageName="web"; //文件路径 - private static String authorName="WuHoujian"; //作者 - private static String table="user"; //table名字 - private static String prefix=""; //table前缀 - private static File file = new File(packageName); - private static String path = file.getAbsolutePath(); - - public static void main(String[] args) { - // 自定义需要填充的字段 - List tableFillList = new ArrayList<>(); - tableFillList.add(new TableFill("ASDD_SS", FieldFill.INSERT_UPDATE)); - // 代码生成器 - AutoGenerator mpg = new AutoGenerator().setGlobalConfig( - // 全局配置 - new GlobalConfig() - .setOutputDir(path+"/src/main/java")//输出目录 - .setFileOverride(true)// 是否覆盖文件 - .setActiveRecord(true)// 开启 activeRecord 模式 - .setEnableCache(false)// XML 二级缓存 - .setBaseResultMap(true)// XML ResultMap - .setBaseColumnList(true)// XML columList - .setOpen(false)//生成后打开文件夹 - .setAuthor(authorName) - // 自定义文件命名,注意 %s 会自动填充表实体属性! - .setMapperName("%sMapper") - .setXmlName("%sMapper") - .setServiceName("%sService") - .setServiceImplName("%sServiceImpl") - .setControllerName("%sController") - ).setDataSource( - // 数据源配置 - new DataSourceConfig() - .setDbType(DbType.MYSQL)// 数据库类型 - .setTypeConvert(new MySqlTypeConvert() { - // 自定义数据库表字段类型转换【可选】 - @Override - public DbColumnType processTypeConvert(String fieldType) { - System.out.println("转换类型:" + fieldType); - // if ( fieldType.toLowerCase().contains( "tinyint" ) ) { - // return DbColumnType.BOOLEAN; - // } - return super.processTypeConvert(fieldType); - } - }) - .setDriverName("com.mysql.jdbc.Driver") - .setUsername("root") - .setPassword("root") - .setUrl("jdbc:mysql://127.0.0.1:3306/star?characterEncoding=utf8") - ).setStrategy( - // 策略配置 - new StrategyConfig() - // .setCapitalMode(true)// 全局大写命名 - //.setDbColumnUnderline(true)//全局下划线命名 - .setTablePrefix(new String[]{prefix})// 此处可以修改为您的表前缀 - .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略 - .setInclude(new String[] { table }) // 需要生成的表 - .setRestControllerStyle(true) - //.setExclude(new String[]{"test"}) // 排除生成的表 - // 自定义实体父类 - // .setSuperEntityClass("com.baomidou.demo.TestEntity") - // 自定义实体,公共字段 - //.setSuperEntityColumns(new String[]{"test_id"}) - .setTableFillList(tableFillList) - // 自定义 mapper 父类 - // .setSuperMapperClass("com.baomidou.demo.TestMapper") - // 自定义 service 父类 - // .setSuperServiceClass("com.baomidou.demo.TestService") - // 自定义 service 实现类父类 - // .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl") - // 自定义 controller 父类 - .setSuperControllerClass("com.gui.star."+packageName+".controller.AbstractController") - // 【实体】是否生成字段常量(默认 false) - // public static final String ID = "test_id"; - // .setEntityColumnConstant(true) - // 【实体】是否为构建者模型(默认 false) - // public User setName(String name) {this.name = name; return this;} - // .setEntityBuilderModel(true) - // 【实体】是否为lombok模型(默认 false)document - // .setEntityLombokModel(true) - // Boolean类型字段是否移除is前缀处理 - // .setEntityBooleanColumnRemoveIsPrefix(true) - // .setRestControllerStyle(true) - // .setControllerMappingHyphenStyle(true) - ).setPackageInfo( - // 包配置 - new PackageConfig() - //.setModuleName("User") - .setParent("com.gui.star."+packageName)// 自定义包路径 - .setController("controller")// 这里是控制器包名,默认 web - .setEntity("entity") - .setMapper("dao") - .setService("service") - .setServiceImpl("service.impl") - //.setXml("mapper") - ).setCfg( - // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 - new InjectionConfig() { - @Override - public void initMap() { - Map map = new HashMap<>(); - map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); - this.setMap(map); - } - }.setFileOutConfigList(Collections.singletonList(new FileOutConfig("/templates/mapper.xml.vm") { - // 自定义输出文件目录 - @Override - public String outputFile(TableInfo tableInfo) { - return path+"/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml"; - } - })) - ).setTemplate( - // 关闭默认 xml 生成,调整生成 至 根目录 - new TemplateConfig().setXml(null) - // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy - // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置: - // .setController("..."); - // .setEntity("..."); - // .setMapper("..."); - // .setXml("..."); - // .setService("..."); - // .setServiceImpl("..."); - ); - - // 执行生成 - mpg.execute(); - - // 打印注入设置,这里演示模板里面怎么获取注入内容【可无】 - System.err.println(mpg.getCfg().getMap().get("abc")); - } -} \ No newline at end of file diff --git a/star/star-web/src/main/resources/application.yml b/star/star-web/src/main/resources/application.yml index 39ce2a9..af31f4f 100644 --- a/star/star-web/src/main/resources/application.yml +++ b/star/star-web/src/main/resources/application.yml @@ -9,10 +9,12 @@ spring: minIdle: 2 maxActive: 10 -mybatis: - mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 - type-aliases-package: com.gui.star.dal.model # 注意:对应实体类的路径 +#mybatis: +# mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 +# type-aliases-package: com.gui.star.dal.model # 注意:对应实体类的路径 mybatis-plus: mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 - typeAliasesPackage: com.gui.star.dal.model # 注意:对应实体类的路径 \ No newline at end of file + type-aliases-package: com.gui.star.dal.model + configuration: + map-underscore-to-camel-case: true \ No newline at end of file From 97f84ba3d46bcb8f2f3c0fb99af818213226f25c Mon Sep 17 00:00:00 2001 From: williamjava Date: Tue, 16 Apr 2019 21:08:05 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star/pom.xml | 14 ---- .../src/main/resources/generatorConfig.xml | 69 ------------------- .../src/main/resources/mapper/UserMapper.xml | 11 ++- .../src/main/resources/application.yml | 2 +- 4 files changed, 11 insertions(+), 85 deletions(-) delete mode 100644 star/star-dal/src/main/resources/generatorConfig.xml diff --git a/star/pom.xml b/star/pom.xml index 74be9b1..e68fffa 100644 --- a/star/pom.xml +++ b/star/pom.xml @@ -71,13 +71,6 @@ ${springboot.version} - - - org.mybatis.spring.boot - mybatis-spring-boot-starter - 1.3.2 - - com.alibaba druid-spring-boot-starter @@ -126,13 +119,6 @@ 2.5.0 - - - tk.mybatis - mapper-spring-boot-starter - 2.0.4 - - org.slf4j slf4j-api diff --git a/star/star-dal/src/main/resources/generatorConfig.xml b/star/star-dal/src/main/resources/generatorConfig.xml deleted file mode 100644 index e8979c1..0000000 --- a/star/star-dal/src/main/resources/generatorConfig.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - -
- - -
- - -
- - -
- - -
- - -
-
-
\ No newline at end of file diff --git a/star/star-dal/src/main/resources/mapper/UserMapper.xml b/star/star-dal/src/main/resources/mapper/UserMapper.xml index 1344ffa..6097212 100644 --- a/star/star-dal/src/main/resources/mapper/UserMapper.xml +++ b/star/star-dal/src/main/resources/mapper/UserMapper.xml @@ -1,5 +1,14 @@ - + + + + + + + + + + \ No newline at end of file diff --git a/star/star-web/src/main/resources/application.yml b/star/star-web/src/main/resources/application.yml index af31f4f..fe7a83a 100644 --- a/star/star-web/src/main/resources/application.yml +++ b/star/star-web/src/main/resources/application.yml @@ -14,7 +14,7 @@ spring: # type-aliases-package: com.gui.star.dal.model # 注意:对应实体类的路径 mybatis-plus: - mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 + mapper-locations: classpath:mapper/**/*.xml #注意:一定要对应mapper映射xml文件的所在路径 type-aliases-package: com.gui.star.dal.model configuration: map-underscore-to-camel-case: true \ No newline at end of file From 5beee3c0cea9d341f6ac47c93533d6083937e9b2 Mon Sep 17 00:00:00 2001 From: wuhoujian Date: Wed, 17 Apr 2019 10:07:20 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gui/star/biz/service/UserService.java | 2 +- .../biz/service/impl/UserServiceImpl.java | 4 +- star/star-common/pom.xml | 15 ++++ .../com/gui/star/common/form/UserForm.java | 8 ++ .../java/com/gui/star/common/util/Result.java | 6 +- star/star-web/pom.xml | 15 ++-- .../star/web/controller/UserController.java | 82 +++++++------------ .../src/main/resources/application.yml | 4 +- 8 files changed, 71 insertions(+), 65 deletions(-) diff --git a/star/star-biz/src/main/java/com/gui/star/biz/service/UserService.java b/star/star-biz/src/main/java/com/gui/star/biz/service/UserService.java index 0ad6364..64a2b32 100644 --- a/star/star-biz/src/main/java/com/gui/star/biz/service/UserService.java +++ b/star/star-biz/src/main/java/com/gui/star/biz/service/UserService.java @@ -13,5 +13,5 @@ public interface UserService { */ List queryList(); - void save(UserForm form); + Long save(UserForm form); } diff --git a/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java b/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java index 7f91bdc..c128559 100644 --- a/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java +++ b/star/star-biz/src/main/java/com/gui/star/biz/service/impl/UserServiceImpl.java @@ -38,12 +38,12 @@ public List queryList() { * 保存用户信息 */ @Override - public void save(UserForm form) { + public Long save(UserForm form) { User user = ObjectUtil.source2Target(form, User.class); DomainUtil.setCommonValueForCreate(user); this.insert(user); - log.info("用户ID为:{}", user.getId()); + return user.getId(); } } diff --git a/star/star-common/pom.xml b/star/star-common/pom.xml index d29e51e..8b0a2e7 100644 --- a/star/star-common/pom.xml +++ b/star/star-common/pom.xml @@ -40,5 +40,20 @@ org.apache.commons commons-lang3
+ + + io.springfox + springfox-swagger-ui + + + + io.springfox + springfox-swagger2 + + + + com.alibaba + fastjson + diff --git a/star/star-common/src/main/java/com/gui/star/common/form/UserForm.java b/star/star-common/src/main/java/com/gui/star/common/form/UserForm.java index a004f78..8ccf3a5 100644 --- a/star/star-common/src/main/java/com/gui/star/common/form/UserForm.java +++ b/star/star-common/src/main/java/com/gui/star/common/form/UserForm.java @@ -1,16 +1,24 @@ package com.gui.star.common.form; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data +@ApiModel("用户对象信息封装") public class UserForm { + @ApiModelProperty(value = "ID") private Long id; + @ApiModelProperty(value = "登录名", required = true) private String loginname; + @ApiModelProperty(value = "密码", required = true) private String password; + @ApiModelProperty(value = "邮箱") private String email; + @ApiModelProperty(value = "联系电话") private String phone; } \ No newline at end of file diff --git a/star/star-common/src/main/java/com/gui/star/common/util/Result.java b/star/star-common/src/main/java/com/gui/star/common/util/Result.java index 7d67ff7..f7058b6 100644 --- a/star/star-common/src/main/java/com/gui/star/common/util/Result.java +++ b/star/star-common/src/main/java/com/gui/star/common/util/Result.java @@ -1,12 +1,11 @@ package com.gui.star.common.util; -import java.io.Serializable; - import com.gui.star.common.enums.CodeEnum; - import lombok.Data; import lombok.extern.slf4j.Slf4j; +import java.io.Serializable; + /** * * @author wuhoujian @@ -17,7 +16,6 @@ * 数据 以json格式返回客户端请求的数据 */ @Data -@SuppressWarnings("serial") @Slf4j public class Result implements Serializable { private T data; diff --git a/star/star-web/pom.xml b/star/star-web/pom.xml index 2ec4b65..722e818 100644 --- a/star/star-web/pom.xml +++ b/star/star-web/pom.xml @@ -34,11 +34,6 @@ spring-boot-starter-web - - org.mybatis.spring.boot - mybatis-spring-boot-starter - - org.springframework.boot spring-boot-starter-test @@ -62,6 +57,16 @@ io.springfox springfox-swagger-ui + + + com.baomidou + mybatis-plus-boot-starter + + + + com.alibaba + fastjson + star-web diff --git a/star/star-web/src/main/java/com/gui/star/web/controller/UserController.java b/star/star-web/src/main/java/com/gui/star/web/controller/UserController.java index 64a51e9..f1c402b 100644 --- a/star/star-web/src/main/java/com/gui/star/web/controller/UserController.java +++ b/star/star-web/src/main/java/com/gui/star/web/controller/UserController.java @@ -1,71 +1,49 @@ package com.gui.star.web.controller; -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.util.CollectionUtils; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - import com.gui.star.biz.service.UserService; import com.gui.star.biz.vo.UserVo; import com.gui.star.common.form.UserForm; import com.gui.star.common.util.Result; - import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; /** * 用户管理控制器 - * - * 注解@ApiOperation用来给API增加说明 - * - * 注解@ApiImplicitParams和@ApiImplicitParam用来给参数增加说明 - * * @author wuhoujian - * */ @Api(value = "用户管理Api", tags = "用户相关操作") @RestController @RequestMapping("/user") public class UserController { - @Autowired - private UserService userService; - - @ApiOperation(value = "查看用户列表", notes = "查看所有用户信息列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - @ApiImplicitParam(name = "id", value = "用户ID", required = false, dataType = "Long") - @RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST }) - public String list() { - List userList = userService.queryList(); - if (!CollectionUtils.isEmpty(userList)) { - for (UserVo user : userList) { - System.out.println(user.getLoginname() + "===" + user.getPassword()); - } - } - - return "Hello, SpringBoot! I have " + userList.size() + "个用户!"; - } - - /** - * 保存用户信息 - * - * @param form - * @return - */ - @ApiOperation(value = "保存用户信息", notes = "保存用户信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - @ApiImplicitParams(value = { - @ApiImplicitParam(name = "loginname", value = "登录名", required = true, dataType = "string", paramType = "query"), - @ApiImplicitParam(name = "password", value = "登录密码", required = true, dataType = "string", paramType = "query"), - @ApiImplicitParam(name = "email", value = "邮箱", required = false, dataType = "string", paramType = "query") }) - @RequestMapping(value = "/save", method = { RequestMethod.GET, RequestMethod.POST }) - public Result save(UserForm form) { - userService.save(form); - - return Result.generateSuccess("保存成功"); - } + @Autowired + private UserService userService; + + @ApiOperation(value = "查看用户列表", notes = "查看所有用户信息列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/list", method = RequestMethod.POST) + public Result> list() { + List userList = userService.queryList(); + + return Result.generateSuccess(userList); + } + + /** + * 保存用户信息 + * + * @param form + * @return + */ + @ApiOperation(value = "保存用户信息", notes = "保存用户信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/save", method = RequestMethod.POST) + public Result save(@ModelAttribute UserForm form) { + return Result.generateSuccess(userService.save(form)); + } } diff --git a/star/star-web/src/main/resources/application.yml b/star/star-web/src/main/resources/application.yml index fe7a83a..c916f2f 100644 --- a/star/star-web/src/main/resources/application.yml +++ b/star/star-web/src/main/resources/application.yml @@ -17,4 +17,6 @@ mybatis-plus: mapper-locations: classpath:mapper/**/*.xml #注意:一定要对应mapper映射xml文件的所在路径 type-aliases-package: com.gui.star.dal.model configuration: - map-underscore-to-camel-case: true \ No newline at end of file + map-underscore-to-camel-case: true + # 配置slq打印日志 + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl \ No newline at end of file From 22ffaddd890928a5d36e452e82bfa72b2f1c568d Mon Sep 17 00:00:00 2001 From: wuhoujian Date: Wed, 17 Apr 2019 15:08:27 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/ContainerEventListener.java | 13 ++--- .../java/com/gui/star/web/Application.java | 2 + .../star/web/controller/TestController.java | 40 +++++++------- .../com/gui/star/web/executor/MyExecutor.java | 4 +- .../src/main/resources/logback-spring.xml | 52 +++++++++++++++++++ 5 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 star/star-web/src/main/resources/logback-spring.xml diff --git a/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java b/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java index 3fb011e..29e1926 100644 --- a/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java +++ b/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java @@ -1,12 +1,12 @@ package com.gui.star.biz.event.listener; +import com.gui.star.biz.event.events.TestEvent; +import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; -import com.gui.star.biz.event.events.TestEvent; - /** * spring事件监听器 * @@ -23,6 +23,7 @@ * */ @Component +@Slf4j public class ContainerEventListener { /** * 监听容器启动事件 @@ -34,7 +35,7 @@ public class ContainerEventListener { * * 2.容器的启动完成了表明所有的bean都已经完成了注册,而且bean之间的相互依赖也完成了 * - * 3.容器启动完成和我们定义的Application启动类极其包含的main方法完全是两个概念。前者的时间发生在前面,Application.main()的作用在于启动容器里面的应用程序(比如我们的saturn) + * 3.容器启动完成和我们定义的Application启动类极其包含的main方法完全是两个概念。前者的时间发生在前面,Application.main()的作用在于启动容器里面的应用程序 * * * @param event @@ -50,13 +51,13 @@ public void afterContainerStartup(ContextRefreshedEvent event) { /** * 监听所有的TestEvent类型事件 - * + * + * @Async标识方法异步执行 */ @Async @EventListener public void handleTestEvent(TestEvent event) { + log.info("我的测试事件开始执行了,测试内容:" , event.getEventContent()); // 可以调用本地业务处理逻辑,也可以调用远程的发送短信、邮件等服务。 - - System.out.println("我的测试事件开始执行了,测试内容:" + event.getEventContent()); } } diff --git a/star/star-web/src/main/java/com/gui/star/web/Application.java b/star/star-web/src/main/java/com/gui/star/web/Application.java index 11ea4af..0a67507 100644 --- a/star/star-web/src/main/java/com/gui/star/web/Application.java +++ b/star/star-web/src/main/java/com/gui/star/web/Application.java @@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; /** @@ -18,6 +19,7 @@ @ComponentScan(basePackages = "com.gui") @MapperScan(basePackages = "com.gui.star.dal.mapper") @EnableScheduling +@EnableAsync public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/star/star-web/src/main/java/com/gui/star/web/controller/TestController.java b/star/star-web/src/main/java/com/gui/star/web/controller/TestController.java index 4a69a2a..13f95a2 100644 --- a/star/star-web/src/main/java/com/gui/star/web/controller/TestController.java +++ b/star/star-web/src/main/java/com/gui/star/web/controller/TestController.java @@ -1,36 +1,36 @@ package com.gui.star.web.controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - import com.gui.star.biz.event.events.TestEvent; +import com.gui.star.common.util.Result; import com.gui.star.common.util.SpringContextUtil; - +import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import springfox.documentation.annotations.ApiIgnore; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; /** * 我的测试控制器 - * - * @author wuhoujian * + * @author wuhoujian */ -@ApiIgnore +@Api(value = "测试管理Api", tags = "测试相关操作") @RestController @RequestMapping("/test") public class TestController { + @ApiOperation(value = "打招呼sayHi", notes = "打招呼sayHi", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/sayHi", method = RequestMethod.GET) + public Result sayHi() { + return Result.generateSuccess("Hello, SpringBoot!"); + } - @ApiOperation(value = "打招呼sayHi", notes = "打招呼sayHi") - @RequestMapping("/sayHi") - public String sayHi() { - return "Hello, SpringBoot!"; - } + @ApiOperation(value = "测试事件发布", notes = "测试事件发布", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/testEvent", method = RequestMethod.POST) + public Result testEvent() { + // 发布事件 + SpringContextUtil.getApplicationContext().publishEvent(new TestEvent(this, "我的测试事件")); - @ApiOperation(value = "测试事件发布", notes = "测试事件发布") - @RequestMapping("/testEvent") - public String testEvent() { - // 发布插入登录日志事件 - SpringContextUtil.getApplicationContext().publishEvent(new TestEvent(this, "我的测试事件")); - return "事件发布成功,事件执行结果见控制台输出。"; - } + return Result.generateSuccess("事件发布成功,事件执行结果见控制台输出。"); + } } diff --git a/star/star-web/src/main/java/com/gui/star/web/executor/MyExecutor.java b/star/star-web/src/main/java/com/gui/star/web/executor/MyExecutor.java index 03b48a8..dfb82e4 100644 --- a/star/star-web/src/main/java/com/gui/star/web/executor/MyExecutor.java +++ b/star/star-web/src/main/java/com/gui/star/web/executor/MyExecutor.java @@ -13,8 +13,8 @@ public class MyExecutor { // @Scheduled(cron = "0 0 1 ? * MON"),每周一凌晨1点执行 - // 每5秒执行 - @Scheduled(fixedRate = 5000) + // 每分钟执行 + @Scheduled(fixedRate = 60000) public void sayName() { System.out.println("My name is William..."); } diff --git a/star/star-web/src/main/resources/logback-spring.xml b/star/star-web/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..c9dbac4 --- /dev/null +++ b/star/star-web/src/main/resources/logback-spring.xml @@ -0,0 +1,52 @@ + + + + + + + + ${user.home}/logs/${LOG_FILE_NAME}.log + + ${user.home}/logs/%d{yyyy-MM-dd}/${LOG_FILE_NAME}.%i.log + 100 + 50MB + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + + ${user.home}/logs/${LOG_FILE_NAME}-error.log + + ${user.home}/logs/%d{yyyy-MM-dd}/${LOG_FILE_NAME}-error.%i.log + 100 + 50MB + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + ERROR + ACCEPT + DENY + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + + + + \ No newline at end of file From 80fea33bd3fd1e271473e19e87efabaaf4f57b8d Mon Sep 17 00:00:00 2001 From: wuhoujian Date: Sun, 28 Apr 2019 17:22:04 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=99=A8=E5=92=8C=E6=8B=A6=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/ContainerEventListener.java | 2 +- .../java/com/gui/star/web/Application.java | 2 + .../com/gui/star/web/config/FilterConfig.java | 46 +++++++++++++++++++ .../star/web/config/InterceptorConfig.java | 26 +++++++++++ .../star/web/filter/AcustomFollowFilter1.java | 39 ++++++++++++++++ .../com/gui/star/web/filter/CustomFilter.java | 33 +++++++++++++ .../gui/star/web/filter/CustomFilter1.java | 39 ++++++++++++++++ .../star/web/filter/CustomFollowFilter.java | 33 +++++++++++++ .../star/web/interceptors/LogInterceptor.java | 34 ++++++++++++++ .../src/main/resources/application.yml | 7 ++- 10 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 star/star-web/src/main/java/com/gui/star/web/config/FilterConfig.java create mode 100644 star/star-web/src/main/java/com/gui/star/web/config/InterceptorConfig.java create mode 100644 star/star-web/src/main/java/com/gui/star/web/filter/AcustomFollowFilter1.java create mode 100644 star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter.java create mode 100644 star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter1.java create mode 100644 star/star-web/src/main/java/com/gui/star/web/filter/CustomFollowFilter.java create mode 100644 star/star-web/src/main/java/com/gui/star/web/interceptors/LogInterceptor.java diff --git a/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java b/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java index 29e1926..57b7f66 100644 --- a/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java +++ b/star/star-biz/src/main/java/com/gui/star/biz/event/listener/ContainerEventListener.java @@ -57,7 +57,7 @@ public void afterContainerStartup(ContextRefreshedEvent event) { @Async @EventListener public void handleTestEvent(TestEvent event) { - log.info("我的测试事件开始执行了,测试内容:" , event.getEventContent()); + log.info("我的测试事件开始执行了,测试内容:{}" , event.getEventContent()); // 可以调用本地业务处理逻辑,也可以调用远程的发送短信、邮件等服务。 } } diff --git a/star/star-web/src/main/java/com/gui/star/web/Application.java b/star/star-web/src/main/java/com/gui/star/web/Application.java index 0a67507..1a0c1c8 100644 --- a/star/star-web/src/main/java/com/gui/star/web/Application.java +++ b/star/star-web/src/main/java/com/gui/star/web/Application.java @@ -3,6 +3,7 @@ import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @@ -20,6 +21,7 @@ @MapperScan(basePackages = "com.gui.star.dal.mapper") @EnableScheduling @EnableAsync +@ServletComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/star/star-web/src/main/java/com/gui/star/web/config/FilterConfig.java b/star/star-web/src/main/java/com/gui/star/web/config/FilterConfig.java new file mode 100644 index 0000000..609a6f9 --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/config/FilterConfig.java @@ -0,0 +1,46 @@ +package com.gui.star.web.config; + +import com.gui.star.web.filter.CustomFilter; +import com.gui.star.web.filter.CustomFollowFilter; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +/** + * 过滤器第二种使用方式配置 + */ +@Configuration +public class FilterConfig { + @Bean + public FilterRegistrationBean filterRegistrationBean(){ + FilterRegistrationBean registrationBean = new FilterRegistrationBean(); + + CustomFilter customFilter = new CustomFilter(); + registrationBean.setFilter(customFilter); + registrationBean.setOrder(10);//设置过滤器执行的先后顺序 + + List urlPatterns = new ArrayList(); + urlPatterns.add("/*"); + registrationBean.setUrlPatterns(urlPatterns); + + return registrationBean; + } + + @Bean + public FilterRegistrationBean filterRegistrationBean1(){ + FilterRegistrationBean registrationBean = new FilterRegistrationBean(); + + CustomFollowFilter customFollowFilter = new CustomFollowFilter(); + registrationBean.setFilter(customFollowFilter); + registrationBean.setOrder(1); + + List urlPatterns = new ArrayList(); + urlPatterns.add("/*"); + registrationBean.setUrlPatterns(urlPatterns); + + return registrationBean; + } +} diff --git a/star/star-web/src/main/java/com/gui/star/web/config/InterceptorConfig.java b/star/star-web/src/main/java/com/gui/star/web/config/InterceptorConfig.java new file mode 100644 index 0000000..e429bdc --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/config/InterceptorConfig.java @@ -0,0 +1,26 @@ +package com.gui.star.web.config; + +import com.gui.star.web.interceptors.LogInterceptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * 拦截器配置类(实现WebMvcConfigurer接口) + */ +@Configuration +public class InterceptorConfig implements WebMvcConfigurer { + @Autowired + private LogInterceptor logInterceptor; + + /** + * 注册自定义的拦截器类 + * @param registry + */ + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 加入自定义拦截器,这里可以根据实际需要对各个url添加不同的拦截器 + registry.addInterceptor(logInterceptor).addPathPatterns("/**/**"); + } +} diff --git a/star/star-web/src/main/java/com/gui/star/web/filter/AcustomFollowFilter1.java b/star/star-web/src/main/java/com/gui/star/web/filter/AcustomFollowFilter1.java new file mode 100644 index 0000000..66ba898 --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/filter/AcustomFollowFilter1.java @@ -0,0 +1,39 @@ +package com.gui.star.web.filter; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.Order; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import java.io.IOException; + +/** + * 利用WebFilter注解配置,启动类要结合@ServletComponentScan注解 + */ +@WebFilter(filterName = "acustomFollowFilter1", urlPatterns = "/*") +@Slf4j +public class AcustomFollowFilter1 implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + log.info("AcustomFollowFilter1初始化..."); + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + log.info("doFilter in AcustomFollowFilter1请求处理...."); + + //对request、response进行一些预处理,比如设置请求编码 + // request.setCharacterEncoding("UTF-8"); + // response.setCharacterEncoding("UTF-8"); + + //TODO 进行业务逻辑 + + filterChain.doFilter(servletRequest, servletResponse); + } + + @Override + public void destroy() { + log.info("AcustomFollowFilter1销毁..."); + } +} diff --git a/star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter.java b/star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter.java new file mode 100644 index 0000000..88ca260 --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter.java @@ -0,0 +1,33 @@ +package com.gui.star.web.filter; + +import lombok.extern.slf4j.Slf4j; + +import javax.servlet.*; +import java.io.IOException; + +@Slf4j +public class CustomFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + log.info("CustomFilter初始化..."); + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + log.info("doFilter in CustomFilter请求处理...."); + + //对request、response进行一些预处理,比如设置请求编码 + // request.setCharacterEncoding("UTF-8"); + // response.setCharacterEncoding("UTF-8"); + + //TODO 进行业务逻辑 + + filterChain.doFilter(servletRequest, servletResponse); + } + + @Override + public void destroy() { + log.info("CustomFilter销毁..."); + } +} diff --git a/star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter1.java b/star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter1.java new file mode 100644 index 0000000..505d31f --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/filter/CustomFilter1.java @@ -0,0 +1,39 @@ +package com.gui.star.web.filter; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.Order; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import java.io.IOException; + +/** + * 利用WebFilter注解配置,启动类要结合@ServletComponentScan注解 + */ +@WebFilter(filterName = "customFilter1", urlPatterns = "/*") +@Slf4j +public class CustomFilter1 implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + log.info("CustomFilter1初始化..."); + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + log.info("doFilter in CustomFilter1请求处理...."); + + //对request、response进行一些预处理,比如设置请求编码 + // request.setCharacterEncoding("UTF-8"); + // response.setCharacterEncoding("UTF-8"); + + //TODO 进行业务逻辑 + + filterChain.doFilter(servletRequest, servletResponse); + } + + @Override + public void destroy() { + log.info("CustomFilter1销毁..."); + } +} diff --git a/star/star-web/src/main/java/com/gui/star/web/filter/CustomFollowFilter.java b/star/star-web/src/main/java/com/gui/star/web/filter/CustomFollowFilter.java new file mode 100644 index 0000000..8f330b2 --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/filter/CustomFollowFilter.java @@ -0,0 +1,33 @@ +package com.gui.star.web.filter; + +import lombok.extern.slf4j.Slf4j; + +import javax.servlet.*; +import java.io.IOException; + +@Slf4j +public class CustomFollowFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + log.info("CustomFollowFilter初始化..."); + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + log.info("doFilter in CustomFollowFilter请求处理...."); + + //对request、response进行一些预处理,比如设置请求编码 + // request.setCharacterEncoding("UTF-8"); + // response.setCharacterEncoding("UTF-8"); + + //TODO 进行业务逻辑 + + filterChain.doFilter(servletRequest, servletResponse); + } + + @Override + public void destroy() { + log.info("CustomFollowFilter销毁..."); + } +} diff --git a/star/star-web/src/main/java/com/gui/star/web/interceptors/LogInterceptor.java b/star/star-web/src/main/java/com/gui/star/web/interceptors/LogInterceptor.java new file mode 100644 index 0000000..add9208 --- /dev/null +++ b/star/star-web/src/main/java/com/gui/star/web/interceptors/LogInterceptor.java @@ -0,0 +1,34 @@ +package com.gui.star.web.interceptors; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 自定义拦截器(继承HandlerInterceptorAdapter类) + */ +@Configuration +@Slf4j +public class LogInterceptor extends HandlerInterceptorAdapter { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + log.info("请求前调用:preHandle" ); + return super.preHandle(request, response, handler); + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + log.info("请求后调用:postHandle"); + super.postHandle(request, response, handler, modelAndView); + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + log.info("请求调用完成后回调方法:afterCompletion"); + super.afterCompletion(request, response, handler, ex); + } +} diff --git a/star/star-web/src/main/resources/application.yml b/star/star-web/src/main/resources/application.yml index c916f2f..cf64596 100644 --- a/star/star-web/src/main/resources/application.yml +++ b/star/star-web/src/main/resources/application.yml @@ -8,7 +8,7 @@ spring: initialSize: 2 minIdle: 2 maxActive: 10 - + #mybatis: # mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 # type-aliases-package: com.gui.star.dal.model # 注意:对应实体类的路径 @@ -19,4 +19,7 @@ mybatis-plus: configuration: map-underscore-to-camel-case: true # 配置slq打印日志 - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl \ No newline at end of file + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + +server: + port: 8700 \ No newline at end of file