I. Initial knowledge of MyBatis
The term ibatis is derived from the combination of "Internet" and "Abatis" and is a Java-based persistence layer framework. Ibatis provides a durable layer framework that includes SQL maps and Data Access Objects (DAO), MyBatis, an open source project for Apache Ibatis, which migrated to Google code in 2010. and renamed MyBatis, November 2013 migration to GitHub.
Two. Basic configuration and execution of MyBatis
1. Prepare the MyBatis jar package: Mybatis-3.2.8.jar
Prepare the Database jar package (I'm using MySQL): Mysql-connector-java-3.1.12-bin.jar
2. Prepare the MyBatis Master profile: Mybatis-config.xml
Project architecture see diagram, where Jdbc.propertis is a database connection configuration file
The code for Jdbc.propertis and Mybatis-config.xml is attached below
Jdbc.driverclassname=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username= Root
jdbc.password=12345
<?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> <!--method One: Specify the properties profile externally, in addition to the Resource property designation, you can also specify url--> <properties re by using the URL property Source= "Jdbc.properties"/> <!--through the package, you can specify the name of the packages directly, MyBatis will automatically scan the JavaBean under your assigned packet and set an alias by default
, the default name is: JavaBean's first lowercase unqualified class name as its alias.
You can also customize aliases in JavaBean plus annotations @alias, for example: @Alias (user) <package name= "com.dy.entity"/>-<typeAliases> <typealias alias= "Student" type= "com.douhao.model.Student"/> </typeAliases> <environments default= " Development "> <environment id=" Development "> <transactionmanager type=" JDBC "/> <datasource type = "Pooled" > <property name= "Driver" value= "${jdbc.driverclassname}"/> <property name= "url" value= "${jd Bc.url} "/> <property name=" username "value=" ${jdBc.username} "/> <property name=" password "value=" ${jdbc.password} "/> </dataSource> <!--
Method Two: Directly configured as XML <datasource type= "pooled" > <property name= "Driver" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "jdbc:mysql://localhost:3306/test"/> <property name= "username" value= "root
"/> <property name=" password "value=" 1234 "/> </dataSource> </environment> <!--a environments element can be equipped with multiple environment elements to correspond to different environments, such as the development environment and production environment. The environment element can be configured with a different ID, as to which environment to use, can be implemented through the default property of the environments element, such as <environments default= "Development" >.
Then it will run the ID is development environment.
--<environment id= "final" > <transactionmanager type= "JDBC"/> <datasource type= "Pooled" > <property name= "Driver" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://localhost : 3306/test "/> <property name=" username"Value=" root "/> <property name=" password "value=" 1234 "/> </dataSource> </environment> </environments> <mappers> <mapper resource= "Com/douhao/mappers/studentmapper.xml"/> </mappers
> <!--<mappers> The first way: Specify <mapper resource= "Com/dy/dao/userdao.xml"/> via resource The second way, the interface is specified through class, and then the interface is mapped to the corresponding XML file. However, it is necessary to ensure that the interface has the same name as the mapper file (case insensitive), and I have an interface here UserD
AO, then means that the mapper file is Userdao.xml <mapper class= "Com.dy.dao.UserDao"/> The third Way, directly specifying the package, automatic scanning, and method two similarly <package name= "Com.dy.dao"/> Fourth Way: Specify Mapper file location by URL <mapper url= "file://..."/&G
T
</mappers> </configuration>
Now that we have the MyBatis master configuration file, but to run mybatis we need to know two key points of knowledge
1.SqlSessionFactory 2.SqlSession
Sqlsessionfactory according to his name, this is a mybatis session factory, used to create the core of MyBatis: sqlsession
So we're 1. To get sqlsessionfactory through the just-in-the-main configuration file. 2. Open the database session via Sqlsessionfactory and get sqlsession
Again the role of the sqlsession:
1. Passing parameters to SQL statements
2. Execute SQL statements
3. Get the result of executing the SQL statement
4. Control of transactions
The idea is clear: 1. Get sqlsessionfactory 2 through the Master profile. Get sqlsession with Sqlsessionfactory to operate our database. See the code below
Package com.douhao.service;
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;
Import com.douhao.model.Student; public class Studentservice {/** * * @author * @description Get sqlsessionfactory * @return * @throws ioexcept Ion * @update September 24, 2016 PM 4:21:54 */public sqlsessionfactory getsessionfactory () throws ioexception{//Get data through configuration file
Information about the library connection Reader reader= resources.getresourceasreader ("Mybatis-config.xml");
Build sqlsessionfactory sqlsessionfactory sqlsessionfactory=new sqlsessionfactorybuilder (). Build (reader) with configuration information;
return sqlsessionfactory;
} public static void Main (string[] args) {studentservice studentservice=new studentservice ();
Sqlsession Sqlsession=null; try {sqlsessionfactory sqlsessionfactory=studentservice.getsessIonfactory ();
Sqlsession=sqlsessionfactory.opensession (); List<student> list=sqlsession.selectlist ("Student.findstudentbyid");//This is configured in Studentmapper.xml
(namespace. Element id) System.out.println (list.size ());
for (Student s:list) {System.out.println (S.getid ());
}} catch (IOException e) {e.printstacktrace ();
}finally {if (sqlsession!=null) {sqlsession.close ();
}
}
}
}
Package Com.douhao.model;
public class Student {
Integer id;
String name;
String age;
Public Student () {
super ();
}
Public Student (string name, String age) {
super ();
this.name = name;
This.age = age;
}
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
this.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
Public String Getage () {
return age;
}
public void Setage (String age) {
this.age = age;
}
}
<?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= "Student" >
<select id= "Findstudentbyid" resulttype= " Com.douhao.model.Student ">
select * from T_student
</select>
After executing the main method, the record is detected. At this point MyBatis can run.