官方地址:https://github.com/mybatis/mybatis-3
准備:
官方中文文檔地址:http://www.mybatis.org/mybatis-3/zh/getting-started.html
1、導入MyBatis的jar包和mysql連接驅動(maven):
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
2、新建數據庫和表,並在表中插入幾條測試數據:
3、創建javabean對應數據表:
Employee.java:

1 package yyc.mybatis.bean; 2 3 public class Employee { 4 private Integer id; 5 private String lastName; 6 private char gender; 7 private String email; 8 public Integer getId() { 9 return id; 10 } 11 public void setId(Integer id) { 12 this.id = id; 13 } 14 15 public String getLastName() { 16 return lastName; 17 } 18 public void setLastName(String lastName) { 19 this.lastName = lastName; 20 } 21 public char getGender() { 22 return gender; 23 } 24 public void setGender(char gender) { 25 this.gender = gender; 26 } 27 public String getEmail() { 28 return email; 29 } 30 public void setEmail(String email) { 31 this.email = email; 32 } 33 @Override 34 public String toString() { 35 return "Employee [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]"; 36 } 37 38 }
一、使用 XML 構建 SqlSessionFactory
官方文檔中給的是:
String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
我在這里將它封裝為一個類的靜態方法:
MySqlSessionFacoty.java:

1 package yyc.mybatis.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 10 public class MySqlSessionFacoty { 11 12 public static SqlSessionFactory getSqlSessionFactory() throws IOException{ 13 String resource = "mybatis-config.xml"; 14 InputStream inputStream = Resources.getResourceAsStream(resource); 15 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 16 return sqlSessionFactory; 17 } 18 }
映射SQL語句:
EmployeeMapperXml.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="yyc.mybatis.EmployeeMapper"> <!-- resultType:返回類型 #{id}:從傳遞過來的參數中獲取id值 --> <select id="selectEmp" resultType="yyc.mybatis.bean.Employee"> select id,last_name lastName,gender,email from tb1_employee where id = #{id} </select> </mapper>
XML 配置文件(configuration XML)中包含了對 MyBatis 系統的核心設置,包含獲取數據庫連接實例的數據源(DataSource)和決定事務作用域和控制方式的事務管理器(TransactionManager)。
mybatis-config.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> <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:///mybatis"/> <property name="username" value="root"/> <property name="password" value="123"/> </dataSource> </environment> </environments> <!-- 將我們寫好的sql映射文件注冊到全局配置文件中 --> <mappers> <!-- xml映射配置方式 --> <mapper resource="EmployeeMapperXml.xml"/> </mappers> </configuration>
測試方法:
/** * 1.根據xml配置文件(全局配置文件)創建一個SqlSessionFactory對象 * 2.sql配置文件:配置了每一個sql,以及sql的封裝規則 * 3.將sql配置文件注冊在全局配置文件 * 4.寫代碼: * (1)、根據全局配置文件得到SqlSessionFactory對象 * (2)、使用SqlSessionFactory獲取sqlsession,使用它來實現crud * 一個sqlsession就是代表和數據庫的一次會話,用完關閉 * @throws IOException */ @Test public void testXml() throws IOException{ SqlSessionFactory sqlSessionFactory = MySqlSessionFacoty.getSqlSessionFactory(); //2.獲取sqlSession實例,能直接執行已經映射的sql語句 SqlSession openSession = sqlSessionFactory.openSession(); try { //1.唯一標識符(namespace + id) //2.執行sql需要用到的參數 Employee employee = openSession.selectOne("yyc.mybatis.EmployeeMapper.selectEmp", 1); System.out.println(employee); } catch (Exception e) { e.printStackTrace(); }finally { openSession.close(); } }
輸出:
Employee [id=1, lastName=tom, gender=0, email=aa@163.com]