What is MyBatis
MyBatis is an open source project for Apache Ibatis, the project was migrated from Apache Software Foundation to Google code in 2010 and renamed MyBatis. The word ibatis is derived from the combination of "Internet" and "Abatis", a Java-based persistence layer framework. The persistence layer framework provided by Ibatis includes SQL maps and Data Access Objects (DAO).
MyBatis is an open source project for Apache Ibatis, the project was migrated from Apache Software Foundation to Google code in 2010 and renamed MyBatis. November 2013 migrated to GitHub.
The June 2011 IBatis renamed MyBatis, from IBatis to MyBatis, not just the name change, MyBatis provides more powerful functionality, while not losing its ease of use, but in many places it has been simplified with the help of JDK's generics and annotation features. So you know, start learning from the MyBatis bar.
Example uses Mybatis+mysql to implement a query for a user table, as follows:
1. Database
In MySQL, under the test database, Resume user table, field: id,name,password, build table statement slightly.
2, need to import the package
Only two: Mybatis-3.0.3.jar Mysql-connector-java-5.1.16-bin.jar (JDBC package)
3, directory structure
With the simplest structure, there are only four files under the Com.mybatis package: Configuration.xml, User.xml, User.java, Test.java.
4, mybatis configuration file Configuration.xml
<?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> <typeAliases> <typealias alias= "User" type= "Com.mybatis.User" ></typeAlias> </typeAliases> <environments default= "Development" > <environment id= "Development" > < TransactionManager type= "JDBC" ></transactionManager> <datasource type= "Pooled" > <property name= " Driver "value=" Com.mysql.jdbc.Driver "/> <property name=" url "value=" Jdbc:mysql://localhost:3306/test? Useunicode=true&characterencoding=utf-8 "/> <property name= username" value= "root"/> <property "Password" value= "123456"/> </dataSource> </environment> </environments> <mappers> <ma Pper resource= "User.xml"/> </mappers> </configuration>
5. User table SQL file User.xml
<?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= "User" >
<select id= "Selectuser" parametertype= "int" resulttype= "user" >
SELECT * FROM user WHERE id = #{id}
</select>
<select id= "selectusers" resulttype= "user" >
SELECT * from user
</select>
</mapper>
6. Table Structure File User.java
Package com.mybatis;
public class User {
private int id;
private String name;
private String password;
Public user () {} public
user (int id, String name) {
this.id = ID;
this.name = name;
public int getId () {return
this.id;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
this.name;
}
public void SetName (String name) {
this.name = name;
}
Public String GetPassword () {return
this.password;
}
public void SetPassword (String password) {
this.password = password;
}
@Override public
String toString () {return
"User [id=] + this.id +", name= "+ THIS.name +", password= "+ This" . Password + "]";
}
}
7. Test Case Test.java
Package com.mybatis;
Import java.io.IOException;
Import Java.io.Reader;
Import java.util.List;
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 Test {public static void main (string[] args) throws IOException {String resource = "Configuration.xml";
Reader reader = Resources.getresourceasreader (Resource);
Sqlsessionfactory SSF = new Sqlsessionfactorybuilder (). build (reader);
sqlsession session = Ssf.opensession ();
try{User user = Session.selectone ("Selectuser", "1");
System.out.println (User.getname ());
SYSTEM.OUT.PRINTLN (user);
System.out.println ("--------------Dividing Line---------------");
list<user> users = session.selectlist ("Selectusers");
for (int i=0; i<users.size (); i++) {System.out.println (Users.get (i). GetName ());
} catch (Exception e) {e.printstacktrace ();
finally {session.close (); }
}
}
The above is a small series to introduce the Java Persistence Layer Framework MyBatis Simple example, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!