1. Although I do not want to say some gossip, but I still think it is necessary to think about thinking.
Recently, when I was studying mybatis, I found that I had always regarded the MyBatis framework as a framework of technology, a tool, and a learning relationship with Hibernate, although it was very rewarding, but always felt like
In circles, deep thought of the next, found that such an understanding may be right, but I think it seems to make their own thinking fixed, so I seriously think about it, there is no better way. I found the rationale behind the idea of the object
Solved a lot, because all things are objects, we can use some of the framework as one of the objects to understand, do not know this will not be better to understand a little.
I think the framework has all the properties that the object should have.
2. Ever since I had this thought, I began to think, to verify, whether this is the case:
The first step in creating an object is this:
(1) Creating a highly abstract object is actually the class we use
(2) Use the new keyword to create an object when using the object we created
(3) Then the method inside the object is called.
Then we look at the steps used to use the framework:
(1) Be sure to import the jar package, omitted in Java because it is a lot of jar
(2) It is definitely import some configuration files, and then go to modify the configuration to achieve our goal. (analogy, I think this is the process we create the class)
(3) There is no doubt that the object is created, that is, reading an XML document, etc., to get the object
(4) It is also called the method that it provides to us
Does it look like it's a bit similar?
Then, every time we take the initiative to configure the file, is not equivalent to our class to add methods, nothing but the name of the method, the parameters passed in, the type of return value, etc., but each framework has its own principles and norms.
I think since I understand this, I feel the framework I will be half, of course, this is exaggerated, after all, the framework to consider a lot of things, but for us to use, should not be a problem.
So in understanding, the framework is an object.
3. Below we will use an example to play the basic usage of the MyBatis framework;
3.1 First apply it to do a query operation:
The same steps used for the framework described above:
Let's just skip the (1) (2) step and start directly from the third step:
(3) Configure the Mybatis-config.xml file with the Usermapper.xml file, as shown below (I do not know can refer to my previous article, perhaps you will be a little sentiment mybatis study)
Mybatis-config.xml file (this is equivalent to the basic class we created, introduced into the embedded class via the mappers tag)
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "
http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<environments default= "Development" >
<environment id= " Development ">
<transactionmanager type=" JDBC "/>
<datasource type=" Pooled ">
< Property name= "Driver" value= "Com.mysql.jdbc.Driver"/>
<property name= "url" value= "jdbc:mysql:// localhost:3306/mybatis_db "/>
<property name=" username "value=" root "/>
<property name=" Password "value=" yqy "/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource= "Com/zqu/mybatis/dao/pojo/user.xml"/>
</mappers>
</configuration>
Usermapper.xml file (this is equivalent to embedding the class, that is, the method we define, for the basic class use)
<?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.zqu.mybatis.dao.pojo.UserMapper" >
<select id= "Getuserbyid" ParameterType = "int" resulttype= "user" >
select Uname as "uname", upwd as "Upasword" from ' user ' where uid = #{uid}
</select >
</mapper>
A mapper is equivalent to an embedded class, the Select tag is a type of method, and Insert,update,delete, in fact, is
Equivalent to the Void,string,int type in the normal object, id = method name, ParameterType = parameter, Resulttype = return type
There is no doubt that the following SQL statement is our process of defining a method, in MyBatis with Ognl method to accept parameters, that is, #{}, with ${} is also OK, but the thread is not safe, Pojo class difficult to handle, recommended #{}
It feels a lot easier, I know a lot of people have this feeling too
(4) Create a generic object for the Pojo class Java user
Package Com.zqu.mybatis.dao.pojo;
public class User {
private int uid;
Private String uname;
Private String Upasword;
Public user () {
//TODO auto-generated constructor stubs
} public
User (int uid, string uname, String Upasword) {
super ();
This.uid = UID;
This.uname = uname;
This.upasword = Upasword;
}
public int Getuid () {
return uid;
}
public void SetUid (int uid) {
this.uid = uid;
}
Public String Getuname () {
return uname;
}
public void Setuname (String uname) {
this.uname = uname;
}
Public String Getupasword () {
return upasword;
}
public void Setupasword (String upasword) {
This.upasword = Upasword;
}
}
(5) We are going to use, the equipment has been installed, now on the battlefield to:
Package Com.zqu.mybatis.dao.pojo;
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;
public class HelloWorld {private static sqlsessionfactory sqlsessionfactory;
private static reader reader;
Static {try {reader = Resources.getresourceasreader ("config + +");
Sqlsessionfactory = new Sqlsessionfactorybuilder (). build (reader);
} catch (Exception e) {e.printstacktrace ();
}} public static Sqlsessionfactory getsession () {return sqlsessionfactory; }/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub sqlsession SE
Ssion = Sqlsessionfactory.opensession ();
try {String statement = "Com.zqu.mybatis.dao.pojo.UserMapper.GetUserByID";
User user = (user) Session.selectone (statement, 2); if (user!=null) {String userInfo = "First Name:" +user. Getuname () + ", Password::" +user.getupasword ();
System.out.println (UserInfo);
}} finally {Session.close ();
}
}
}
The results show:
Name: Sword Demon, Password:: 12345631
The article is written here, there is nothing wrong place, hope a lot of understanding, message let me correct, study, thank you.