天天看點

了解MyBatis——一篇就夠了

目錄

MyBatis的概念及優缺點 

運作環境

 MyBatis建立流程

映射(*)

MyBatis的使用 

MyBatis的概念及優缺點 

MyBatis是一個半自動映射的架構。這裡所謂的"半自動"是相對于Hibernate全表映射而言的,MyBatis需要手動比對提供POJO、SQL和映射關系,而Hibernate隻需提供POJO和映射關系即可。

缺點:

MyBatis手動編寫SQL,工作量大。

優點:

可以配置動态SQL并優化SQL

可以通過配置決定SQL的映射規則

支援存儲過程

對于一些複雜的和需要優化性能的項目來說,顯示使用MyBatis更加合适。

運作環境

idea:2018-2

mysql:5.6

使用maven管理jar包

資料庫名為test,表為emp和dept,表資料如下

了解MyBatis——一篇就夠了
了解MyBatis——一篇就夠了

 MyBatis建立流程

1、利用idea建立一個maven工程,在pom.xml中引入jar包

<!--mybatis核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!--mysql驅動包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!-- 日志檔案管理包 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
           

避免配置檔案釋出到tomcat伺服器失敗,添加都build标簽裡

<!--把xml,properties配置檔案強制性的部署到tomcat伺服器上-->
<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <includes>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
  </resource>
</resources>
           

2、在resources裡添加日志檔案log4j.porperties(檔案名不可更改)

### jibie mudidi  ###
log4j.rootLogger=debug, stdout,logfile
### kongzhitai  ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
### wenjian ###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n
           

3、在resources裡添加mybatis配置檔案

編寫db.properties

jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc_username=root
jdbc_password=root
           

編寫mybatis-config.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--引入資料庫連接配接配置檔案-->
    <properties resource="db.properties"></properties>
    <!--别名,可以直接使用類名,不用加全限定類名-->
    <typeAliases>
        <package name="cn.sdut.mybatis.po"></package>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置資料庫連接配接資訊 -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc_driver}" />
                <property name="url" value="${jdbc_url}" />
                <property name="username" value="${jdbc_username}" />
                <property name="password" value="${jdbc_password}" />
            </dataSource>
        </environment>
    </environments>
</configuration>
           

4.編寫資料庫實體類

Dept

package cn.sdut.mybatis.po;

public class Dept {

    private int deptno;
    private String dname;
    private String loc;

    public Dept() {
    }

    public Dept(int deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    @Override
    public String toString() {
        return "dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
}
           

5.編寫dao接口

package cn.sdut.mybatis.dao;

import cn.sdut.mybatis.po.Dept;

import java.util.List;

public interface DeptDao {

    //查詢
    Dept selectById(int deptno);

    //增加
    int insertDept(Dept dept);

    //删除
    int deleteDeptById(int deptno);

    //更新
    int updateDept(Dept dept);

    List<Dept> selectAll();
}
           

6.編寫DeptDao.xml檔案

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--對應哪個dao接口-->
<mapper namespace="cn.sdut.mybatis.dao.DeptDao">
    <!--查詢的時候會用到resultMap-->
    <resultMap id="deptMap" type="Dept">
        <id column="deptno" property="deptno"></id>
        <result column="dname" property="dname"></result>
        <result column="loc" property="loc"></result>
    </resultMap>
    <!-- -查詢-->
    <select id="selectById" parameterType="int" resultMap="deptMap">
        select * from dept where deptno=#{deptno}
    </select>
    <insert id="insertDept" parameterType="Dept">
        insert into dept value(null,#{dname},#{loc})
    </insert>
    <delete id="deleteDeptById" parameterType="int">
        delete from dept where deptno=#{deptno}
    </delete>
    <update id="updateDept" parameterType="Dept">
        update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
    </update>

    <select id="selectAll" resultMap="deptMap">
        select * from dept
    </select>
</mapper>
           

在mybatis配置檔案mybatis-config.xml裡,裝配dao,configuration标簽裡

<mappers>
        <mapper class="cn.sdut.mybatis.dao.DeptDao"></mapper>
    </mappers>
           

6.編寫測試類

package cn.test.test;

import cn.sdut.mybatis.dao.DeptDao;
import cn.sdut.mybatis.po.Dept;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class Test {

    @org.junit.Test
    public void test(){

        //将主配置檔案讀取轉化為檔案流
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        //擷取sqlSessionFactory工廠
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        //獲得dao對象
        DeptDao deptDao = session.getMapper(DeptDao.class);

        //精确查找
        deptDao.selectById(10);
        //插入
        deptDao.insertDept(new Dept(0,"後勤部","青島"));
        //删除
        deptDao.deleteDeptById(42);
        //修改
        deptDao.updateDept(new Dept(42,"行政部","青島"));
        //查詢所有
        List<Dept> list = deptDao.selectAll();
        for (Dept d:list) {
            System.out.println(d.toString());
        }
        //如果不送出,則隻修改記憶體中的,雖然日志傳回正确,但是資料庫中的資料并沒有修改
        session.commit();
    }
}
           

運作結果

了解MyBatis——一篇就夠了
了解MyBatis——一篇就夠了

先删除後修改竟然成功了,髒資料,成功了。

映射(*)

多對一,在多的一方添加一的一方的對象

例如:根據員工id查詢部門資訊

因為dept和emp表中都有deptno,是以Emp中吧deptno删掉就可以了

package cn.sdut.mybatis.po;

/**
 * @author:hydra
 * @date:2019/11/29
 */
public class Emp {

    private int id;
    private String name;
    private String job;
    private int sal;
    private Dept dept;

    public Emp() {
    }


    public Emp(int id, String name, String job, int sal, int deptno, Dept dept) {
        this.id = id;
        this.name = name;
        this.job = job;
        this.sal = sal;
        this.dept = dept;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }


    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                ", dept=" + dept +
                '}';
    }
}
           

編寫EmpDao.xml 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.sdut.mybatis.dao.EmpDao">
    <resultMap id="empMap" type="Emp">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="job" column="job"></result>
        <result property="sal" column="sal"></result>
        <result property="deptno" column="deptno"></result>
        <!--多對一的時候用association,dept為屬性名稱,Dept為屬性類型-->
        <association property="dept" javaType="Dept">
            <!--配置屬性-->
            <id column="deptno" property="deptno"></id>
            <result column="dname" property="dname"></result>
            <result column="loc" property="loc"></result>
        </association>
    </resultMap>
    <insert id="insert" parameterType="Emp">
        insert into emp value(null,#{name},#{job},#{sal},#{deptno})
    </insert>
    <!--連表查詢-->
    <select id="select" resultMap="empMap">
        select * from emp join dept on emp.deptno=dept.deptno where id=#{id}
    </select>
</mapper>
           

MyBatis的使用 

在使用MyBatis架構的時候,主要涉及兩個核心對象:SQLSessionFactory和SQLSession。

SQLSessionFactory

  • 它是單個資料庫映射關系經過編譯後的記憶體鏡像,其主要作用是建立SqlSession。
    • SqlSessionFactory則可以通過SqlSessionFactoryBuilder對象來建立
    • 而SqlSessionFactoryBuilder則可以通過XML配置檔案或一個預先定義好的Configuration執行個體建構出SqlSessionFactory執行個體。
  • SqlSessionFactory是線程安全的
    • SqlSessionFactory一旦被建立,整個應用執行期間都會存在。
    • 如果多次建立同一個資料庫的SQLSessionFactory,name資料庫資源很容易被耗盡。
    • 為了解決這個問題,通常每一個資料庫隻會對于一個SQLSessionFactory,是以建構SqlSessionFactory執行個體時,建議使用單例模式。

SqlSession

  • 它是應用程式與持久層之間執行互動操作的一個單線程對象,其作用主要是執行持久化操作。
  • SqlSession對象包含了資料庫中所有執行SQL操作的方法,由于底層封裝了JDBC連接配接,是以可以直接使用其執行個體來執行已映射的SQL語句
  • SqlSession執行個體線程不安全,使用範圍最好在一個請求或者一個方法中

測試類

@org.junit.Test
    public void test1(){
        //将主配置檔案讀取轉化為檔案流
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        //擷取sqlSessionFactory工廠
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        //獲得dao對象
        EmpDao empDao = session.getMapper(EmpDao.class);

//        empDao.insert(new Emp(0,"hydra","ceo",500000,20));

        Emp emp = empDao.select(7936);
        System.out.println(emp);
    }
           

查詢成功 

了解MyBatis——一篇就夠了

一對多

事例:查詢研發部所有的部門員工

修改emp

package cn.sdut.mybatis.po;

import java.util.List;

public class Dept {

    private int deptno;
    private String dname;
    private String loc;
    private List<Emp> emps;

    public Dept() {
    }

    public Dept(int deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public Dept(int deptno, String dname, String loc, List<Emp> emps) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
        this.emps = emps;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public List<Emp> getEmps() {
        return emps;
    }

    public void setEmps(List<Emp> emps) {
        this.emps = emps;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                ", emps=" + emps +
                '}';
    }
}
           

 修改EmpDao.xml

在resultMap中添加collection标簽,添加emps的屬性

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--對應哪個dao接口-->
<mapper namespace="cn.sdut.mybatis.dao.DeptDao">
    <!--查詢的時候會用到resultMap-->
    <resultMap id="deptMap" type="Dept">
        <id column="deptno" property="deptno"></id>
        <result column="dname" property="dname"></result>
        <result column="loc" property="loc"></result>
        <!--一對多的時候為collection,屬性名為emps,ofType代表資料類型-->
        <collection property="emps" ofType="Emp">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="job" column="job"></result>
            <result property="sal" column="sal"></result>
            <result property="deptno" column="deptno"></result>
        </collection>
    </resultMap>
    <!-- -查詢-->
    <select id="selectById" parameterType="int" resultMap="deptMap">
        select * from dept join emp on dept.deptno=emp.deptno where dept.deptno=#{deptno}
    </select>
    <insert id="insertDept" parameterType="Dept">
        insert into dept value(null,#{dname},#{loc})
    </insert>
    <delete id="deleteDeptById" parameterType="int">
        delete from dept where deptno=#{deptno}
    </delete>
    <update id="updateDept" parameterType="Dept">
        update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
    </update>

    <select id="selectAll" resultMap="deptMap">
        select * from dept
    </select>
</mapper>
           

測試類

@org.junit.Test
    public void test2(){
        //将主配置檔案讀取轉化為檔案流
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        //擷取sqlSessionFactory工廠
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        //獲得dao對象
        DeptDao deptDao = session.getMapper(DeptDao.class);

        Dept dept = deptDao.selectById(20);
        System.out.println(dept.getDname());
        List<Emp> emps =dept.getEmps();
        for (Emp emp: emps) {
            System.out.println(emp);
        }
    }
           
了解MyBatis——一篇就夠了

總結:走了一遍流程,最重要的還是映射!映射!!映射!!! 

繼續閱讀