mybatis
2020-03-04 本文已影响0人
cdz620
docs
glossary
关系路线: SqlSessionFactoryBuilder --> SqlSessionFactory --> SqlSession -query-> Mapper Instances
SqlSessionFactoryBuilder
- 功能:获取SqlSessionFactory
- recommend scope: method scope(i.e. a local method variable)
- 一般只要用一次,通过SqlSessionFactoryBuilder获得SqlSessionFactory
SqlSessionFactory
- 功能:创建SqlSession
- recommend scope: application scope, 应用程序存在就不该销毁或丢弃引用,
- 最好是单例实现
SqlSession
- 功能:会话连接用来查询
- recommend scope: thread scope,request scope, method scope,不要保存引用在类成员变量或者静态变量中
- 使用姿势:
SqlSession session = sqlSessionFactory.openSession();
try {
// do work
} finally {
session.close();
}
Mapper Instances
- SqlSession通过mapped statement查询到的实例,
- recommend scope: thread scope,request scope, method scope
mapped statements
查询语句
使用过程
1. Building SqlSessionFactory from XML
- 加载配置代码
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- 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>
<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>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
- JavaConfig
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
2. Acquiring a SqlSession from SqlSessionFactory
SqlSession session = sqlSessionFactory.openSession();
try {
Blog blog = session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
} finally {
session.close();
}
3. query
SqlSession session = sqlSessionFactory.openSession();
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
} finally {
session.close();
}
4. 配置mapped statements
xml
<?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="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
- Namespaces: 尽量用包名路径
- Name Resolve: 如果不存在歧义的Mapper statement id,可以直接使用“selectBlog”,建议还是用全名
- 查询:
Blog blog = session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
annotation
package org.mybatis.example;
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}
- 查询:
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);