mybtis 逆向
mbg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 7 <!-- 8 targetRuntime= 9 10 MyBatis3Simple:生成简单版的CRUD 11 MyBatis3:豪华版 12 --> 13 <context id="DB2Tables" targetRuntime="MyBatis3"> 14 <commentGenerator> 15 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 16 <property name="suppressAllComments" value="true" /> 17 </commentGenerator> 18 19 <!-- jdbcConnection:指定如何连接到目标数据库 --> 20 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 21 connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true" 22 userId="root" 23 password="000000"> 24 </jdbcConnection> 25 26 <!-- --> 27 <javaTypeResolver > 28 <property name="forceBigDecimals" value="false" /> 29 </javaTypeResolver> 30 31 <!-- javaModelGenerator:指定javaBean的生成策略 32 targetPackage="test.model":目标包名 33 targetProject="\MBGTestProject\src":目标工程 34 --> 35 <javaModelGenerator targetPackage="com.pojo" 36 targetProject=".\src"> 37 <property name="enableSubPackages" value="false" /> 38 <property name="trimStrings" value="true" /> 39 </javaModelGenerator> 40 41 <!-- sqlMapGenerator:sql映射生成策略: --> 42 <sqlMapGenerator targetPackage="com.mapper" 43 targetProject=".\src"> 44 <property name="enableSubPackages" value="false" /> 45 </sqlMapGenerator> 46 47 <!-- javaClientGenerator:指定mapper接口所在的位置 --> 48 <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" 49 targetProject=".\src"> 50 <property name="enableSubPackages" value="false" /> 51 </javaClientGenerator> 52 53 <!-- 指定要逆向分析哪些表:根据表要创建javaBean --> 54 <table schema="" tableName="employee"></table> 55 </context> 56 </generatorConfiguration>
测试代码:
1 @Test 2 public void testMbg() throws Exception { 3 List<String> warnings = new ArrayList<String>(); 4 boolean overwrite = true; 5 File configFile = new File("mbg.xml"); 6 ConfigurationParser cp = new ConfigurationParser(warnings); 7 Configuration config = cp.parseConfiguration(configFile); 8 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 9 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, 10 callback, warnings); 11 myBatisGenerator.generate(null); 12 }
生成包结构:
配置mybatis-conf.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties resource="dbconfig.properties"></properties> 7 <!-- <settings> 8 <setting name="mapUnderscoreToCamelCase" value="true"/> 9 <setting name="jdbcTypeForNull" value="NULL"/> 10 11 显式的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 12 <setting name="cacheEnabled" value="true"/> 13 <setting name="lazyLoadingEnabled" value="true"/> 14 <setting name="aggressiveLazyLoading" value="false"/> 15 </settings> --> 16 <!-- <typeAliases> 17 <package name="com.pojo"/> 18 </typeAliases> --> 19 <environments default="_mysql"> 20 <environment id="_mysql"> 21 <transactionManager type="JDBC"></transactionManager> 22 <dataSource type="POOLED"> 23 <property name="driver" value="${jdbc.driver}" /> 24 <property name="url" value="${jdbc.url}" /> 25 <property name="username" value="${jdbc.username}" /> 26 <property name="password" value="${jdbc.password}" /> 27 </dataSource> 28 </environment> 29 </environments> 30 <mappers> 31 <!-- 扫描mapper包 --> 32 <package name="com.mapper"/> 33 </mappers> 34 </configuration>
测试类:
1 package test; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import org.apache.ibatis.io.Resources; 10 import org.apache.ibatis.session.SqlSession; 11 import org.apache.ibatis.session.SqlSessionFactory; 12 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 13 import org.junit.Test; 14 import org.mybatis.generator.api.MyBatisGenerator; 15 import org.mybatis.generator.config.Configuration; 16 import org.mybatis.generator.config.xml.ConfigurationParser; 17 import org.mybatis.generator.internal.DefaultShellCallback; 18 import com.mapper.EmployeeMapper; 19 import com.pojo.Employee; 20 import com.pojo.EmployeeExample; 21 import com.pojo.EmployeeExample.Criteria; 22 23 24 25 public class MyBatisTest { 26 27 public SqlSessionFactory getSqlSessionFactory() throws IOException { 28 String resource = "mybatis-config.xml"; 29 InputStream inputStream = Resources.getResourceAsStream(resource); 30 return new SqlSessionFactoryBuilder().build(inputStream); 31 } 32 33 @Test 34 public void testMbg() throws Exception { 35 List<String> warnings = new ArrayList<String>(); 36 boolean overwrite = true; 37 File configFile = new File("mbg.xml"); 38 ConfigurationParser cp = new ConfigurationParser(warnings); 39 Configuration config = cp.parseConfiguration(configFile); 40 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 41 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, 42 callback, warnings); 43 myBatisGenerator.generate(null); 44 } 45 46 @Test 47 public void testInsert() throws IOException{ 48 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 49 SqlSession openSession = sqlSessionFactory.openSession(); 50 try{ 51 EmployeeMapper employeeMapper = openSession.getMapper(EmployeeMapper.class); 52 Employee employee = new Employee(5,"EEE","EEE"); 53 int i = employeeMapper.insert(employee); 54 System.out.println(i); 55 openSession.commit(); 56 }finally{ 57 openSession.close(); 58 } 59 } 60 @Test 61 public void testSelect() throws IOException{ 62 63 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 64 SqlSession sqlSession = sqlSessionFactory.openSession(); 65 try { 66 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); 67 EmployeeExample employeeExample = new EmployeeExample(); 68 Criteria criteria = employeeExample.createCriteria(); 69 criteria.andIdEqualTo(1); 70 List<Employee> employees = employeeMapper.selectByExample(employeeExample); 71 for (Employee employee : employees) { 72 System.out.println(employee.toString()); 73 } 74 } finally { 75 sqlSession.close(); 76 } 77 78 } 79 @Test 80 public void testUpdata() throws IOException { 81 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 82 SqlSession sqlSession = sqlSessionFactory.openSession(); 83 try { 84 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); 85 EmployeeExample employeeExample = new EmployeeExample(); 86 // 1. 87 Employee employee = employeeMapper.selectByPrimaryKey(1); 88 employee.setGender("acd"); 89 employeeMapper.updateByPrimaryKey(employee); 90 // 2. 91 //如果传入字段不空为才更新,在批量更新中使用此方法,不需要先查询再更新 92 //itemsMapper.updateByPrimaryKeySelective(record); 93 sqlSession.commit(); 94 } finally { 95 sqlSession.close(); 96 } 97 } 98 @Test 99 public void testDelete() throws IOException { 100 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 101 SqlSession sqlSession = sqlSessionFactory.openSession(); 102 try { 103 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); 104 EmployeeExample employeeExample = new EmployeeExample(); 105 106 employeeMapper.deleteByPrimaryKey(5); 107 sqlSession.commit(); 108 } finally { 109 sqlSession.close(); 110 } 111 } 112 }