懶人必備《mybatis-plus》

精句:孩子是祖國的未來!

一、目錄

1.1接入mybatis-plus

1.2進階mybatis-plus(多數據源)

二、接入,mybatis-plus

2.1、引入maven

<code>
    com.baomidou
    mybatis-plus-boot-starter
    3.3.1

/<code>

2.2、引入代碼生成工具類

<code>ublic class CodeGenerator {

    public static void main(String[] args) throws InterruptedException {
        AutoGenerator mpg = new AutoGenerator();

        /**
         * 全局配置生成文件的基礎參數
         *
         */
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二級緩存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setOpen(false);
        gc.setAuthor("Code-VS-Life");
        /**
         * 自定義文件命名,注意 %s 會自動填充表實體屬性!
         */
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 數據源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType( DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/user-db?useSSL=false&serverTimezone=Asia/Shanghai");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.gf");
        pc.setController( "controller");
        pc.setEntity( "entity" );
        //pc.setModuleName("test");
        mpg.setPackageInfo(pc);

        // 自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定義輸入文件名稱
           String path =  projectPath + "/src/main/resources/mapper/"
                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
           System.err.print("=================path:"+path);
                return path;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //此處可以修改為您的表前綴
        strategy.setTablePrefix(new String[] { "tb_"});
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 需要生成的表
        strategy.setInclude(new String[] { "user_info","tb_user_info" });
        // 排除生成的表
        //strategy.setExclude(new String[]{"test"});
        strategy.setEntityLombokModel( true );

        mpg.setStrategy(strategy);

        // 執行生成
        mpg.execute();
    }
/<code>

現在的工程結構:


懶人必備《mybatis-plus》

mybatis-plus

2.3、運行工具類main方法(類似逆向工程)


懶人必備《mybatis-plus》

運行之後

2.4、生成後的類

(1)實體類

<code>/**
 * 

* *

* * @author Code-VS-Life * @since 2020-05-07 */ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("tb_user_info") public class UserInfo extends Model { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.AUTO) private Integer id; private String name; private Integer age; @Override protected Serializable pkVal() { return this.id; } }/<code>

(2)生成Mapper類

<code>import com.example.demo.entity.UserInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * 

* Mapper 接口 *

* * @author Code-VS-Life * @since 2020-05-07 */ public interface UserInfoMapper extends BaseMapper { }/<code>

XML文件

<code>


     
    
        
        
        
    
     
    
        id, name, age
    

/<code>

(3)生成的ServiceImpl類

<code>/**
 * 

* 服務實現類 *

* * @author Code-VS-Life * @since 2020-05-07 */ @Service public class UserInfoServiceImpl extends ServiceImpl implements UserInfoService { }/<code>

Service接口類

<code>/**
 * 

* 服務類 *

* * @author Code-VS-Life * @since 2020-05-07 */ public interface UserInfoService extends IService { }/<code>

(4)生成的Controller

<code>/**
 * 

* 前端控制器 *

* * @author Code-VS-Life * @since 2020-05-07 */ @Controller @RequestMapping("/userInfo") public class UserInfoController { @Autowired private UserInfoService userInfoService; @GetMapping("/user/add") public Object getUserInfo(){ UserInfo userInfo = new UserInfo(); userInfoService.save(userInfo); return "success"; } }/<code>

2.5、mybatis-plus自帶的內置方法,開箱即用。


懶人必備《mybatis-plus》

mybatis-plus


分享到:


相關文章: