栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus-generator

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus-generator

mven依赖

		
       		
            com.baomidou
            mybatis-plus-boot-starter
            3.3.1.tmp
mybatis-plus-generator逆向工程代码自动生成

官网:https://baomidou.com/pages/d357af/#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B

新建一个springboot项目:

maven依赖:
 
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.3.1.tmp
    
    
    
        com.baomidou
        mybatis-plus-generator
        3.3.1.tmp
    
    
    
        org.freemarker
        freemarker
    
    
    
        mysql
        mysql-connector-java
        runtime
    
创建目录:

导入代码:
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
	
	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.isNotBlank(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "!");
	}

	public static void main(String[] args) {
		// 代码生成器
		AutoGenerator mpg = new AutoGenerator();
		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
         //输出目录
		gc.setOutputDir(projectPath + "/yeb-generator/src/main/java");
		gc.setAuthor("crabin");
		gc.setOpen(false);
		//实体属性 Swagger2 注解
		gc.setSwagger2(true);
		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:mysql://localhost:3306/yeb?useUnicode=true&useSSL=false&characterEncoding=utf8");
		// dsc.setSchemaName("public");
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("lpb062700");
		mpg.setDataSource(dsc);
// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.crabin")
				.setEntity("pojo")
				.setMapper("mapper")
				.setService("service")
				.setServiceImpl("service.impl")
				.setController("controller");
		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 focList = new ArrayList<>();
		// 自定义配置会被优先输出
		focList.add(new FileOutConfig(templatePath) {
			@Override
			public String outputFile(TableInfo tableInfo) {
				// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
				return projectPath + "/yeb-generator/src/main/resources/mapper/"
						+ tableInfo.getEntityName() + "Mapper"
						+ StringPool.DOT_XML;
			}
		});
        
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);

		// 配置模板
		TemplateConfig templateConfig = new TemplateConfig();

		// 配置自定义输出模板
		//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
		// templateConfig.setEntity("templates/entity2.java");
		// templateConfig.setService();
		// templateConfig.setController();

		templateConfig.setXml(null);
		mpg.setTemplate(templateConfig);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.no_change);
//		strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
		//lombok模型
		strategy.setEntityLombokModel(true);
		//生成 @RestController 控制器
		strategy.setRestControllerStyle(true);
		// 公共父类
//		strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
		// 写于父类中的公共字段
		strategy.setSuperEntityColumns("id");
		strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
		strategy.setControllerMappingHyphenStyle(true);
		//表前缀
		strategy.setTablePrefix("t_");

		mpg.setStrategy(strategy);
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();
	}

}

生成:

MybatisPlus设置分页 在配置文件中:MybatisPlusConfig 开启mybatis-plusMybatis分页配置

@EnableTransactionManagement // 开启事务管理
@MapperScan("com.abin.mapper") // 扫描mapper接口
@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

}

分页返回类 RespPageBean
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespPageBean implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private Long total;

    
    private List data;
}
controller
	//currentPage:当前页,size:每页大小
    @GetMapping("/")
    public RespPageBean employeePage(@RequestParam(defaultValue = "1") Integer currentPage,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     ) {

        return employeeService.getEmployeePage(currentPage, size );
    }
serviceImpl
	@Override
    public RespPageBean getEmployeePage(
        Integer currentPage, Integer size ) {
        
        Page page = new Page<>(currentPage,size);
        IPage employeeIPage = employeeMapper.getEmployeeByPage(page);
        RespPageBean respPageBean= new RespPageBean(employeeIPage.getTotal(),employeeIPage.getRecords());
        return respPageBean;
    }
Mapper
//可以传入Employee进行查询并返回
IPage getEmployeeByPage(Page page);
****Mapper.xml
  
        SELECT e.*,n.id as nid,n.`name` as nname, p.id as pid,p.`name` as pname,
        d.id as did,d.`name` as dname,j.id as jid,j.`name` as jname,pos.id as posid,
        pos.`name` as posname
      
        FROM
        t_employee e,t_nation n,t_politics_status p,
        t_department d,t_joblevel j,t_position pos
      
        WHERe
        e.nationId = n.id
        AND e.politicId = p.id
        AND e.departmentId = d.id
        AND e.jobLevelId = j.id
        AND e.posId = pos.id
      
        ORDER BY e.id
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号