Commit da556397 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

add:menu

parent 0508e072
...@@ -33,5 +33,10 @@ ...@@ -33,5 +33,10 @@
<groupId>org.freemarker</groupId> <groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId> <artifactId>freemarker</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.github.fengyuchenglun</groupId>
<artifactId>kim-boot-util</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package com.duanledexianxianxian.demo;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 代码生成器
*/
public class CodeGenerator {
/**
* 读取控制台内容
*
* @param tip the tip
* @return the string
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
// 项目路径
String projectPath = "F:/@project@/@dianli@/springboot-demo/demo";
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("duanledexianxianxian");
gc.setOpen(false);
gc.setFileOverride(true);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://rm-bp1451sz2b7q8w95xvo.mysql.rds.aliyuncs.com:3306/convention?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("convention");
dsc.setPassword("Convention123");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
pc.setParent("com.duanledexianxianxian.demo");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
package com.duanledexianxianxian.demo.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "com.duanledexianxianxian.demo.dao")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
\ No newline at end of file
package com.duanledexianxianxian.demo.dao;
import com.duanledexianxianxian.demo.domain.entity.Menu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 菜单表 Mapper 接口
* </p>
*
* @author duanledexianxianxian
* @since 2020-03-19
*/
public interface MenuDao extends BaseMapper<Menu> {
}
package com.duanledexianxianxian.demo.dao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duanledexianxianxian.demo.domain.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.duanledexianxianxian.demo.domain.model.query.UserQuery;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* <p>
* 用户表 Mapper 接口
* </p>
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
public interface UserDao extends BaseMapper<User> {
/**
* Gets list.
*
* @param page the page
* @param query the query
* @return the list
*/
List<User> getList(IPage<User> page, @Param("query") UserQuery query);
}
package com.duanledexianxianxian.demo.domain.dto;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 菜单传输对象
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Data
public class MenuDTO implements Serializable {
/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* 菜单编号
*/
private Long menuId;
/**
* 菜单url
*/
private String menuUrl;
/**
* 菜单名称
*/
private String menuName;
/**
* 菜单编码
*/
private String menuCode;
/**
* 创建时间
*/
private LocalDateTime createTime;
}
package com.duanledexianxianxian.demo.domain.dto;
import com.duanledexianxianxian.demo.domain.entity.Menu;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
/**
* 用户传输对象
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Data
public class UserDTO implements Serializable {
/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
private Long userId;
/**
* 用户名称
*/
private String userName;
/**
* 用户昵称
*/
private String nickName;
/**
* 年龄
*/
private Integer age;
/**
* 生日
*/
private LocalDate birthday;
/**
* 菜单列表
*/
List<MenuDTO> menuList;
}
package com.duanledexianxianxian.demo.domain.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.time.LocalDateTime;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
* 菜单表
* </p>
*
* @author duanledexianxianxian
* @since 2020-03-19
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("test_menu")
public class Menu implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
private Long userId;
/**
* 菜单编号
*/
@TableId
private Long menuId;
/**
* 菜单url
*/
private String menuUrl;
/**
* 菜单名称
*/
private String menuName;
/**
* 菜单编码
*/
private String menuCode;
/**
* 创建时间
*/
private LocalDateTime createTime;
}
package com.duanledexianxianxian.demo.domain.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.time.LocalDate;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
* 用户表
* </p>
*
* @author duanledexianxianxian
* @since 2020-03-19
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("test_user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
@TableId
private Long userId;
/**
* 用户名称
*/
private String userName;
/**
* 用户昵称
*/
private String nickName;
/**
* 年龄
*/
private Integer age;
/**
* 生日
*/
private LocalDate birthday;
}
package com.duanledexianxianxian.demo.domain.model.query;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 菜单表单对象
*
* @author duanledexianxianxian
* @since 2020-03-19
*/
@Data
public class MenuForm implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
private Long userId;
/**
* 菜单编号
*/
private Long menuId;
/**
* 菜单url
*/
private String menuUrl;
/**
* 菜单名称
*/
private String menuName;
/**
* 菜单编码
*/
private String menuCode;
/**
* 创建时间
*/
private LocalDateTime createTime;
}
package com.duanledexianxianxian.demo.domain.model.query;
import lombok.Data;
import java.io.Serializable;
/**
* 菜单查询对象
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Data
public class MenuQuery implements Serializable {
/**
* 菜单名称
*/
private String menuName;
}
package com.duanledexianxianxian.demo.domain.model.query;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDate;
/**
* 用户表单对象
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Data
public class UserForm implements Serializable {
/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
private Long userId;
/**
* 用户名称
*/
private String userName;
/**
* 用户昵称
*/
private String nickName;
/**
* 年龄
*/
private Integer age;
/**
* 生日
*/
private LocalDate birthday;
}
package com.duanledexianxianxian.demo.domain.model.query;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDate;
/**
* 用户查询对象
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Data
public class UserQuery implements Serializable {
/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* The Page num.
*/
Long pageNum=1L;
/**
* The Page size.
*/
Long pageSize=10L;
/**
* 用户名称
*/
private String userName;
/**
* 用户昵称
*/
private String nickName;
}
package com.duanledexianxianxian.demo.domain.model.vo;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 菜单vo对象
*
* @author duanledexianxianxian
* @since 2020-03-19
*/
@Data
public class MenuVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 菜单编号
*/
private Long menuId;
/**
* 菜单url
*/
private String menuUrl;
/**
* 菜单名称
*/
private String menuName;
/**
* 菜单编码
*/
private String menuCode;
/**
* 创建时间
*/
private LocalDateTime createTime;
}
package com.duanledexianxianxian.demo.domain.model.vo;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
/**
* 用户vo对象
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Data
public class UserDetailVO implements Serializable {
/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
private Long userId;
/**
* 用户名称
*/
private String userName;
/**
* 用户昵称
*/
private String nickName;
/**
* 年龄
*/
private Integer age;
/**
* 生日
*/
private LocalDate birthday;
/**
* The Menu list.
*/
private List<MenuVO> menuList;
}
package com.duanledexianxianxian.demo.domain.model.vo;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDate;
/**
* 用户vo对象
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Data
public class UserVO implements Serializable {
/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
private Long userId;
/**
* 用户名称
*/
private String userName;
/**
* 用户昵称
*/
private String nickName;
/**
* 年龄
*/
private Integer age;
/**
* 生日
*/
private LocalDate birthday;
}
package com.duanledexianxianxian.demo.service;
import com.duanledexianxianxian.demo.domain.dto.MenuDTO;
import com.duanledexianxianxian.demo.domain.entity.Menu;
import com.baomidou.mybatisplus.extension.service.IService;
import com.duanledexianxianxian.demo.domain.model.query.MenuForm;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* <p>
* 菜单表 服务类
* </p>
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
public interface IMenuService extends IService<Menu> {
/**
* Add menu long.
*
* @param form the form
* @return the long
*/
Long addMenu(MenuForm form);
/**
* Edit menu boolean.
*
* @param form the form
* @return the boolean
*/
Boolean editMenu(MenuForm form);
/**
* Delete menu boolean.
*
* @param menuId the menu id
* @return the boolean
*/
Boolean deleteMenu(Long menuId);
/**
* Gets user menu list.
*
* @param userId the user id
* @return the user menu list
*/
List<MenuDTO> getUserMenuList(Long userId);
}
package com.duanledexianxianxian.demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duanledexianxianxian.demo.dao.UserDao;
import com.duanledexianxianxian.demo.domain.dto.MenuDTO;
import com.duanledexianxianxian.demo.domain.dto.UserDTO;
import com.duanledexianxianxian.demo.domain.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import com.duanledexianxianxian.demo.domain.model.query.MenuForm;
import com.duanledexianxianxian.demo.domain.model.query.UserForm;
import com.duanledexianxianxian.demo.domain.model.query.UserQuery;
import java.util.List;
/**
* 用户接口
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
public interface IUserService extends IService<User> {
/**
* Add menu long.
*
* @param query the query
* @return the long
*/
IPage getUserListPage(UserQuery query);
/**
* Gets user menu list.
*
* @param userId the user id
* @return the user menu list
*/
UserDTO getUserDetail(Long userId);
/**
* Add menu long.
*
* @param form the form
* @return the long
*/
Long addUser(UserForm form);
/**
* Edit menu boolean.
*
* @param form the form
* @return the boolean
*/
Boolean editUser(UserForm form);
/**
* Delete menu boolean.
*
* @param userId the user id
* @return the boolean
*/
Boolean deleteUser(Long userId);
}
package com.duanledexianxianxian.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.duanledexianxianxian.demo.common.util.BeanUtils;
import com.duanledexianxianxian.demo.domain.dto.MenuDTO;
import com.duanledexianxianxian.demo.domain.entity.Menu;
import com.duanledexianxianxian.demo.dao.MenuDao;
import com.duanledexianxianxian.demo.domain.model.query.MenuForm;
import com.duanledexianxianxian.demo.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* 菜单服务实现类
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements IMenuService {
@Transactional(rollbackFor = Exception.class)
@Override
public Long addMenu(MenuForm form) {
// 做插入前检查
Menu menu = BeanUtils.copyObject(form, Menu.class);
this.baseMapper.insert(menu);
return menu.getMenuId();
}
@Transactional(rollbackFor = Exception.class)
@Override
public Boolean editMenu(MenuForm form) {
// 做编辑前检查
Menu menu = BeanUtils.copyObject(form, Menu.class);
int cnt = this.baseMapper.updateById(menu);
return SqlHelper.retBool(cnt);
}
@Transactional(rollbackFor = Exception.class)
@Override
public Boolean deleteMenu(Long menuId) {
// 做删除前检查
return SqlHelper.retBool(this.baseMapper.deleteById(menuId));
}
@Override
public List<MenuDTO> getUserMenuList(Long userId) {
return BeanUtils.copyList(this.baseMapper.selectList(new LambdaQueryWrapper<Menu>()
.eq(Menu::getUserId, userId)), MenuDTO.class);
}
}
package com.duanledexianxianxian.demo.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.duanledexianxianxian.demo.common.util.BeanUtils;
import com.duanledexianxianxian.demo.dao.MenuDao;
import com.duanledexianxianxian.demo.domain.dto.MenuDTO;
import com.duanledexianxianxian.demo.domain.dto.UserDTO;
import com.duanledexianxianxian.demo.domain.entity.Menu;
import com.duanledexianxianxian.demo.domain.entity.User;
import com.duanledexianxianxian.demo.dao.UserDao;
import com.duanledexianxianxian.demo.domain.model.query.UserForm;
import com.duanledexianxianxian.demo.domain.model.query.UserQuery;
import com.duanledexianxianxian.demo.service.IMenuService;
import com.duanledexianxianxian.demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
/**
* 用户服务接口实现类
*
* @author duanledexianxianxian
* @since 2020 -03-19
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {
@Autowired
IMenuService menuService;
@Override
public IPage getUserListPage(UserQuery query) {
IPage page = new Page(query.getPageNum(), query.getPageSize());
List<User> userList = this.baseMapper.getList(page, query);
page.setRecords(BeanUtils.copyList(userList, UserDTO.class));
return page;
}
@Override
public UserDTO getUserDetail(Long userId) {
User user = this.baseMapper.selectById(userId);
// 需要检查用户是否存在
UserDTO userDTO = BeanUtils.copyObject(user, UserDTO.class);
List<MenuDTO> menuDTOList = menuService.getUserMenuList(userId);
userDTO.setMenuList(menuDTOList);
return userDTO;
}
@Override
public Long addUser(UserForm form) {
// 做插入前检查
User user = BeanUtils.copyObject(form, User.class);
this.baseMapper.insert(user);
return user.getUserId();
}
@Override
public Boolean editUser(UserForm form) {
// 做编辑前检查
User user = BeanUtils.copyObject(form, User.class);
return SqlHelper.retBool(this.baseMapper.updateById(user));
}
@Override
public Boolean deleteUser(Long userId) {
// 做删除前检查
return SqlHelper.retBool(this.baseMapper.deleteById(userId));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.duanledexianxianxian.demo.dao.MenuDao">
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.duanledexianxianxian.demo.dao.UserDao">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.duanledexianxianxian.demo.domain.entity.User">
<id column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="nick_name" property="nickName"/>
<result column="age" property="age"/>
<result column="birthday" property="birthday"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
user_id, user_name, nick_name, age, birthday
</sql>
<select id="getList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from test_user
<where>
<if test="query.userName!=null and query.userName!=''">
and user_name like concat('%',#{query.userName})
</if>
<if test="query.nickName!=null and query.nickName!=''">
and nick_name like concat('%',#{query.nickName})
</if>
</where>
</select>
</mapper>
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<modules> <modules>
<module>demo</module> <module>demo</module>
<module>demo1</module>
</modules> </modules>
<parent> <parent>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment