数据库已经事先建好了名字叫ssm_crud
,里面有两张表tbl_emp
和tbl_dept
,tbl_emp
表中的d_id
是外键关联tbl_dept
表中的主键.
首先使用maven
来导入jar
包
到Maven
依赖查询中搜索mybatis generator
找到依赖加入到pom.xml
中
<!-- 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>
然后创建一个名字叫mbg.xml
的文件放在与pom.xml
同级的目录中
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>
<!--我的数据库是mysql8的,5.7等低版本的可以用com.mysql.jdbc.Driver-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT"
userId="root"
password="1230">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--指定javabean生成的位置-->
<javaModelGenerator targetPackage="com.atguigu.crud.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接口生成的位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.crud.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>
然后在com.atguigu.crud.test
中新建一个类MBGTest
package com.atguigu.crud.test;
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;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author nyh
* @create 2018-08-15 11:39
**/
public class MBGTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("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);
}
}
运行main
方法看到如下图一样有文件生成就代表成功了
自动生成的不一定是符合我们业务逻辑的,所以我们可能有时候要对其进行一些修改
示例:新增两个查询
先到EmployeeMapper
中添加两个方法
/*新增两个查询方法,希望查询员工信息时部门的信息也一并查询出来*/
List<Employee> selectByExampleWithDept(EmployeeExample example);
Employee selectByPrimaryKeyWithDept(Integer empId);
到Employee
类中添加Department
属性,并加上get,set
方法
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
然后就可以到EmployeeMapper.xml
中写查询了
<resultMap id="WithDeptResultMap" type="com.atguigu.crud.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.atguigu.crud.bean.Department">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
<sql id="WithDept_Column_List">
emp_id, emp_name, gender, email, d_id,dept_id,dept_name
</sql>
<select id="selectByExampleWithDept" resultMap="WithDeptResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="WithDept_Column_List"/>
from tbl_emp
left join tbl_dept on 'd_id'='dept_id'
<if test="_parameter != null">
<include refid="Example_Where_Clause"/>
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKeyWithDept" resultMap="WithDeptResultMap">
select
<include refid="WithDept_Column_List"/>
from tbl_emp
left join tbl_dept on 'd_id'='dept_id'
where emp_id = #{empId,jdbcType=INTEGER}
</select>
上面的配置我看不懂,所以我得赶紧去复习一下mybatis
哈哈哈