1. 从XML中构建SqlSessionFactory
String resource = “org/mybatis/example/Configuration.xml”;
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
mybatis-config.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>
<typeAliases>
<typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
...
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/manager/data/maps/StudentMapper.xml" />
</mappers>
</configuration>
2.从SqlSessionFactory中获取SqlSession
SqlSession session = sqlMapper.openSession();
session.selectOne(“com.huaxin.mapper.BlogMapper.selectBlog”, 101);
或者
session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
3.范围和生命周期
SqlSessionFatoryBuilder 方法范围 可以用spring beanfactory
SqlSessionFactory 应用范围
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- <property name="mapperLocations" value="classpath:sample/config/mappers/**/*.xml" -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
SqlSession 请求范围 Spring依赖注入
4.XML映射配置文件
Configuration配置
Properties属性
Settings设置
typeAliases类型命名
objectFactory对象工厂
plugins 插件
environment环境变量
transactionManager事务管理器
dataSource数据源
映射器
5. 一对一连接查询
1)延迟加载方式
<select>语句不写连接
<resultMap>
<association column=”blog_author_id” select=”selectAuthor” />
<!— 复合主键 column=”{prop1=col1, prop2=col2}” à
</resultMap>
2)连接查询方式
<select>语句写连接
<resultMap>
<association column=”blog_author_id” resultMap=”authorResult” />
<!— 复合主键 column=”{prop1=col1, prop2=col2}” à
</resultMap>
6.一对多连接查询
1)s<select>语句不写连接
<resultMap>
<collection column=”blog_author_id” select=”selectAuthor” ofType=”集合里元素的类型”/>
</resultMap>
2)连接查询方式
<select>语句写连接
<resultMap>
<collection column=”blog_author_id” resultMap=”selectAuthor” ofType=”集合里元素的类型”/>
</resultMap>
注意:对于不是用left join连接的,而用where 全连接的,只需用resultType就行了,关联属性,MYBATIS会自动填补相应的属性。
7.获取自动增长主键
1)<insert>
<selectKey resultType=”int” keyProperty=”id”>
<!—mysql --> SELECT LAST_INSERT_ID()
<!—oracle -->SELECT SEQ_TEST.NEXTVAL FROM DUAL
<!—mssql -->SELECT @@IDENTITY
</selectKey>
</insert>
Spring整合的 三种方式查询
1)映射类
<bean id=”userMapper” class=”org.mybatis.spring.mapper.MapperFactoryBean”>
<property name=”mapperInterface” value=”org.mybatis.spring.sample.mapper.UserMapper”/>
<property name=”sqlSessionFactory” ref=”sqlSessionFactory”/>
</bean>
2)使用SqlSessionTemplate
public Notice get(Integer id) {
return (Notice) this.sqlSessionTemplate.selectOne("com.huaxin.dao.NoticeDao.get", id);
}
3)UserDaoImpl extends SqlSessionDaoSupport
继承父类getSqlSession()方法
<bean id=”userMapper” class=”com.huaxin.dao.UserDaoImpl”>
<property name=”sqlSessionFactory” ref=”sqlSessionFactory”/>
</bean>