MyBatis xml and dao layer interface are used in combination
mybatis can use xml for data operations, or use annotations at the dao layer, or use a combination of xml and dao layer interfaces. Obviously, the latter is simpler
package com.zhao.entity; /** * * @author: zhao * @time: May 31, 2016 * * @description:student */ 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 mode for database query operation
<?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>
Of course, in the configuration file of mybatis, we define the alias of the class, StudentDao.xml and the database
<mappers> <mapper resource="com/zhao/mapper/StudentDao.xml"/> </mappers>
test first
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 way to operate the database, using the selectOne method of SqlSession.
public abstract <T> T selectOne(String paramString, Object paramObject);
2: Use annotations at the dao layer
public interface StudentDao { @Select("select * from student where stu_id=#{stuId}") public Student queryById(int stuId); }
To avoid confusion, modify the configuration file again
<mappers> <mapper class="com.zhao.dao.StudentDao"/> </mappers>
then test again
@Test public void testAnnotationQueryById(){ StudentDao studentDao=sqlSession.getMapper(StudentDao.class); Student student=studentDao.queryById(1); System.out.println(student); }
We can see that a Dao layer interface object is obtained by using the getMapper method of SqlSession, and then the queryById method is called to find the result.
For now:
There is no connection between xml and dao layer annotations, they are two different query methods.
However, the configuration of xml is relatively simple, but it is more cumbersome to use. The dao layer annotations need to be operated on the code, which looks uncomfortable
3: xml+dao
The mapper file needs to link xml and dao
In fact, I have not modified the mapper file. We can see that the namespace attribute of the mapper note is the full path of the Dao layer interface, and the id attribute of select is the corresponding method of the Dao layer interface. These names are the same. Of course it has to be the same
<?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>
The Dao layer is the same as in the annotation method. However, the annotations in the Dao layer interface have been deleted by me
public interface StudentDao { public Student queryById(int stuId); }
No need to modify the test class
@Test public void testAnnotationQueryById(){ StudentDao studentDao=sqlSession.getMapper(StudentDao.class); Student student=studentDao.queryById(1); System.out.println(student); }
Summary: This is done so that xml and dao can be combined. The configuration file is configured in xml. But this xml points to an interface. When we use it, it will be more clear to perform corresponding operations through the interface. It is also comfortable to modify sql code in xml