Mybatis-Generator逆向工程,简单策略
1.下载generator包
https://github.com/mybatis/generator/releases
mybatis-generator-core-1.3.6.zip
官网下载即可
2.编写generator运行的xml文件
只做了简单的基本配置,如果需要更加复杂的配置,请参阅官方文档
http://www.mybatis.org/generator/configreference/context.html
注意:这个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> <!-- 这个配置文档可以参考官网:http://www.mybatis.org/generator/configreference/context.html --> <!-- 引入zip包,我们用解压后的jar包,故舍弃这种方式 --> <!-- <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" /> --> <!-- context:指定generator运行环境 targetRuntime -MyBatis3:复杂版的mapper -MyBatis3Simple:简单版的mapper --> <context id="DB2Tables" targetRuntime="MyBatis3Simple"> <!-- 数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/beijing" userId="root" password="root"> </jdbcConnection> <!-- 类型解析器,可以注掉(默认) --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- javaBean的生成策略 targetPackage:javaBean的生成路径 targetProject:目标工程 --> <javaModelGenerator targetPackage="com.Entity" targetProject=".\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- mapper映射文件生成地址 targetPackage:mapper的生成路径 --> <sqlMapGenerator targetPackage="com.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:客户端代码生成器位置,指定mapper接口所在位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定数据库中的表 简单的配置一下 - tableName:表名 - domainObjectName:生成的Bean类名 - 可以有多个table --> <table tableName="members" domainObjectName="Members"/> <!-- <table tableName="books" domainObjectName="Books"/> --> </context> </generatorConfiguration>
3.生成代码
下面代码官方文档都有
贴出地址
http://www.mybatis.org/generator/running/runningWithJava.html
// 逆向工程自动生成 public static void generatorAuto() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("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); }
4.工程目录结构
5.测试结果
public static void main(String[] args) throws Exception { SqlSession session = getSqlSession(); try { // 面向接口方式 MembersMapper mapper = session.getMapper(MembersMapper.class); // 测试返回list集合 List<Members> list = mapper.selectAll(); for (Members member : list) { System.out.println(member); } } finally { // 关闭session session.close(); } // generatorAuto(); }
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@224aed64] ==> Preparing: select id, member_name, age, gender, email from members ==> Parameters: <== Columns: id, member_name, age, gender, email <== Row: 1, jack, 25, 1, jack@beijing.com <== Row: 2, pnoy, 26, 0, pnoy@beijing.com <== Total: 2 com.Entity.Members@7ac7a4e4 com.Entity.Members@6d78f375
ok,success~