MyBatis概念和”安裝“
阿新 • • 發佈:2018-09-19
9.png word 關系 Opens 框架 層次 映射文件 span 最好
MyBatis概念
MyBatis的前身就是iBatis,本是apache的一個開源項目,2010年這個項目由apahce sofeware foundation 遷移到了google code,並且改名為MyBatis。
MyBatis是一個數據持久層(ORM)框架。
MyBatis是把實體類和sql語句之間建立了映射關系,而Hibernate在實體類和數據庫之間建立了映射關系。
總體來說 MyBatis 主要完成兩件事情
- 根據 JDBC 規範建立與數據庫的連接;
- 通過Annotaion/XML+JAVA反射技術,實現 Java 對象與關系數據庫之間相互轉化。
MyBatis的優點
- 基於SQL語法,簡單易學。
- 能了解底層組裝過程。
- SQL語句封裝在配置文件中,便於統一管理與維護,降低了程序的耦合度。
- 程序調試方便。
MyBatis層次結構
MyBatis安裝(環境配置)*.* 例子是最好的筆記
- 準配需要的jar包:mybatis-3.2.6.jar和mysql-connector-java-5.1.11-bin.jar
2.創建mybatis-config.xml,並配置數據庫環境
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"View Code4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 環境 --> 7 <environments default="developments"> 8 <environment id="mysql"> 9 <transactionManager type="JDBC"></transactionManager> 10 <dataSourcetype="POOLED"> 11 <property name="driver" value="com.mysql.jdbc.Driver"/> 12 <property name="url" value="jdbc:mysql://127.0.0.1:3306/mydbone"/> 13 <property name="username" value="root" /> 14 <property name="password" value="199751hwl" /> 15 </dataSource> 16 </environment> 17 </environments> 18 19 </configuration>
3.創建一個實體Person.java
package com.model.system; public class Person { private Integer persId; private String persName; private String persSex; private String persHobby; public Person() {} public Person(Integer persId, String persName, String persSex, String persHobby) { super(); this.persId = persId; this.persName = persName; this.persSex = persSex; this.persHobby = persHobby; } public Integer getPersId() { return persId; } public void setPersId(Integer persId) { this.persId = persId; } public String getPersName() { return persName; } public void setPersName(String persName) { this.persName = persName; } public String getPersSex() { return persSex; } public void setPersSex(String persSex) { this.persSex = persSex; } public String getpersHobby() { return persHobby; } public void setpersHobby(String persHobby) { this.persHobby = persHobby; } @Override public String toString() { return "Person [persId=" + persId + ", persName=" + persName + ", persSex=" + persSex + ", persHobby=" + persHobby + "]"; } }View Code
4.為實體配置對應的映射文件Person.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.model.system.Person"> <resultMap type="com.model.system.Person" id="person"> <result column="persId" property="persId" /> <result column="persName" property="persName" /> <result column="persSex" property="persSex" /> <result column="persHobby" property="persHobby" /> </resultMap> <!-- 查詢用戶通過用戶ID --> <select id="queryPersonByPersonId" parameterType="java.lang.Integer" resultMap="person"> select * from person where persId = #{persId} </select> <!-- 添加用戶通過用戶ID --> <select id="insertPerson" parameterType="java.lang.String" resultMap="person"> INSERT INTO person(persName,persSex,persHobby) VALUE (#{persName},#{persSex},#{persHobby}) </select> </mapper>View Code
5.在mybatis-config.xml,添加User.xml映射文件
1 <!-- 添加映射文件 --> 2 <mappers> 3 <mapper resource="com/mapper/system/Person.xml" /> 4 </mappers>View Code
6. 調用類(測試類)
1 package com; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 import com.model.system.Person; 12 13 public class TestQueryPersonById { 14 15 public static void main(String[] args) throws IOException { 16 17 // 從XML中創建SqlSessionFactory 18 String resource = "mybatis-config.xml"; 19 20 Reader reader = Resources.getResourceAsReader(resource); 21 SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader, "mysql"); 22 // 獲取session 23 SqlSession session = ssf.openSession(); 24 25 Person person = session.selectOne("com.model.system.Person.queryPersonByPersonId", 1); 26 // 通過命名空間查找sql 27 System.out.println(person); 28 } 29 }
MyBatis概念和”安裝“