使用逆向工程進行快速開發
阿新 • • 發佈:2020-07-09
逆向工程的使用
表(資料庫)→實體類Student、StudentMapper.java、studentMapper.xml
1.新增jar包
mybatis-3.5.1.jar
mybatis-generator-core-1.3.5.jar
ojdbc7-12.1.0.2.jar
2.xml模板檔案(修改生成路徑、表名)
src\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="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <!-- suppressAllComments屬性值: true:自動生成實體類、SQL對映檔案時沒有註釋true:自動生成實體類、SQL對映檔案,並附有註釋 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 資料庫連線資訊 --> <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:mldn" userId="scott" password="tiger"> </jdbcConnection> <!-- forceBigDecimals屬性值: true:把資料表中的DECIMAL和NUMERIC型別, 解析為JAVA程式碼中的java.math.BigDecimal型別 false(預設):把資料表中的DECIMAL和NUMERIC型別, 解析為解析為JAVA程式碼中的Integer型別 --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject屬性值:實體類的生成位置 targetPackage屬性值:實體類所在包的路徑 --> <javaModelGenerator targetPackage="org.myy.entity" targetProject=".\src"> <!-- trimStrings屬性值: true:對資料庫的查詢結果進行trim操作 false(預設):不進行trim操作 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject屬性值:SQL對映檔案的生成位置 targetPackage屬性值:SQL對映檔案所在包的路徑 --> <sqlMapGenerator targetPackage="org.myy.mapper" targetProject=".\src"> </sqlMapGenerator> <!-- 生成動態代理的介面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="org.myy.mapper" targetProject=".\src"> </javaClientGenerator> <!-- 指定資料庫表 --> <table tableName="Studen t1"> </table> <table tableName="studentCard"> </table> <table tableName="studentClass"> </table> </context> </generatorConfiguration>
3.根據java模板類 一鍵生成
表(資料庫)→實體類Student、Mapper介面StudentMapper.java、studentMapper.xml
4.如何使用
增加mybatis配置檔案:conf.xml等
對於like模糊查詢,逆向工程需要在傳值時,寫入criteria.andStunameLike("%z%");
src\org\myy\test\TestGeneratorDemo.java
package org.myy.test; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.myy.entity.Student1; import org.myy.entity.Student1Example; import org.myy.mapper.Student1Mapper; import java.io.IOException; import java.io.Reader; import java.util.List; public class TestGeneratorDemo { public static void main(String[] args) throws IOException { // Connection - SqlSession操作Mybatis // conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); // reader->sqlSession // 可以通過build的第二引數 指定資料庫環境 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "devOracle"); SqlSession session = sessionFactory.openSession(); Student1Mapper mapper = session.getMapper(Student1Mapper.class); //Example:查詢條件 //where (xx=xx and xx=xx) or (xx=xx and xx=xx) //where stuname like '%z%' Student1Example student1Example=new Student1Example(); Student1Example.Criteria criteria=student1Example.createCriteria(); //criteria.andStunoBetween((short)1, (short) 4); criteria.andStunameLike("%z%"); //or //stuno<=4 and graname like '%d%' Student1Example.Criteria criteria1=student1Example.createCriteria(); criteria1.andStunoLessThanOrEqualTo((short) 4);//<= criteria1.andGranameLike("%d%"); //quert by Criteria,QBC student1Example.or(criteria1); List<Student1> student1s = mapper.selectByExample(student1Example); System.out.println(student1s); session.close(); } }