mybatis.generator使用

来源:互联网 发布:数据修约标准 编辑:程序博客网 时间:2024/06/06 17:19


Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件


关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases


pom.xml添加jar依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency>

创建表

create table `departments` (`DEPARTMENT_ID` int ,`DEPARTMENT_NAME` varchar ,`MANAGER_ID` int ,`LOCATION_ID` int );
CREATE TABLE `employees` (  `EMPLOYEE_ID` int(11) NOT NULL AUTO_INCREMENT,  `FIRST_NAME` varchar(20) DEFAULT NULL,  `LAST_NAME` varchar(20) DEFAULT NULL,  `EMAIL` varchar(20) DEFAULT NULL,  `PHONE_NUMBER` varchar(20) DEFAULT NULL,  `HIRE_DATE` date DEFAULT NULL,  `JOB_ID` int(11) DEFAULT NULL,  `SALARY` double DEFAULT NULL,  `COMMISSION_PCT` int(11) DEFAULT NULL,  `MANAGER_ID` int(11) DEFAULT NULL,  `DEPARTMENT_ID` int(11) DEFAULT NULL,  PRIMARY KEY (`EMPLOYEE_ID`)) ENGINE=InnoDB AUTO_INCREMENT DEFAULT CHARSET=utf8;

编写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><property name="suppressAllComments" value="true" /></commentGenerator><!-- 配置数据库连接 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/crud" userId="root"password="root"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- 指定javaBean生成的位置 --><javaModelGenerator targetPackage="com.me.bean"targetProject=".\src\main\java"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!--指定sql映射文件生成的位置 --><sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 指定dao接口生成的位置,mapper接口 --><javaClientGenerator type="XMLMAPPER"targetPackage="com.me.dao" targetProject=".\src\main\java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- table指定每个表的生成策略 --><table tableName="tbl_emp" domainObjectName="Employee"></table><table tableName="tbl_dept" domainObjectName="Department"></table></context></generatorConfiguration>

main主方法

import java.io.File;import java.util.ArrayList;import java.util.List;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;public class Generator{public static void main(String[] args) 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);}}
运行后会自动生成javaBean,Mapper Java和xml配置文件




注:多表查询语句还是要自己编写

连接多表查询语句的编写

EmployeeMapper接口添加方法,例如

List<Employee> selectByExampleWithDept(EmployeeExample example);Employee selectByPrimaryKeyWithDept(Integer empId);

EmployeeMapper.xml编写对应sql查询

把自动selectByPrimaryKey和selectByExample 复制一份稍做修改

就可以了

<!-- List<Employee> selectByExampleWithDept(EmployeeExample example); Employee selectByPrimaryKeyWithDept(Integer empId); --><resultMap id="WithDeptResultMap" type="com.me.bean.Employee"><id column="emp_id" jdbcType="INTEGER" property="empId" /><result column="emp_name" jdbcType="VARCHAR" property="empName" /><result column="gender" jdbcType="CHAR" property="gender" /><result column="email" jdbcType="VARCHAR" property="email" /><result column="d_id" jdbcType="INTEGER" property="dId" /><!-- 指定联合查询出部门字段的封装 --><association property="department" javaType="com.me.bean.Department"><id column="dept_id" jdbcType="INTEGER" property="deptId" /><result column="dept_name" jdbcType="VARCHAR" property="deptName" /></association></resultMap><sql id="WithDept_Column_List">e.emp_id, e.emp_name, e.gender, e.email, e.d_id, d.dept_id, d.dept_name</sql><select id="selectByExampleWithDept" parameterType="com.me.bean.EmployeeExample"resultMap="WithDeptResultMap">select<if test="distinct">distinct</if><include refid="WithDept_Column_List" />FROM tbl_emp e LEFT JOIN tbl_dept d ON e.`d_id`= d.`dept_id`<if test="_parameter != null"><include refid="Example_Where_Clause" /></if><if test="orderByClause != null">order by ${orderByClause}</if></select><select id="selectByPrimaryKeyWithDept" parameterType="java.lang.Integer"resultMap="WithDeptResultMap">select<include refid="WithDept_Column_List" />FROM tbl_emp e LEFT JOIN tbl_dept d ON e.`d_id`= d.`dept_id`where emp_id = #{empId,jdbcType=INTEGER}</select>






原创粉丝点击