根据表关系创建实体,mapper,service等

package com.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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 java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class MyBatisPlusGenerator {
    public static void main(String[] args) throws SQLException {

        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);

        //1. 全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(true) // 是否支持AR模式
                .setAuthor("XX") // 作者
                //.setOutputDir("D:\\workspace_mp\\mp03\\src\\main\\java") // 生成路径
                .setOutputDir(projectPath+"/project/src/main/java") // 生成路径
                .setFileOverride(true)  // 文件覆盖
                .setIdType(IdType.AUTO) // 主键策略
                .setServiceName("%sService")  // 设置生成的service接口的名字的首字母是否为I
                .setBaseResultMap(true)//生成基本的resultMap
                .setBaseColumnList(true)
//                .setSwagger2(true)
                ;//生成基本的SQL片段

        //2. 数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)  // 设置数据库类型
                .setDriverName("com.mysql.jdbc.Driver")
                .setUrl("数据库地址/库")
                .setUsername("root")
                .setPassword("123456");

        //3. 策略配置globalConfiguration中
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true) //全局大写命名
                .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
//                .setTablePrefix("grading_")
                .setInclude(
                        "table1",
                        "table2"
                );  // 生成的表



        //4. 包名策略配置
        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent("com.mybatis.unit")
                .setMapper("mapper")//dao
                .setService("service")
//                .setController("controller")//controller
                .setEntity("model")
                .setXml("mapper");//mapper.xml

        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
//                Map<String, Object> map = new HashMap<String, Object>();
//                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
//                this.setMap(map);
            }
        };
        //自定义文件输出位置(非必须)
        List<FileOutConfig> fileOutList = new ArrayList();
        fileOutList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/project/src/main/resources/mybatis/" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        fileOutList.add(new FileOutConfig("/templates/entity.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/project/src/main/java/com/mybatis/api/model/po/" + tableInfo.getEntityName() + ".java";
            }
        });
        injectionConfig.setFileOutConfigList(fileOutList);

        //5. 整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(config)
            .setDataSource(dsConfig)
            .setStrategy(stConfig)
            .setPackageInfo(pkConfig)
            .setCfg(injectionConfig)
            .setTemplate(// 关闭默认 xml 生成,调整生成 至 根目录
                    new TemplateConfig().setXml(null).setController(null).setEntity(null)
            );

        //6. 执行
        ag.execute();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于MySQL的多更新,你可以通过使用Mapper、Entity和Service来实现。下面是一种常见的实现方式: 首先,在Mapper中定义多更新的SQL语句。例如,假设你有两张:TableA和TableB,你想要根据某个条件更新它们的数据。你可以在Mapper中定义一个更新语句,类似于下面的示例: ```java @Mapper public interface YourMapper { @Update("UPDATE table_a a, table_b b SET a.column1 = #{value1}, b.column2 = #{value2} WHERE a.id = b.id AND a.condition = #{condition}") void updateTables(@Param("value1") String value1, @Param("value2") String value2, @Param("condition") String condition); } ``` 接下来,在Entity中定义对应的结构。你需要创建两个实体类,分别对应TableA和TableB的结构。 ```java public class TableA { private Integer id; private String column1; // 其他属性及其对应的getter和setter方法 } public class TableB { private Integer id; private String column2; // 其他属性及其对应的getter和setter方法 } ``` 然后,在Service中编写业务逻辑代码。你可以注入Mapper,并在Service方法中调用Mapper的更新方法。 ```java @Service public class YourService { private final YourMapper yourMapper; public YourService(YourMapper yourMapper) { this.yourMapper = yourMapper; } public void updateTables(String value1, String value2, String condition) { yourMapper.updateTables(value1, value2, condition); } } ``` 最后,你可以在需要的地方调用Service方法来进行多更新操作。 ```java @Service public class YourController { private final YourService yourService; public YourController(YourService yourService) { this.yourService = yourService; } public void updateTables() { String value1 = "new value 1"; String value2 = "new value 2"; String condition = "some condition"; yourService.updateTables(value1, value2, condition); } } ``` 这样,你就可以使用Mapper、Entity和Service来实现MySQL的多更新操作了。当然,具体的代码实现可能会根据你的需求和框架有所不同,以上只是一个简单的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值