MybatisPlus 自动生成(部份表生成)+打包发布阿里云

    技术2022-07-12  64

    根据数据表自动生成实体类,Mapper接口,Service,ServiceImpl(Service实体类),Controller

    1  在pom中导入依赖   MybatisPlus Genetator  然后再导入一个模板

    一般模板有三个  默认使用velocity  除此之外还有FreeMarker  Beetl

    <dependency><!-- 加入mybatisplus Generator的依赖--> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.1.tmp</version> </dependency> <dependency><!-- 加入模板的依赖--> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency>

     2 启动类

    写一个main 函数  然后配置好代码  运行就会生成了          然后还需要再application中加上@MapperScan("包名")  否则后面mapper注入不进来   

    //创建Generator 对象 AutoGenerator autoGenerator = new AutoGenerator(); //数据源 DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL); //如果是远端的数据库 需要把localhost改成ip地址就好了 dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"); dataSourceConfig.setUsername("root"); dataSourceConfig.setPassword("admin"); dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver"); autoGenerator.setDataSource(dataSourceConfig); //全局配置 GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");//这样会拿到工程的绝对路径 //System.getProperty("user.dir") 就是当前工程的绝对目录 globalConfig.setOpen(false);//创建好工程之后不会自动打开 globalConfig.setAuthor("zf");// globalConfig.setServiceName("%sService");//加上这句代码 在生成Service类的时候就不会默认前面有I了 autoGenerator.setGlobalConfig(globalConfig); //包信息 PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent("com.nange.mybatisplus"); packageConfig.setModuleName("generator"); packageConfig.setController("controller"); packageConfig.setService("service"); packageConfig.setServiceImpl("servicce.impl"); packageConfig.setMapper("mapper"); packageConfig.setEntity("entity"); autoGenerator.setPackageInfo(packageConfig); //配置策略 //生成部份表的设置在这里 StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setInclude("user",“table”);//只会生成user和table这两个表 strategyConfig.setEntityLombokModel(true);//实体类生成之后自动添加lombok注解 strategyConfig.setNaming(NamingStrategy.underline_to_camel); strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);//将数据库中带下划线的转成驼峰规则 autoGenerator.setStrategy(strategyConfig); autoGenerator.execute();//配置完之后运行

    3然后直接再Controller中调用

    public ModelAndView index(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("list",userService.list()); return modelAndView; }

    然后需要写一个html

    <!DOCTYPE html> <html lang="en"> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> <tr th:each="user:${list}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> </tr> </table> </body> </html>

    然后启动   就可以在8080端口访问了

     

     

    打包发布阿里云

    在右侧MAVEAN中找到这个package双击就可以打包了

    将打包好的jar包  传至云服务器就可以访问了

     

     

     

    Processed: 0.013, SQL: 9