1. 目錄結(jié)構(gòu)
目錄結(jié)構(gòu)
2. 獲取SqlSessionFactory
SqlSessionFactoryBuilder有5個(gè)build()方法用于創(chuàng)建SqlSessionFactory:
SqlSessionFactory build(InputStream inputStream)
SqlSessionFactory build(InputStream inputStream, String environment)
SqlSessionFactory build(InputStream inputStream, Properties properties)
SqlSessionFactory build(InputStream inputStream, String env, Properties props)
SqlSessionFactory build(Configuration config)
其中inputStream就是mybatis配置文件的Reader實(shí)例斗搞,可選的參數(shù)是environment 和 properties,分別用來(lái)決定代碼環(huán)境和參數(shù)文件掸掸。
由于參數(shù)的獲取方式有多種湿弦,mybatis于是按照優(yōu)先級(jí)來(lái)獲取參數(shù):
方法傳遞參數(shù) > properties 元素的類路徑 resource或者url指定的元素 > properties 元素體指定的屬性
最后的一個(gè)方法,傳入的參數(shù)是Configuration凿将。其實(shí)Configuration類校套,包含了創(chuàng)建SqlSessionFactory所需要的所有的內(nèi)容:
DataSource dataSource = BaseDataTest.createBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.setLazyLoadingEnabled(true);
configuration.setEnhancementEnabled(true);
configuration.getTypeAliasRegistry().registerAlias(Blog.class);
configuration.getTypeAliasRegistry().registerAlias(Post.class);
configuration.getTypeAliasRegistry().registerAlias(Author.class);
configuration.addMapper(BoundBlogMapper.class);
configuration.addMapper(BoundAuthorMapper.class);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(configuration);
這其實(shí)和mybatis 的配置文件一樣
<?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="${db.driver}"/>
<property name="url" value="jdbc:mysql://${db.host}:3306/${db.database}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="data/TestMapper.xml"/>
</mappers>
</configuration>
3. 獲取SqlSession
SqlSessionFactory中用于創(chuàng)建SqlSession的方法
SqlSession openSession()
SqlSession openSession(boolean autoCommit)
SqlSession openSession(Connection connection)
SqlSession openSession(TransactionIsolationLevel level)
SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level)
SqlSession openSession(ExecutorType execType)
SqlSession openSession(ExecutorType execType, boolean autoCommit)
SqlSession openSession(ExecutorType execType, Connection connection)
在創(chuàng)建SqlSession時(shí)需要考慮:
- session是否需要使用事務(wù)或者自動(dòng)提交功能(開(kāi)啟事務(wù)就不自動(dòng)提交了)
- 是否需要自定義數(shù)據(jù)源配置(connection)
- 是否需要復(fù)用預(yù)處理語(yǔ)句和批量更新處理
4. 使用映射器Mapper
- 定義映射器接口
public interface TestMapper {
void insert(@Param("user") User user);
select(@Param("name") String name);
List<User> selectAll();
}
- 定義映射器xml文件: 指定namespace為定義的映射器接口
<?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="data.TestMapper">
<select id="select" resultType="model.User">
select name,age from mybatis where name=#{name}
</select>
<select id="selectAll" resultType="model.User">
select name,age from mybatis
</select>
<insert id="insert" parameterType="model.User">
insert into mybatis(name,age) values(#{user.name},#{user.age})
</insert>
</mapper>
- 在mybatis-config.xml 文件中,指定mapper
<mappers>
<mapper resource="data/TestMapper.xml"/>
</mappers>
- 使用SqlSession創(chuàng)建Mapper
TestMapper mapper = session.getMapper(TestMapper.class);
mapper.insert(user);
List<User> users = mapper.selectAll();
User userSelected = mapper.select("peggi");