Mybatis, mybatis Official Website
1. Introduction to Mybatis
MyBatis is a supportedNormalSQLQuery,Stored ProcedureAndAdvanced ingExcellentPersistence Layer framework. MyBatis eliminates the manual setup of almost all JDBC code and parameters and retrieval encapsulation of result sets. MyBatis can be used easilyXML or annotationIt is used for configuration and original ing to map interfaces and Java POJO (Plain Old Java Objects, common Java Objects) into records in the database.
Ii. Mybatis jar package
Reference package to be prepared
Mybatis-3.2.8.jar: myBatis framework use
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
Ojdbc6.jar: Database Connection
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>5.1.36</version>
</dependency>
Iii. Structure Analysis
1) You need to configure a file to connect to the database;
2) public methods are required to provide database connection;
3) PO: Persistent Object
4) SQL configuration XML file for data query;
5) external interface;
Iv. Structure Construction
Example:
IStudentInfoDAO. java
Package cn. happy. dao; import cn. happy. entity. studentInfo; import java. util. list;/*** Created by Happy on 2017-07-09. */public interface IStudentInfoDAO {// 01. query all student information in public List <StudentInfo> findAll (); // 02. query the public StudentInfo getStudentById (int stuid) of a specific student based on the student ID );}
IStudentInfoDAO. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Header file --> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- Namespace: differentiate sqlid a: findlAll B: findAll in Different Spaces --> <mapper namespace = "cn. happy. dao. IStudentInfoDAO"> <! -- SQL tag id: the unique SQL tag paramenterType: the SQL statement input parameter can be omitted resultType: Add delete operation: cannot write query: type of a single object --> <select id = "findAll" resultType = "StudentInfo">/* SQL: SQL statement */select * from studentinfo </select> <! -- Query by primary key --> <select id = "getStudentById" resultType = "StudentInfo"> select * from studentinfo WHERE stuid =#{ stuId} </select> </mapper>
StudentInfo. java
package cn.happy.entity;import java.util.Date;/** * Created by Happy on 2017-07-09. */public class StudentInfo { private Integer stuId; private String stuName; private Integer stuAge; private Date stuDate; public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public Integer getStuAge() { return stuAge; } public void setStuAge(Integer stuAge) { this.stuAge = stuAge; } public Date getStuDate() { return stuDate; } public void setStuDate(Date stuDate) { this.stuDate = stuDate; }}
MyBatis-config.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Document type, header file --> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <! -- The root node and XMl can only have one --> <configuration> <typeAliases> <! -- <TypeAlias type = "cn. happy. entity. StudentInfo" alias = "StudentInfo"> </typeAlias> --> <! -- Use the simple type of StudentInfo in the package as the class alias --> <package name = "cn. happy. entity"> </package> </typeAliases> <! -- One environment has N environment --> <environments default = "development"> <environment id = "development"> <! -- TransactionManager: JDBC ensures the update delete transaction category of the transaction: JDBC: programming transaction xxx. beginTransaction () tx. commit () tx. rollback () configuration-based transaction JDBC | MANAGED difference --> <transactionManager type = "JDBC"/> <! -- POOLED: MyBatis built-in connection pool c3p0 connection pool POOLED, UNPOOLED, and JNDI --> <dataSource type = "POOLED"> <property name = "driver" value = "com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // localhost: 3306/y2165 "/> <property name =" username "value =" root "/> <property name =" password "value =" root "/> </dataSource> </ environment> </environments> <mappers> <mapper resource = "cn/happy/dao/IStudentInfoDAO. xml "/> </mappers> </configuration>
MyBatisTest0709.java
Package cn. happy. test; import cn. happy. dao. IStudentInfoDAO; import cn. happy. entity. studentInfo; 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 org. junit. test; import java. io. IOException; import java. io. inputStream; import java. util. list;/*** Created by Happy on 2017-07-09. */public class MyBatisTest0709 {@ Test // 03. query the public void testSelectOneByGetMapper () {String path = "MyBatis-config.xml"; InputStream is = null; try {is = Resources. getResourceAsStream (path); SqlSessionFactory factory = new SqlSessionFactoryBuilder (). build (is); SqlSession session = factory. openSession (); IStudentInfoDAO = session. getMapper (IStudentInfoDAO. class); StudentInfo info = dao. getStudentById (3); System. out. println (info. getStuName ();/* MyBatisTest0709 my = new MyBatisTest0709 (); Class <MyBatisTest0709> clazz = (Class <MyBatisTest0709>) my. getClass (); try {Class <MyBatisTest0709> clazzz = (Class <MyBatisTest0709>) Class. forName ("cn. happy. test. myBatisTest0709 ");} catch (ClassNotFoundException e) {e. printStackTrace ();} */session. close ();} catch (IOException e) {e. printStackTrace () ;}@ Test // 02. query a single object public void testSelectOne () {String path = "MyBatis-config.xml"; InputStream is = null; try {is = Resources. getResourceAsStream (path); SqlSessionFactory factory = new SqlSessionFactoryBuilder (). build (is); SqlSession session = factory. openSession (); StudentInfo info = session. selectOne ("getStudentById", 3); System. out. println (info. getStuName (); session. close ();} catch (IOException e) {e. printStackTrace () ;}@ Test public void testAll () {String path = "MyBatis-config.xml"; InputStream is = null; try {is = Resources. getResourceAsStream (path); SqlSessionFactory factory = new SqlSessionFactoryBuilder (). build (is); SqlSession session = factory. openSession (); List <StudentInfo> list = session. selectList ("findAll"); for (StudentInfo info: list) {System. out. println (info. getStuName ();} session. close ();} catch (IOException e) {e. printStackTrace ();}}}