文章目錄
-
-
- 一、建立student表(id,name,email,age)
- 二、建立maven項目、修改pom.xml檔案
-
- 1、設定編碼方式
- 2、加入依賴 mybatis依賴, mysql驅動, junit單元測試
- 3、加入資源插件
- 3、建立實體類Student。定義屬性, 屬性名和列名保持一緻
- 4、建立Dao接口, 定義操作資料庫的方法
- 5、建立xml檔案(mapper檔案), 寫sql語句
- 6、建立mybatis的主配置檔案(xml檔案):僅有一個, 放在resources目錄下
- 7、建立測試的内容
- 8、MyBatis的一些重要對象
-
一、建立student表(id,name,email,age)
二、建立maven項目、修改pom.xml檔案
1、設定編碼方式
2、加入依賴 mybatis依賴, mysql驅動, junit單元測試
3、加入資源插件
<!--設定源代碼的編碼方式-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--mybatis架構-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--mysql驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<!--junit驅動-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--資源插件: 處理src/main/java目錄中的xml-->
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目錄-->
<includes><!--包括目錄下的.properties,.xml 檔案都會掃描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
如果不配置資源插件,後面xml檔案是找不到的:
3、建立實體類Student。定義屬性, 屬性名和列名保持一緻
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
}
4、建立Dao接口, 定義操作資料庫的方法
public interface StudentDao {
Student selectStudentById(Integer id);
}
5、建立xml檔案(mapper檔案), 寫sql語句
mybatis官方文檔:https://mybatis.org/mybatis-3/zh/configuration.html
<?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.macay.dao.StudentDao">
<!--
1.限制檔案
http://mybatis.org/dtd/mybatis-3-mapper.dtd
限制檔案作用: 定義和限制目前檔案中可以使用的标簽和屬性,以及标簽出現的順序。
2.mapper是根标簽
namespace: 命名空間,必須有值,不能為空。唯一值。
推薦使用Dao接口的全限定名稱(全路徑)。
作用: 參與識别sql語句的作用。
3.在mapper裡面可以寫 <insert>,<update>,<delete>,<select>等标簽。
<insert>裡面是 insert語句,表示執行的insert操作
<update>裡面是 update語句
<delete>裡面是 delete語句
<select> 裡面是 select語句
-->
<!--查詢一個學生Student
<select>:表示查詢操作, 裡面是select 語句
id: 要執行的sql語句的唯一辨別, 是一個自定義字元串。
推薦使用dao接口中的方法名稱
resultType:告訴mybatis,執行sql語句,把資料指派給那個類型的java對象。
resultType的值現在使用的java對象的全限定名稱
#{studentId}:占位符, 表示從java程式中傳入過來的資料
-->
<select id="selectStudentById" resultType="com.macay.entity.Student">
select id, name, email, age from student where id =100
</select>
</mapper>
6、建立mybatis的主配置檔案(xml檔案):僅有一個, 放在resources目錄下
1.定義建立連接配接執行個體的資料源(DataSource)對象
2.指定其他mapper檔案的位置
<?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>
<!--設定日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置資料源: 建立Connection對象。-->
<dataSource type="POOLED">
<!--driver:驅動的内容-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--連接配接資料庫的url-->
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8"/>
<!--使用者名-->
<property name="username" value="root"/>
<!--密碼-->
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--指定其他mapper檔案的位置:
其他其他mapper檔案目的是找到其他檔案的sql語句
-->
<mappers>
<!--
使用mapper的resource屬性指定mapper檔案的路徑。
這個路徑是從target/classes路徑開啟的
使用注意:
resource=“mapper檔案的路徑,使用 / 分割路徑”
一個mapper resource 指定一個mapper檔案,若有多個,需要些多個mapper标簽
-->
<mapper resource="com/macay/dao/StudentDao.xml"/>
<!--<mapper resource="com/bjpowernode/dao/OrderDao.xml"/>
<mapper resource="com/bjpowernode/dao/UserDao.xml"/>-->
</mappers>
</configuration>
7、建立測試的内容
public class MyTest {
@Test
public void testSelectById() throws IOException {
//調用mybatis某個對象的方法,執行mapper檔案中的sql語句
//mybatis核心類: SqlSessionFactory
//1.定義mybatis主配置檔案的位置, 從類路徑開始的相對路徑
String config="mybatis.xml";
//2.讀取主配置檔案。使用mybatis架構中的Resources類
InputStream inputStream = Resources.getResourceAsStream(config);
//3.建立SqlSessionFactory對象, 使用SqlSessionFactoryBuidler類
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
//4.擷取SqlSession對象。
SqlSession session = factory.openSession();
//5.指定要執行的sql語句的 id
// sql的id = namespace+"."+ select|update|insert|delete标簽的id屬性值
String sqlId = "com.macay.dao.StudentDao"+"."+"selectStudentById";
// 6.通過SqlSession的方法,執行sql語句
Student student = session.selectOne(sqlId);
System.out.println("使用mybatis查詢一個學生:"+student);
// 7.關閉SqlSession對象
session.close();
}
}
結果如下:
8、MyBatis的一些重要對象
(1)、 Resources : mybatis架構中的對象, 一個作用讀取 主配置資訊。
(2)、SqlSessionFactoryBuilder:負責建立SqlSessionFactory對象
(3)、SqlSessionFactory: 重要對象
SqlSessionFactory是重量級對象:建立此對象需要使用更多的資源和時間。 在項目中有一個就可以了。
DefaultSqlSessionFactory實作類
SqlSessionFactory接口:作用是SqlSession的工廠, 就是建立SqlSession對象。
SqlSessionFactory接口中的方法
openSession(): 擷取一個預設的SqlSession對象, 預設是需要手工送出事務的。
openSession(boolean): boolean參數表示是否自動送出事務。
true: 建立一個自動送出事務的SqlSession
false: 等同于沒有參數的openSession
(4)、SqlSession對象
SqlSession對象是通過SqlSessionFactory擷取的。 SqlSession本身是接口
DefaultSqlSession: 實作類
SqlSession作用是提供了大量的執行sql語句的方法:
selectOne:執行sql語句,最多得到一行記錄,多餘1行是錯誤。
selectList:執行sql語句,傳回多行資料
selectMap:執行sql語句的,得到一個Map結果
insert:執行insert語句
update:執行update語句
delete:執行delete語句
commit:送出事務
rollback:回顧事務
注意SqlSession對象不是線程安全的, 使用的步驟:
①:在方法的内部,執行sql語句之前,先擷取SqlSession對象
②:調用SqlSession的方法,執行sql語句
③:關閉SqlSession對象,執行SqlSession.close()