MyBatis--DAO(二)

MyBatis xml和dao层接口组合使用

mybatis可以用xml进行数据操作,也可以在dao层用注解的方式,也可以采取xml和dao层接口组合使用的方法。显然 ,后者更加简单

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>

当然,我们在mybatis的配置文件中,定义了类的别名、StudentDao.xml 以及数据库

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

 先进行测试

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);

 

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

mapper文件需要把xml和dao 联系起来

其实我并没有修改这个mapper文件,我们可以看到 mapper便签的namespace属性就是Dao层接口的全路径,select的id属性就是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>
<mappers>
        <mapper resource="com/zhao/mapper/StudentDao.xml"/>
</mappers>

  Dao层跟用注解方法中是一样的。不过Dao层接口中注解已经被我删除了

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

 不需要修改测试类

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

总结:这样做就是为了让xml和dao能组合起来。配置文件中配置的是xml。但是这个xml指向了一个接口。我们在用的时候通过接口来进行相应操作,会更加清晰明了。在xml中修改sql代码也很舒服

猜你喜欢

转载自2277259257.iteye.com/blog/2335045