SSM整合-使用mybatis的逆向工程生成对应的bean以及mapper

数据库已经事先建好了名字叫ssm_crud,里面有两张表tbl_emptbl_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&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;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哈哈哈

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值