Mybatis Generator(逆向工程)
编程的一部分意义就在于消除人类的重复劳动,所以重复的编程天然就不该存在;
mybatis的逆向工程解决了利用ssm框架编写Javaweb项目时每次都要要机械重复的编写pojo、dao、mapper的问题。
mybatis逆向工程通过简单的配置就可以快速生成我们每次新建ssm项目时pojo、dao、mapper层的通用基础模板;通过简单的改写,我们可以就快速进入业务编写的工作;
重要提示:使用Mybats Generator时,mysql-connector-java版本不能用太新的,否则会出现错误,下面版本测试通过;
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency>
使用
-
首先引入所需jar包
maven项目在pom文件中引入:
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency>
普通web项目可以去这里下载所需jar包;
-
编写配置文件
配置文件的编写,及关于逆向工程的其它问题可以参考Mybatis Generator的官方文档;下面摘取介绍一下常用配置(完整xml在文末)
首先配置所连数据库
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/testdatabase" userId="username" password="password"> </jdbcConnection>
配置与数据库中单表所对应的Java对象(po、domain)层
这层怎么取名的都有,不止我上面及下面列出来的这几个,看个人习惯吧,下面是阿里发布的Java开发手册中的建议;
• DO(Data Object):此对象与数据库表结构一一对应,通过 DAO 层向上传输数据源对象。
• DTO(Data Transfer Object):数据传输对象,Service 或 Manager 向外传输的对象。
• BO(Business Object):业务对象,由 Service 层输出的封装业务逻辑的对象。
• AO(Application Object):应用对象,在 Web 层与 Service 层之间抽象的复用对象模型,极为贴
近展示层,复用度不高。
• VO(View Object):显示层对象,通常是 Web 向模板渲染引擎层传输的对象。
• Query:数据查询对象,各层接收上层的查询请求。注意超过 2 个参数的查询封装,禁止使用 Map 类
来传输。<!--./src/main/java 表示当前项目下的src/main/java(maven项目的默认结构),targetPackage则是指定包名 --> <javaModelGenerator targetPackage="hoimsys.pojo" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator>
配置dao层接口的生成位置
<javaClientGenerator type="XMLMAPPER" targetPackage="hoimsys.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator>
配置mapper文件生成位置
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator>
配置每个表的生成策略
<!-- 简单配置一下表名与生成类名就能用 --> <table tableName="department" domainObjectName="Department" ></table>
至此,在配置文件中配置这么多就可以用了,随便在那个测试中运行以下代码,刷新项目即可真香;
public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("MBG.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); }
一些建议的可选配置
<!-- 配置这个设置后生成的代码就会清爽很多(就不会生成那些看不懂的英文注释了...) --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 生成toString方法) --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 不想要example类以及相关内容可以在table中配置以下属性 --> <table tableName="department" domainObjectName="Department" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
最后
如果有N张表,就会生成2N个PO,N个mapper.java以及N个mapper.xml,因为它除了常规的PO之外还生成了用于设置条件的xxxExample,不喜欢用删了就好,没事最好还是研究一下怎么用;
以下测试MBG.xml文件
<?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="DB2Tables" targetRuntime="MyBatis3"> <!-- 生成toString方法) --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <commentGenerator> <!-- 配置这个设置后生成的代码就会清爽很多(就不会生成那些看不懂的英文注释了...) --> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/hoimsys" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--./src/main/java 表示当前项目下的src/main/java(maven项目的默认结构),targetPackage则是指定包名 --> <javaModelGenerator targetPackage="hoimsys.pojo" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="hoimsys.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 简单配置一下表名与生成类名就能用 --> <table tableName="department" domainObjectName="Department"></table> <table tableName="doctor" domainObjectName="Doctor"></table> <table tableName="medicine" domainObjectName="Medicine"></table> <table tableName="patient" domainObjectName="Patient"></table> <table tableName="prescription" domainObjectName="Prescription"></table> <table tableName="registration" domainObjectName="Registration"></table> <table tableName="storage" domainObjectName="Storage"></table> <table tableName="title" domainObjectName="Title"></table> </context> </generatorConfiguration>