在以前的文章中都是學習java基礎相關知識,近期抽出時間,整理下mybatis的學習筆記。html
簡單來講mybatis是一個ORM框架,官方文檔以下寫到java
MyBatis 是一款優秀的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎全部的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 能夠使用簡單的 XML 或註解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄mysql
create table tbl_employee(id int(11) primary key auto_increment,last_name varchar(255),gender char(1),email varchar(255));
複製代碼
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
複製代碼
用了mybatis 以及鏈接數據庫工具還有就是log4j 用於打印sql語句。sql
package com.zhang;
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Employee() {
}
public Employee(Integer id, String lastName, String email, String gender) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public void setId(Integer id) {
this.id = id;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getId() {
return id;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getGender() {
return gender;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' + ", email='" + email + '\'' + ", gender='" + gender + '\'' +
'}';
}
}
複製代碼
<?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://localhost:3306/employee"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml"/>
</mappers>
</configuration>
複製代碼
<?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="com.zhang.EmployeeMapper">
<select id="selectEmp" resultType="com.zhang.Employee">
select id,last_name lastName,email,gender from tbl_employee where id = #{id}
</select>
</mapper>
複製代碼
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession= sqlSessionFactory.openSession();
try{
Employee employee= sqlSession.selectOne("com.zhang.EmployeeMapper.selectEmp",1);
System.out.println(employee);
}finally {
sqlSession.close();
}
複製代碼
能夠看到mybatis鏈接了數據庫 併發起了查詢,並將查詢結果封裝成了了一個java bean對象。數據庫
mybatis-config.xml是全局的一個配置文件,其最主要的配置項即是鏈接數據庫的配置,指定庫,端口,用戶名密碼等,EmployeeMapper.xml是一個映射文件,其中記載了須要執行的sql語句和結果的封裝類型。且在全局配置文件中引入加載。 咱們從配置文件中讀取數據流,得到sqlSessionFactory,而後構建sqlSession, 再用sqlSession.selectOne去傳入咱們指定好的sql,再傳入參數id=1; 那麼mybatis便構建出一條完整的sql語句編程
select id,last_name lastName,email,gender from tbl_employee where id = 1;
複製代碼
而後根據咱們事先指定的類型,將結果集封裝成咱們須要的一個對象。緩存
##總結 初步上手得出mybatis將構建sql一個環節交給了用戶,咱們須要指定返回值類型和傳入對應參數,mybatis便執行查詢且將結果集封裝,的確很方便。 本篇是初步上手,緩存動態配置sql且關注做者後續文章。 喜歡本文的朋友們,歡迎長按下圖關注訂閱號"個人編程筆記",收看更多精彩內容~~ bash