mybatis-8-逆向工程
阿新 • • 發佈:2021-09-01
MyBatis Generator簡稱MBG
流程:
匯入依賴:
<!--MBG逆向工程-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
再resource下建立MBG配置檔案mbg.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> <!-- 是否去除自動生成的註釋 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- Mysql資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123456"> </jdbcConnection> <!-- Oracle資料庫 <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 預設為false,把JDBC DECIMAL 和NUMERIC型別解析為Integer,為true時 把JDBC DECIMAL 和NUMERIC型別解析為java.math.BigDecimal --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成POJO類的位置 --> <javaModelGenerator targetPackage="com.wang.entity" targetProject=".\src\main\java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper對映檔案生成的位置 --> <sqlMapGenerator targetPackage="com.wang.dao.xml" targetProject=".\src\main\java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetProject:mapper介面生成的的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.wang.dao" targetProject=".\src\main\java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定資料表 --> <table schema="" tableName="tbl_employee" domainObjectName="Employee"></table> <table schema="" tableName="tbl-department" domaninObjectName="Department"></table> <!-- <table schema="" tableName="tb_content_category"></table>--> <!-- <table schema="" tableName="tb_item"></table>--> <!-- <table schema="" tableName="tb_item_cat"></table>--> <!-- <table schema="" tableName="tb_item_desc"></table>--> <!-- <table schema="" tableName="tb_item_param"></table>--> <!-- <table schema="" tableName="tb_item_param_item"></table>--> <!-- <table schema="" tableName="tb_order"></table>--> <!-- <table schema="" tableName="tb_order_item"></table>--> <!-- <table schema="" tableName="tb_order_shipping"></table>--> <!-- <table schema="" tableName="tb_user"></table>--> <!-- 有些表的欄位需要指定java型別 <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" > <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> --> </context> </generatorConfiguration>
生成的程式碼
@org.junit.Test public void test1(){ try{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; // 指定配置檔案,這裡最好是使用絕對路徑 File configFile = new File("D:\\Coding\\IntellijProjects\\mybatis-MBG\\src\\main\\resources\\mbg.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); }catch (Exception e){ e.printStackTrace(); } }
補充:
自動生成的程式碼是不會包含有關聯查詢的,所以需要手動補充
如:
<!--=================補充:帶部門的查詢=====================--> <resultMap id="withDepartmentResultMap" type="employee"> <id column="employee_id" property="employeeId"/> <result column="employee_name" property="employeeName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> <result column="department_id" property="departmentId"/> <!--分步查詢--> <!--呼叫departmentmapper中的selectByPrimaryKey來級聯合成--> <association property="department" select="com.wang.dao.DepartmentMapper.selectByPrimaryKey" column="department_id"/> </resultMap> <sql id="withDepartment_Column_List"> e.employee_id,e.employee_name,e.gender,e.email,e.department_id,d.department_name </sql> <!-- List<Employee> selectByExampleWithDepartment(EmployeeExample example);--> <select id="selectByExampleWithDepartment" parameterType="com.wang.pojo.EmployeeExample"> select <if test="distinct"> distinct </if> <include refid="withDepartment_Column_List" /> FROM tbl_employee e LEFT JOIN tbl_department d ON e.`department_id` = d.`department_id` <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null"> order by ${orderByClause} </if> </select> <!-- Employee selectByPrimaryKeyWithDepartment(Integer employeeId);--> <select id="selectByPrimaryKeyWithDepartment" parameterType="com.wang.pojo.EmployeeExample"> select <include refid="withDepartment_Column_List" /> FROM tbl_employee e LEFT JOIN tbl_department d ON e.`department_id` = d.`department_id` where employee_id = #{employeeId,jdbcType=INTEGER} </select>