MyBatis xml和dao層接口組合使用


在這里,不再介紹Mybatis的配置。

mybatis可以用xml進行數據操作,也可以在dao層用注解的方式,也可以采取xml和dao層接口組合使用的方法。顯然 ,后者更加簡單。

實體類Student

package com.zhao.entity;

/**
 * 
 * @author: zhao
 * @time: 2016年5月31日
 * 
 * @description:學生
 */
public class Student {
    private int stuId;
    private String stuName;
    private String stuClass;

    public int getStuId() {
        return stuId;
    }

    public void setStuId(int stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuClass() {
        return stuClass;
    }

    public void setStuClass(String stuClass) {
        this.stuClass = stuClass;
    }

    @Override
    public String toString() {
        return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuClass=" + stuClass + "]";
    }

}

 

1:xml方式進行數據庫查詢操作

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhao.dao.StudentDao">
    <select id="queryById" parameterType="int" resultType="Student">
        select * from student where stu_id=#{stuId}
    </select>
</mapper>

先進行測試

private String resource="mybatis-config.xml";
    private InputStream inputStream;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    @Before
    public void before(){
        inputStream=StudentTest.class.getClassLoader().getResourceAsStream(resource);
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession=sqlSessionFactory.openSession();
    }
    @After
    public void after(){
        sqlSession.close();
    }

    @Test
    public void testXmlQueryById() {
        Student student=(Student)sqlSession.selectOne("com.zhao.dao.StudentDao.queryById", 1);
        System.out.println(student);
    }

xml的方式操作數據庫,用了SqlSession的selectOne方法。

public abstract <T> T selectOne(String paramString, Object paramObject);

當然,我們在mybatis的配置文件中,定義了類的別名、StudentDao.xml 以及數據庫

    <mappers>
        <mapper resource="com/zhao/mapper/StudentDao.xml"/>
    </mappers>

 

現在我們能查到結果

Student [stuId=1, stuName=ZHAO, stuClass=Java10班]

2:在dao層使用注解

 

public interface StudentDao {
    
    @Select("select * from student where stu_id=#{stuId}")
    public Student queryById(int stuId);
}

為了避免混淆,再修改一下配置文件

    <mappers>
        <mapper class="com.zhao.dao.StudentDao"/>
    </mappers>

然后再進行測試

    @Test
    public void testAnnotationQueryById(){
        StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
        Student student=studentDao.queryById(1);
        System.out.println(student);
    }

我們可以看到,是用了SqlSession的getMapper方法得到了一個Dao層接口對象,然后調用了其中的queryById方法查到的結果。

目前來看:

  xml和dao層注解之間並沒有什么聯系,是兩個不同的查詢方式。

  但是xml的配置比較簡單,但是使用起來比較繁瑣。而dao層注解需要在代碼上進行操作,看起來也不舒服。

3:xml+dao

並不需要修改測試類

    @Test
    public void testAnnotationQueryById(){
        StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
        Student student=studentDao.queryById(1);
        System.out.println(student);
    }

這里跟用注解是一樣的。不過Dao層接口中注解已經被我刪除了

public interface StudentDao {
    
    public Student queryById(int stuId);
}

現在需要把xml和dao 聯系起來

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhao.dao.StudentDao">
    <select id="queryById" parameterType="int" resultType="Student">
        select * from student where stu_id=#{stuId}
    </select>
</mapper>

其實我並沒有修改這個mapper文件,我們可以看到 mapper便簽的namespace屬性就是Dao層接口的全路徑,select的id屬性就是Dao層接口的相應方法,這些名字都是一樣的。當然 也必須是一樣的。

然后修改配置文件

<mappers>
        <mapper resource="com/zhao/mapper/StudentDao.xml"/>
</mappers>

這樣做就是為了讓xml和dao能組合起來。配置文件中配置的是xml。但是這個xml指向了一個接口。我們在用的時候通過接口來進行相應操作,會更加清晰明了。在xml中修改sql代碼也很舒服。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM