I. Introduction of MyBatis
MyBatis is an excellent persistence layer framework that supports common SQL queries , stored Procedures , and advanced mappings . MyBatis eliminates the manual setting of almost all JDBC code and parameters and encapsulates the retrieval of the result set. MyBatis can use simple XML or annotations for configuration and raw mapping, mapping interfaces and Java Pojo (Plain old Java Objects, ordinary Java objects) to records in a database.
Second, MyBatis Quick Start
2.1. Prepare for development environment
1. Create a test project, either an ordinary Java project or a Javaweb project, as shown in:
2. Add the appropriate JAR package
" MyBatis "
Mybatis-3.1.1.jar
"MySQL driver package"
Mysql-connector-java-5.1.7-bin.jar
3. Create databases and tables for MySQL database
The SQL script is as follows:
- );
Execute the SQL script in the MySQL database and complete the creation of the database and table as follows:
To this end, the preparation of the pre-development environment is fully completed.
2.2. Use MyBatis to query the data in the table
1. Add MyBatis configuration file Conf.xml
Create a conf.xml file in the SRC directory, as shown in:
The contents of the Conf.xml file are as follows:
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" > 3 <configuration> 4 <environments default= "Development" > 5 <environment id= "Development" > 6 <transactionmanager type= "JDBC"/> 7 <!--configuration database connection Information-8 <datasource type= "Pooled" > 9< C5/><property name= "Driver" value= "Com.mysql.jdbc.Driver"/>10 <property name= "url" value= "Jdbc:mysql ://localhost:3306/mybatis "/>11 <property name=" username "value=" root "/>12 <property name=" Password "value=" XDP "/>13 </datasource>14 </environment>15 </environments>16 </configuration>
2. Define the entity class corresponding to the table, as shown in the following:
The code for the user class is as follows:
1 package me.gacl.domain; 2 3/** 4 * @author gacl 5 * The entity class corresponding to the Users table 6 * /7 public class User {8 9 ///entity class attribute and table field name one by one corresponds to 10
private int id;11 private String name;12 private int age;13 + public int getId () { return id;16
}17 public void SetId (Int. ID) { this.id = id;20}21 public String getName () { Retu RN name;24 }25 -public void SetName (String name) { this.name = name;28}29-public int get Age () { age;32}33, public void Setage (int.) { this.age = age;36 }37 Override39 public String toString () {+ + " User [id=" + ID + ", name=" + name + ", age=" + Age + "]"; 41
}42}
3. Define the SQL mapping file that operates the users table Usermapper.xml
Create a me.gacl.mapping package that is designed to hold the SQL mapping file and create a usermapper.xml file in the package, as shown in:
The contents of the Usermapper.xml file are as follows:
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <!-- Specifying a unique Namespace,namespace value for this mapper is customarily set to the package name +sql the map file name, so that the value of namespace is guaranteed to be unique 4 for example namespace= " Me.gacl.mapping.userMapper "is me.gacl.mapping (package name) +usermapper (usermapper.xml file removal suffix) 5-- 6 <mapper Namespace= "Me.gacl.mapping.userMapper" > 7 <!--The SQL statement that writes the query in the Select tab, sets the ID property of the Select tag to GetUser, The id attribute value must be unique and cannot be repeated 8 using the ParameterType property to indicate the type of parameter used when querying, the Resulttype property indicates the result set type returned by the query 9 resulttype= " Me.gacl.domain.User "means the object that encapsulates the query result into a user class returns the user class is the entity class corresponding to the users table. -->12 <!-- Get a user object based on ID query -->15 <select id= "GetUser" parametertype= "int" resulttype= " Me.gacl.domain.User ">17 select * from users where id=#{id}18 </select>19 </mapper>
4. Register the Usermapper.xml file in the Conf.xml file
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" > 3 <configuration> 4 <environments default= "Development" > 5 <environment id= "Development" > 6 <transactionmanager type= "JDBC"/> 7 <!--configuration database connection Information-8 <datasource Typ E= "Pooled" > 9 <property name= "Driver" value= "Com.mysql.jdbc.Driver"/>10 <prop Erty name= "url" value= "Jdbc:mysql://localhost:3306/mybatis"/>11 <property name= "username" value= "ro OT "/>12 <property name=" password "value=" XDP "/>13 </datasource>14 < ;/environment>15 </environments>16 <mappers>18 <!--registration Usermapper.xml file, 19 Usermapper.xml is located under Me.gacl.mapping This package, so resource written me/gacl/mapping/usermapper.xml-->20 <mapper resource= "Me/gacl/mapping/usermapper.xml"/>21 </mappers>22 </configuration>
5. Write the test code: Execute the defined SELECT statement
To create a Test1 class, write the following test code:
Package Me.gacl.test;import Java.io.ioexception;import Java.io.inputstream;import java.io.reader;import Me.gacl.domain.user;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 Test1 {public static void main (string[] args) throws IOException {//mybatis configuration file String Resource = "Conf.xml"; Use the class loader to load the MyBatis configuration file (It also loads the associated mapping file) InputStream is = Test1.class.getClassLoader (). getResourceAsStream (Resource); Build Sqlsession factory Sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). Build (IS); Use the Resources class provided by MyBatis to load the MyBatis configuration file (which also loads the associated mapping file)//reader Reader = Resources.getresourceasreader (Resource); Build Sqlsession factory//sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). build (reader); Create sqlsession that can execute SQL in the mapping file sqlsession session = SESSionfactory.opensession (); /** * Mapping SQL identification String, * Me.gacl.mapping.userMapper is the value of namespace attribute of mapper tag in Usermapper.xml file, * getUser is the id attribute value of the select tag, the SQL/String statement = "Me.gacl.mapping.userMapper.getUser" is found by the ID attribute value of the select tag; The SQL User user = Session.selectone (statement, 1) that is a unique User object is returned by an identity string//execution Query SYSTEM.OUT.PRINTLN (user); }}
----------turn to the arrogant and aloof wolf
Http://www.cnblogs.com/xdp-gacl/p/4261895.html
MyBatis Inductive Learning 1