MyBatis是什麼java
MyBatis是什麼,MyBatis的jar包中有它的官方文檔,文檔是這麼描述MyBatis的:mysql
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
翻譯過來就是:MyBatis是一款支持普通SQL查詢、存儲過程和高級映射的持久層框架。MyBatis消除了幾乎全部的JDBC代碼、參數的設置和結果集的檢索。MyBatis可使用簡單的XML或註解用於參數配置和原始映射,將接口和Java POJO(普通Java對象)映射成數據庫中的記錄。sql
本文先入門地搭建表、創建實體類、寫基礎的配置文件、寫簡單的Java類,從數據庫中查出數據,深刻的內容後面的文章再逐一研究。數據庫
建表、創建實體類mybatis
從簡單表開始研究MyBatis,因此咱們先創建一張簡單的表:app
create table student ( studentId int primary key auto_increment not null, studentName varchar(20) not null, studentAge int not null, studentPhone varchar(20) not null )charset=utf8 insert into student values(null, 'Jack', 20, '000000'); insert into student values(null, 'Mark', 21, '111111'); insert into student values(null, 'Lily', 22, '222222'); insert into student values(null, 'Lucy', 23, '333333'); commit;
一個名爲student的表格,裏面存放了student相關字段,固然此時咱們須要有一個Java實體類與之對應:框架
public class Student { private int studentId; private String studentName; private int studentAge; private String studentPhone; public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getStudentAge() { return studentAge; } public void setStudentAge(int studentAge) { this.studentAge = studentAge; } public String getStudentPhone() { return studentPhone; } public void setStudentPhone(String studentPhone) { this.studentPhone = studentPhone; } public String toString() { return "StudentId:" + studentId + "\tStudentName:" + studentName + "\tStudentAge:" + studentAge + "\tStudentPhone:" + studentAge; } }
OK,這樣student表及其對應的實體類Student.java就建立好了。dom
寫config.xml文件ui
在寫SQL語句以前,首先得有SQL運行環境,所以第一步是配置SQL運行的環境。建立一個Java工程,右鍵src文件夾,建立一個普通文件,取個名字叫作config.xml,config.xml裏這麼寫:this
<?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> <typeAliases> <typeAlias alias="Student" type="com.xrq.domain.Student"/> </typeAliases> <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/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="student.xml"/> </mappers> </configuration>
這就是一個最簡單的config.xml的寫法。接着右鍵src,建立一個普通文件,命名爲student.xml,和mapper裏面的一致(resource沒有任何的路徑則表示student.xml和config.xml同路徑),student.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="com.xrq.StudentMapper"> <select id="selectStudentById" parameterType="int" resultType="Student"> <![CDATA[ select * from student where studentId = #{id} ]]> </select> </mapper>
專門有一個student.xml表示student表的sql語句,固然也能夠幾張表共用一個.xml文件,這個看本身的項目和我的喜愛。至此,MyBatis須要的兩個.xml文件都已經創建好,接下來須要作的就是寫Java代碼了。
Java代碼實現
首先要進入MyBatis的jar包:
第二個MySql-JDBC的jar包注意一下要引入,這個很容易忘記。
接着建立一個類,我起名字叫作StudentOperator,因爲這種操做數據庫的類被調用頻繁可是調用者之間又不存在數據共享的問題,所以使用單例會比較節省資源。爲了好看,寫一個父類BaseOperator,SqlSessionFactory和Reader都定義在父類裏面,子類去繼承這個父類:
public class BaseOperator { protected static SqlSessionFactory ssf; protected static Reader reader; static { try { reader = Resources.getResourceAsReader("config.xml"); ssf = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } }
而後建立子類StudentOperator:
public class StudentOperator extends BaseOperator { private static StudentOperator instance = new StudentOperator(); private StudentOperator() { } public static StudentOperator getInstance() { return instance; } public Student selectStudentById(int studentId) { SqlSession ss = ssf.openSession(); Student student = null; try { student = ss.selectOne("com.xrq.StudentMapper.selectStudentById", 1); } finally { ss.close(); } return student; } }
這個類裏面作了兩件事情:
一、構造一個StudentOperator的單示例
二、寫一個方法根據studentId查詢出一個指定的Student
驗證
至此,全部步驟所有就緒,接着寫一個類來驗證一下:
public class MyBatisTest { public static void main(String[] args) { System.out.println(StudentOperator.getInstance().selectStudentById(1)); } }
運行結果爲:
StudentId:1 StudentName:Jack StudentAge:20 StudentPhone:20
看一下數據庫中的數據:
數據一致,說明以上的步驟都OK,MyBatis入門完成,後面的章節,將會針對MyBatis的使用細節分別進行研究。