mybatis 3.5.0版本(四)
阿新 • • 發佈:2019-04-18
逆向工程
導包
建立 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> <context id="mysqlTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8" userId="root" password="root123"> </jdbcConnection> <!-- java型別解析 --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.company.model" targetProject="./src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="com.company.mapper" targetProject="./src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.company.mapper" targetProject="./src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="user" domainObjectName="User"></table> <table tableName="orderdetail" domainObjectName="OrderDetail"></table> </context> </generatorConfiguration>
執行程式碼
List<String> warnings = new ArrayList<>(); boolean overwrite = true; File configFile = new File("src/generator.xml"); ConfigurationParser configurationParser = new ConfigurationParser(warnings); Configuration configuration = configurationParser.parseConfiguration(configFile); DefaultShellCallback defaultShellCallback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, defaultShellCallback, warnings); myBatisGenerator.generate(null);
自動生成的檔案
測試
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
UserExample userExample = new UserExample();
Criteria c = userExample.createCriteria();
c.andNameLike("%張%");
List<Items> list = userMapper.selectByExample(userExample);