MyBatis的逆向工程
逆向工程
1.导入jar包
2.generator.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="F:/eclipse-p/eclipse/mysql-connector-java-5.1.45.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ban32" userId="root" password="199617"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.zhiyou.han.bean" targetProject=".\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.zhiyou.han.mapper" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou.han.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="user" domainObjectName="User" enableUpdateByExample="false" enableCountByExample="false" enableSelectByExample="true" enableDeleteByExample="false" > <!--enableUpdateByExample="false" enableCountByExample="false" enableSelectByExample="false" enableDeleteByExample="false" 设置后会生成相关的封装的方法,例如模糊查询,不需要再xml里面进行大量的环境配置 --> </table> </context> </generatorConfiguration>
3.测试类函数
package com.zhiyou.han; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; 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 org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; public class GeneratorTest { @Test public void show() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("generator.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); System.out.println("=============>OVER"); } }
本文来自博客园踩坑狭,作者:韩若明瞳,转载请注明原文链接:https://www.cnblogs.com/han-guang-xue/p/9757355.html