How to Use mybatis II and mybatis II
The front section explains how to use mybatis in a java project. We use the ing file method. When obtaining specific data operation methods, we need to input namespace + "in the ing file. "method name. This method is sometimes uncomfortable and troublesome. Isn't it often said that the interface orientation should be changed during development? mybatis also supports the interface, and the following modifications are made based on the previous example.
The environment and ing files in the previous example remain unchanged. The following is my ing file,
<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, comment from message; </select></mapper>
We can see that the namespace is com.cn. inter. imessageOperation, now we create such a package, com.cn. inter, create the interface IMessageOperation in this package. There is a method in the interface, and the signature of the method is public Message selectUserByID (Integer id );
The interface we created corresponds to the ing file, including the method name, return value, and parameter list. The following describes the test method.
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. 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 stub Reader; SqlSession sqlSession = null; try {// read the mybatis configuration file from the class path (src)
Reader = Resources. getResourceAsReader ("Configuration. xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (reader); sqlSession = sqlSessionFactory. openSession (); // obtain the IMessageOperation Interface
IMessageOperation imo = sqlSession. getMapper (IMessageOperation. class); // call the interface method to return the query result
Message message = imo. selectMessageByIdI (new Integer (3); System. out. println (message);} catch (Exception e) {e. printStackTrace ();} finally {// If sqlSession is not empty, disable
If (null! = SqlSession) sqlSession. close ();}}}
We can see that the method for 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. We can feel that it is simpler than the method in the previous article, and more in line with our daily coding specifications.
Using either of the two methods is acceptable, but it is only two different methods. I personally prefer the latter.
If you have any mistakes, please correct them.
Thank you.