mybatis是一個基于java的持久層框架。
持久化:數(shù)據(jù)從瞬時狀態(tài)變?yōu)槌志脿顟B(tài)。------->主要是在Dao層。
瞬時狀態(tài):數(shù)據(jù)存在于內(nèi)存當中。
持久狀態(tài):數(shù)據(jù)存在于硬盤、磁盤中。
持久層:完成持久化工作的代碼塊
mtbatis是一個半自動化的ORM框架:即對象--Object、關系--Relationship、映射--Mapping。
java可以通過Mybatis訪問數(shù)據(jù)庫
先直接上項目.
- 創(chuàng)建數(shù)據(jù)庫mybatis,數(shù)據(jù)表user
里邊放兩個字段id和name - 根據(jù)數(shù)據(jù)表創(chuàng)建實體類User.java
- 編寫mybatis.cfg.xml配置文件
包括連接驅(qū)動,端口,用戶名,密碼,映射文件
<?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/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="user.mapper.xml" />
</mappers>
</configuration>
- 編寫映射文件user.mapper.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">
<!-- namespace屬性要和一個接口的全限定名保持一致 -->
<mapper namespace="mybatis.mapper.UserMapper">
<!-- resultMap元素用于定義映射規(guī)則 -->
<resultMap type="mybatis.entity.User" id="UserMapper">
<id property="id" column="id" />
<result property="name" column="name" />
</resultMap>
<select id="getUser" resultMap="UserMapper">
select * from user where id = #{id}
</select>
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
<insert id="insertUser" parameterType="mybatis.entity.User" useGeneratedKeys="true">
insert into user(id,name) values(#{id},#{name})
</insert>
<update id="updateUser" parameterType="mybatis.entity.User">
update user set name=#{name} where id = #{id}
</update>
</mapper>
- 定義mybatis映射文件(mapper接口)
namespace提到的接口
mapper中定義的方法參數(shù)與
user.mapper.xml中parameterType相同
import mybatis.entity.User;
public interface UserMapper {
public User getUser(Integer id);
public int deleteUser(Integer id);
public int insertUser(User user);
public int updateUser(User user);
}
- 編寫工具類
工具類中有兩個方法
i. 類型為SqlSessionFactory的getSessionFactory()
每個基于 MyBatis 的應用都是以一個 SqlSessionFactory 的實例為核心的。
該方法用于返回一個SqlSessionFactory對象
ii. 類型為SqlSession的getSession()
通過 SqlSession 實例來直接執(zhí)行已映射的 SQL 語句
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtil {
public static SqlSessionFactory getSessionFactory() throws IOException {
String resource ="mybatis.cfg.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
return sessionFactory;
}
public static SqlSession getSession() throws IOException {
SqlSessionFactory sessionFactory=getSessionFactory();
return sessionFactory.openSession();
}
}
- Test類
我們來看看程序執(zhí)行過程
首先創(chuàng)建一個SqlSession的對象,這個對象通過工具類的getSession()方法創(chuàng)建
然后這個方法里又創(chuàng)建了一個SqlSessionFactory對象
SqlSessionFactory 的實例可以通過 SqlSessionFactoryBuilder 獲得。
SqlSessionFactoryBuilder可以從xml配置文件中獲取SqlSessionFactory的實例
最后返回sessionFactory.openSession()相當于sqlSession對象
然后執(zhí)行映射的sql語句
public class Test {
public static void main(String[] args) throws IOException {
SqlSession sqlSession = MybatisUtil.getSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
User user=mapper.getUser(1);
System.out.println(user.getId()+" "+user.getName());
}
}
- 要注意的一點
在執(zhí)行curd時,每個操作都算數(shù)據(jù)庫的一個事務
所以在程序中需要手動提交一遍
session.commit();