Here, the configuration of MyBatis is no longer described.
MyBatis can use XML for data manipulation, or it can be used in the form of annotations in the DAO layer, or in combination with the XML and DAO layer interfaces. Obviously, the latter is much simpler.
Entity class Student
Packagecom.zhao.entity;/** * * @author: Zhao * @time: May 31, 2016 * * @description: Students*/ Public classStudent {Private intStuid; PrivateString Stuname; PrivateString Stuclass; Public intGetstuid () {returnStuid; } Public voidSetstuid (intstuid) { This. Stuid =Stuid; } PublicString Getstuname () {returnStuname; } Public voidsetstuname (String stuname) { This. Stuname =Stuname; } PublicString Getstuclass () {returnStuclass; } Public voidSetstuclass (String stuclass) { This. Stuclass =Stuclass; } @Override PublicString toString () {return"Student [stuid=" + Stuid + ", stuname=" + Stuname + ", stuclass=" + Stuclass + "]"; }}
Database query operation by 1:xml method
<?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 "><Mappernamespace= "Com.zhao.dao.StudentDao"> <SelectID= "Querybyid"ParameterType= "int"Resulttype= "Student">SELECT * from student where Stu_id=#{stuid}</Select></Mapper>
To test first
PrivateString resource= "Mybatis-config.xml"; PrivateInputStream InputStream; Privatesqlsessionfactory sqlsessionfactory; Privatesqlsession sqlsession; @Before Public voidbefore () {InputStream=studenttest.class. getClassLoader (). getResourceAsStream (Resource); Sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); Sqlsession=sqlsessionfactory.opensession (); } @After Public voidAfter () {sqlsession.close (); } @Test Public voidTestxmlquerybyid () {Student Student= (Student) sqlsession.selectone ("Com.zhao.dao.StudentDao.queryById", 1); SYSTEM.OUT.PRINTLN (student); }
XML to manipulate the database, using the Sqlsession SelectOne method.
Public abstract <T> T SelectOne (String paramstring, Object paramobject);
Of course, in the MyBatis configuration file, we define the class alias, Studentdao.xml, and the database
< mappers > < resource= "Com/zhao/mapper/studentdao.xml"/> </ Mappers>
Now we can find the results.
Student [Stuid=1, Stuname=zhao, STUCLASS=JAVA10 class]
2: Using annotations in 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
< mappers > < class= "Com.zhao.dao.StudentDao"/> </ Mappers>
And then the test.
@Test publicvoid Testannotationquerybyid () { Studentdao Studentdao =sqlsession.getmapper (Studentdao. Class); Student Student=studentdao.querybyid (1); SYSTEM.OUT.PRINTLN (student); }
As we can see, the sqlsession Getmapper method is used to get a DAO layer interface object, and then calls the results of the Querybyid method.
At present:
There is no connection between XML and DAO layer annotations, which is two different ways of querying.
But the configuration of XML is relatively simple, but it is cumbersome to use. The DAO layer annotations need to be manipulated in code and look uncomfortable.
3:xml+dao
You do not need to modify the test class
@Test publicvoid Testannotationquerybyid () { Studentdao Studentdao =sqlsession.getmapper (Studentdao. Class); Student Student=studentdao.querybyid (1); SYSTEM.OUT.PRINTLN (student); }
This is the same as using annotations. But I've already deleted the annotations in the DAO layer interface.
Public Interface Studentdao { public Student querybyid (int stuid);}
Now we need to link XML with 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 "><Mappernamespace= "Com.zhao.dao.StudentDao"> <SelectID= "Querybyid"ParameterType= "int"Resulttype= "Student">SELECT * from student where Stu_id=#{stuid}</Select></Mapper>
In fact, I did not modify this mapper file, we can see the namespace property of Mapper note is the full path of the DAO layer interface, 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.
Then modify the configuration file
< mappers > < resource= "Com/zhao/mapper/studentdao.xml"/></mappers >
This is done so that the XML and DAO can be combined together. XML is configured in the configuration file. But this XML points to an interface. When we use the interface to do the corresponding operation, it will be more clear. It is also comfortable to modify the SQL code in XML.
MyBatis XML and DAO layer interfaces combined with