Mybatis

mybatis 與 hibernate 區別 :

mybatis 相比于hibernate 需要關心很多細節? ? hibernate體系比較龐大

mybatis是什么?? 是持久層框架? 支持定制化SQL? (簡便的操作sql)

--------------------------------------------------------------------------------------------------------------------

要使用 MyBatis, 需要導入架包? ? 》》? ? [mybatis-x.x.x.jar]

如果使用 Maven 來構建項目,則需將下面的 dependency 代碼置于 pom.xml 文件中:

<dependency>

? <groupId>org.mybatis</groupId>

? <artifactId>mybatis</artifactId>

? <version>x.x.x</version>

</dependency>

===========================================================================================

每個基于 MyBatis 的應用都是以一個 SqlSessionFactory 的實例為中心的。SqlSessionFactory 的實例可以通過 SqlSessionFactoryBuilder 獲得。而 SqlSessionFactoryBuilder 則可以從 XML 配置文件或一個預先定制的 Configuration 的實例構建出 SqlSessionFactory 的實例。

-----------輸入流(inputStream)加載外部文件進行配置? ? ? ? 輸入流可以是多種形式 ,字節流 ,字符流等

-----------mybatis-config.xml? ? ? 配置文件:需要配置? 鏈接數據庫

String resource = "org/mybatis/example/mybatis-config.xml";(這是本地路徑加載方式)?

InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder();? ? ?

SqlSession session = sqlSessionFactory.openSession();

sqlSessionFactory 打開session的操作默認不支持事務的提交?

session.commit? ? 提交事務? ? ;? 或者指定new SqlSessionFactoryBuilder(true);? 則自動提交事務



XML 配置文件(configuration XML)中包含了對 MyBatis 系統的核心設置,包含獲取數據庫連接實例的數據源(DataSource)和決定事務作用域和控制方式的事務管理器(TransactionManager)。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="root"/>

? ? ? ? ? ? </dataSource>

? ? ? ? </environment>

? ? </environments>

? ? <mappers>

? ? ? ? <mapper resource="com/java12/mybatis/entity/BlogMapper.xml"/>

? ? </mappers>

</configuration>

environment 元素體中包含了事務管理和連接池的配置。mapper 元素體中包含了SQL代碼和映射定義的信息?



SQL代碼則是對映射信息的sql語句操作? (由此我們需要創建數據庫 ,需要數據庫里的數據表? 和對數據表內數據定義的實體類 )

現在我創建了數據庫mybatis數據庫,里面創建數據表user ,有ID ,Title 兩個屬性? 實體類定義示例:(生成setter方法,為了方便顯示值 生成ToString方法)

package com.java12.mybatis.entity;

public class Blog {

? ? private? int id;

? ? private String title;

? ? public void setId(int id)? {this.id = id;}

? ? public void setTitle(String title)? {this.title = title;}

? ? @Override

? ? public String toString() {

? ? ? ? return "Blog{" +"id=" + id +", title='" + title + '\'' +'}';}

}



BlogMapper.xml? 則是具體的對映射值信息的sql語句操作實例:

<?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="org.mybatis.example.BlogMapper">

? ? <select id="selectBlog" resultType="com.java12.mybatis.entity.Blog">

? ? select * from user where id = #{id}? ? ? ? ? ? ? ? ??? //? #{}? 在這里僅當做站位符?

? </select>

</mapper>



創建main方法測試示例:

package com.java12.mybatis.demo;

public class MybatisDemo {

? ? public static void main(String[] args) throws IOException {

//鏈接數據庫

? ? ? ? String resource = "mybatis-config.xml";

? ? ? ? InputStream inputStream = Resources.getResourceAsStream(resource);

? ? ? ? SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);

? ? ? ? System.out.println(build);

//執行sql語句的封裝方法

? ? ? ? SqlSession session = build.openSession(true);

//調用查詢方法查詢數據表內title存在的對應值

? ? ? ? selectUser("title",session);

? ? }

? ? public static? User? selectUser(String title,SqlSession session){

? ? ? ? UserMapper mapper = session.getMapper(UserMapper.class);

? ? ? ? User user = mapper.selectUser(title);

? ? ? ? return user;

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容