MaBatis逆向工程的使用(非插件方式)
作者:互联网
一、概述
MyBatis是目前流行的优秀持久层框架,其逆向工程更是大大缩减了开发时间。所谓逆向工程,指的是mybatis根据数据库设计好的表,自动生成对应model、mapper及mapper.xml,本文是以非插件的方式进行逆向工程。
二、手工代码
本文采用IDEA结合Maven实现逆向工程,工程结构如下:
(1)入口类
package com.practice.common.generate; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.util.ArrayList; import java.util.List; /** * @ProjectName: profile-demo * @Package: com.pds.common.generate * @ClassName: Generator * @Author: dong * @Description: ${description} * @Date: 2019/9/10 15:45 * @Version: 1.0 */ public class Generator { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(Generator.class.getResourceAsStream("/generator/generatorConfig.xml")); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }
(2)逆向工程配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="MySQL2" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="javaFileEncoding" value="UTF-8" /> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="com.practice.common.base.BaseMapper" /> <property name="caseSensitive" value="true" /> <!--<property name="beginningDelimiter" value="`"/> --> <!--<property name="endingDelimiter" value="`"/> --> </plugin> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.229.132:3306/test_db" userId="root" password="root"> </jdbcConnection> <javaModelGenerator targetPackage="com.practice.common.model" targetProject="student-demo-common-base/src/main/java" /> <sqlMapGenerator targetPackage="commonmappers" targetProject="student-demo-common-base/src/main/resources" /> <javaClientGenerator targetPackage="com.practice.common.mapper" targetProject="student-demo-common-base/src/main/java" type="XMLMAPPER" /> <table tableName="student"> <generatedKey column="name" sqlStatement="JDBC" /> </table> </context> </generatorConfiguration>
(3)运行逆向工程入口类即可
三、总结
本文是以非插件的方式进行逆向工程,在pom文件也有对应插件逆向工程配置,代码已经上传至:https://github.com/Simple-Coder/student-demo/tree/master/student-demo-common-base
标签:MaBatis,逆向,插件,工程,generator,mybatis,import 来源: https://www.cnblogs.com/rmxd/p/11508293.html