1.MyBatis是持久化層框架(SQL映射框架)-操作數據庫
實質就是將JDBC的一系列操作進行封裝,並且將SQL語句部分轉換成配置文件(更好的實現了Java文件和數據庫操作解耦)
2.用MyBatis實現操作數據庫步驟:
1>導包:①mybatis-3.4.1.jar ② mysql-connector-java-5.1.37-bin.jar ③log4j.jar
2>創建全局配置文件:
<?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://localhost:3306/tx"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
3>編寫每一個方法如何向數據庫發送sql語句,如何執行的配置文件。(本來應該是實現Dao接口的實現類,在此xml配置文件相當於實現Dao接口的接口類)
指定該配置文件實現的是哪個接口,每個方法如何實現
<?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">
<!-- namespace:寫接口的全類名,告訴MyBatis該配置文件是實現哪個Dao接口的 -->
<mapper namespace="com.cuco.dao.BookDao">
<!-- select標籤:定義一個查詢操作 id:相對於配置是對於某個方法的實現 resultType:指定方法運行後的返回值類型 -->
<!-- #{是姓名}表示的是取方法中參數傳遞過來的值 -->
<select id="getById" resultType="com.cuco.bean.Book">
select * from book where isbn = #{isbn}
</select>
</mapper>
4>上面的針對每個Dao接口的實現類配置文件,MyBatis默認不知道,因此需要在全局配置文件中註冊
<!-- 引入自己編寫的每一個接口的實現文件 -->
<mappers>
<!-- resource:表示從類路徑下找資源 -->
<mapper resource="BookDao.xml"/>
</mappers>
Test測試:
public class MyBatisTest {
@Test
public void test() throws IOException {
//根據全局配置文件創建出一個SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//獲取和數據庫的一次會話(相當於getConnection,獲取連接)
SqlSession openSession = sqlSessionFactory.openSession();
//使用SqlSession操作數據庫
BookDao bookDao = openSession.getMapper(BookDao.class);
Book book = bookDao.getById("ISBN-001");
System.out.println(book);
}
}
①根據全局配置文件創建一個SqlSessionFactory對象
②通過SqlSessionFactory獲取sqlSession對象操作數據庫
③使用sqlSession對象操作數據庫
5>導入dtd約束文件
3.實現一次增刪改查操作
<?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">
<!--
public Book getById(String isbn);
public int updateBook(Book book);
public int deleteBook(Book book);
public int insertBook(Book book);
-->
<mapper namespace="com.cuco.dao.BookDao">
<select id="getById" resultType="com.cuco.bean.Book">
select * from book where isbn=#{isbn}
</select>
<update id="updateBook">
update book set book_name=#{book_name},price=#{price}
</update>
<delete id="deleteBook">
delete from book where isbn=#{isbn}
</delete>
<insert id="insertBook">
insert into book(isbn,book_name,price) values(#{isbn},#{book_name},#{price})
</insert>
</mapper>
public class MyBatisTest {
SqlSessionFactory sqlSessionFactory ;
@Before
public void initSqlSessionFactory() throws IOException{
//根據全局配置文件創建出一個SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
//測試添加
@Test
public void testInsert() throws IOException {
//獲取和數據庫的一次會話(相當於getConnection,獲取連接)
SqlSession openSession = sqlSessionFactory.openSession(true);
//使用SqlSession操作數據庫,獲取到Dao接口的實現(映射器)
BookDao bookDao = openSession.getMapper(BookDao.class);
try{
bookDao.insertBook(new Book("ISBN-031","book31",30));
}finally{
openSession.close();
}
}
//測試刪除
@Test
public void testDel() throws IOException {
//獲取和數據庫的一次會話(相當於getConnection,獲取連接)
SqlSession openSession = sqlSessionFactory.openSession();
//使用SqlSession操作數據庫,獲取到Dao接口的實現(映射器)
BookDao bookDao = openSession.getMapper(BookDao.class);
try{
Book book = new Book("ISBN-001","book01",100);
bookDao.deleteBook(book);
}finally{
openSession.commit();
openSession.close();
}
}
//測試查詢
@Test
public void test() throws IOException {
//獲取和數據庫的一次會話(相當於getConnection,獲取連接)
SqlSession openSession = sqlSessionFactory.openSession();
//使用SqlSession操作數據庫,獲取到Dao接口的實現(映射器)
BookDao bookDao = openSession.getMapper(BookDao.class);
try{
Book book = bookDao.getById("ISBN-001");
System.out.println(book);
}finally{
openSession.close();
}
}
}
兩個文件:1>全局配置文件:mybatis-config.xml:指導mybatis正確運行的一些全局設置
2>SQL映射文件:BookDao.xml:相當於是實現BookDao接口的實現類
細節:1>獲取到的是接口的代理對象;mybatis自動創建
2>SqlSessionFactory只需要創建一次
4.引入外部配置文件
mybatis-config.xml中的標籤:
1><properties></properties>:引入外部文件
<configuration>
<!-- 引用外部配置文件
resource:從類路徑下開始引用
url:引用磁盤路徑或者網絡路徑的資源
-->
<properties resource="dbconfig.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置連接池 -->
<dataSource type="POOLED">
<!-- 用${}取出配置文件中的值 -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 引入自己編寫的每一個接口的實現文件 -->
<mappers>
<!-- resource:表示從類路徑下找資源 -->
<mapper resource="BookDao.xml"/>
</mappers>
</configuration>
2><settings></settings>
3><typeAliases></typeAliases>:類型別名:爲常用的類型起別名;(但是要注意標籤順序,否則會報錯)
<configuration>
<!-- 引用外部配置文件
resource:從類路徑下開始引用
Url:引用磁盤路徑或者網絡路徑的資源-->
<properties resource="dbconfig.properties"></properties>
<typeAliases>
<!-- typeAlias:就是爲一個javaBean起別名;別名默認就是類名(不缺分大小寫),配置文件中就可以使用別名 -->
<!-- <typeAlias type="com.cuco.bean.Book" alias="bo"/> -->
<package name="com.cuco.bean"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置連接池 -->
<dataSource type="POOLED">
<!-- 用${}取出配置文件中的值 -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 引入自己編寫的每一個接口的實現文件 -->
<mappers>
<!-- resource:表示從類路徑下找資源 -->
<mapper resource="BookDao.xml"/>
</mappers>
</configuration>
<mapper namespace="com.cuco.dao.BookDao">
<select id="getById" resultType="bo">
select * from book where isbn=#{isbn}
</select>
<update id="updateBook">
update book set book_name=#{book_name},price=#{price}
</update>
<delete id="deleteBook">
delete from book where isbn=#{isbn}
</delete>
<insert id="insertBook">
insert into book(isbn,book_name,price) values(#{isbn},#{book_name},#{price})
</insert>
</mapper>
4> typeHandler:類型處理器
5>objectFactory
6>plugins插件
7>environments:配置環境:都需要一個事務管理器和一個數據源。但實際上配置數據源和事務管理器都是用Spring 的IOC容器配置
8>databaseIdProvider:用來考慮數據庫移植性的
9>mappers:寫好的SQL映射文件需要使用該標籤進行註冊
可以通過<mappers>
<package name="com.cuco.dao"/>
</mappers>實現批量註冊(將實現接口的xml文件放在conf源文件下的dao包中)