一、什么是逆向工程
MyBatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java. po.) 。一般在开发中,常用的逆向工程方式是通过数据库的表生成代码。
通过表生成Mapper,mapper.xml,domain
二、使用
下载地址:
http://download.csdn.net/download/monkey_wen/10020047
创建项目,并导入以下三个包:mybatis-3.2.7.jar、mybatis-generator-core-1.3.2.jar、mysql-connector-java-5.1.25-bin.jar
创建generatorConfig.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> <!-- 数据库驱动--> <classPathEntry location="WebContent/WEB-INF/lib/mysql-connector-java-5.1.25-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test1" userId="root" password="123456"> </jdbcConnection> <!-- 处理oracle里面的number类型的 BigDecimal price === oracle num(10,2) --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.gx.domain" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="com.gx.mapping" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成Mapper的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.gx.mapper" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- 可添加多张表,如上 --> <!-- java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite --> </context> </generatorConfiguration>
创建GeneratorSqlmap.java去生成
public class GeneratorSqlmap { public static void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("config/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); } public static void main(String[] args) { try { generator(); System.out.println("生成成功,请刷新"); } catch (Exception e) { e.printStackTrace(); } } }
效果:
三、把文件生成到其他项目里面
<?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> <!-- 数据库驱动 --> <classPathEntry location="lib/mysql-connector-java-5.1.25-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/0412user" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 实体 targetProject:生成的源代码放到位置 可以是相对位置 也可以绝对位置 targetProject=‘src’指本项目的src --> <javaModelGenerator targetPackage="com.sxt.domain" targetProject="D:\Workspaces\ADV0412_mybatis\13_mybtais_erp\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置 生成xml的位置--> <sqlMapGenerator targetPackage="com.sxt.mapping" targetProject="D:\Workspaces\ADV0412_mybatis\13_mybtais_erp\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成Mapper的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.sxt.mapper" targetProject="D:\Workspaces\ADV0412_mybatis\13_mybtais_erp\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 --> <table tableName="sys_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="sys_dept" domainObjectName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="sys_emp" domainObjectName="Emp" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
提示:把table里面的所有属性改成true之生生成的代码,不推荐,会生成一个UserExample的实体
public interface UserMapper { //查询 满足example条件的数据总条数 int countByExample(UserExample example); int deleteByExample(UserExample example); int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); //查询 满足example条件的数据 List<User> selectByExample(UserExample example); User selectByPrimaryKey(Integer id); //批量选择更新 满足example条件的数据总条数 int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); //批量全部更新 满足example条件的数据总条数 int updateByExample(@Param("record") User record, @Param("example") UserExample example); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }
原文:https://www.cnblogs.com/97guoxiang/p/12776147.html