Mybatis逆向工程
Mybatis逆向工程
介绍: MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
当数据库表比较多的时候,重复的创建pojo对象和简单的数据库表的(CRUD)操作的mapper,效率低,官方给出了使用mybatis Generator用来根据数据库表逆向生成pojo和mapper文件,极大的方便开发。
IDEA实现Mybatis逆向工程
加入maven依赖
<!--mybatis逆向工程插件-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<!-- 日志依赖 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>generatorConfig.xml 配置
<generatorConfiguration>
<!-- 数据库驱动-->
<!--<classPathEntry location=""/>-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&serverTimezone=UTC&characterEncoding=utf8" userId="root" password="33333333">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="Mapper" targetProject=".\src\main\java\dao">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO接口的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!--这里是个坑,不要用user(不要使用任何关键字!),他会读取MySQL中的user表!!!!-->
<table tableName="dept"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
domainObjectName="Dept" ></table>
<table tableName="emp"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
domainObjectName="Emp" ></table>
</context>
</generatorConfiguration>编写生成方法
public void test01() throws IOException, XMLParserException, SQLException, InterruptedException, InvalidConfigurationException {
List warnings = new ArrayList();
boolean overwrite = true;
//逆向工程的配置文件路径
File configFile = new File("D:\\JAVA学习\\第一个ssm项目\\src\\main\\resources\\generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}最后看看效果截图吧
总结 mybatis逆向工程可以很快捷的生成实体类和dao层模块和mapper.xml文件 极大地增加了开发人员的开发效率
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 风离の博客!






