mybatis 與 hibernate 區(qū)別 :
mybatis 相比于hibernate 需要關心很多細節(jié)? ? hibernate體系比較龐大
mybatis是什么?? 是持久層框架? 支持定制化SQL? (簡便的操作sql)
--------------------------------------------------------------------------------------------------------------------
要使用 MyBatis, 需要導入架包? ? 》》? ? [mybatis-x.x.x.jar]
如果使用 Maven 來構(gòu)建項目,則需將下面的 dependency 代碼置于 pom.xml 文件中:
<dependency>
? <groupId>org.mybatis</groupId>
? <artifactId>mybatis</artifactId>
? <version>x.x.x</version>
</dependency>
===========================================================================================
每個基于 MyBatis 的應用都是以一個 SqlSessionFactory 的實例為中心的。SqlSessionFactory 的實例可以通過 SqlSessionFactoryBuilder 獲得。而 SqlSessionFactoryBuilder 則可以從 XML 配置文件或一個預先定制的 Configuration 的實例構(gòu)建出 SqlSessionFactory 的實例。
-----------輸入流(inputStream)加載外部文件進行配置? ? ? ? 輸入流可以是多種形式 ,字節(jié)流 ,字符流等
-----------mybatis-config.xml? ? ? 配置文件:需要配置? 鏈接數(shù)據(jù)庫
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 系統(tǒng)的核心設置,包含獲取數(shù)據(jù)庫連接實例的數(shù)據(jù)源(DataSource)和決定事務作用域和控制方式的事務管理器(TransactionManager)。XML 配置文件的詳細內(nèi)容后面再探討,這里先給出一個簡單的示例:
<?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語句操作? (由此我們需要創(chuàng)建數(shù)據(jù)庫 ,需要數(shù)據(jù)庫里的數(shù)據(jù)表? 和對數(shù)據(jù)表內(nèi)數(shù)據(jù)定義的實體類 )
現(xiàn)在我創(chuàng)建了數(shù)據(jù)庫mybatis數(shù)據(jù)庫,里面創(chuàng)建數(shù)據(jù)表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>
創(chuàng)建main方法測試示例:
package com.java12.mybatis.demo;
public class MybatisDemo {
? ? public static void main(String[] args) throws IOException {
//鏈接數(shù)據(jù)庫
? ? ? ? String resource = "mybatis-config.xml";
? ? ? ? InputStream inputStream = Resources.getResourceAsStream(resource);
? ? ? ? SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
? ? ? ? System.out.println(build);
//執(zhí)行sql語句的封裝方法
? ? ? ? SqlSession session = build.openSession(true);
//調(diào)用查詢方法查詢數(shù)據(jù)表內(nèi)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;