The previous explanation is how to use mybatis in a Java project. We use the method of mapping files. When obtaining specific data operation methods, you need to pass the namespace + "." method name in the map file. This method sometimes feels very unpleasant and troublesome. Don’t we often say that we need to be interface-oriented in development? Mybatis also supports interfaces. The following is based on the previous examples.
The environment and mapping files of the previous example remain unchanged. The following is my mapping files.
<mapper namespace="com.cn.inter.IMessageOperation"><select id="selectUserByID" parameterType="int" resultType="com.cn.imooc.entity.Message">select * from `message` where id = #{id}</select><select id="selectMessages" resultType="Message">select id,command,description,commentfrom message;</select></mapper>We can see that the namespace is com.cn.inter.ImessageOperation inside. Now we create such a package, com.cn.inter. In this package, IMessageOperation creates an interface. There is a method in the interface. The method signature is: public Message selectUserByID(Integer id);
The interface and mapping files we created are consistently corresponding, including method names, return values, and parameter lists. See the test method below
package com.cn.test;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.cn.imooc.entity.Message;import com.cn.inter.IMessageOperation;public class MyTest2 {public static void main(String[] args) {// TODO Auto-generated method stubReader reader;SqlSession sqlSession=null;try{//Read the mybatis configuration file from the classpath (src) reader=Resources.getResourceAsReader("Configuration.xml");SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);sqlSession=sqlSessionFactory.openSession();//Get IMessageOperation interface IMessageOperation imo=sqlSession.getMapper(IMessageOperation.class);//Call the interface method to return query result Message message=imo.selectMessageByIdI(new Integer(3));System.out.println(message);}catch(Exception e){e.printStackTrace();} finally{//If sqlSession is not empty, close if(null!=sqlSession)sqlSession.close();}}}}We can see that the method of calling data operations in the test method has changed. We first obtain an IMessageOperation interface, then call its selectMessageByID method, and finally get the result. It can be felt that the method is simpler than the one in the previous article and is more in line with our daily coding specifications.
Combining the methods in these two articles, it is OK to use either one, but only two different methods. I personally prefer the latter.
The above is the relevant information about how to use MyBatis (II) introduced to you by the editor. It is very good and has reference value. I hope it will be helpful to everyone!