关于mybatis的相关介绍,本人不作多解释。我们来进入对mybatis的基本认识。
1.下载mybatis的jar包,放入你的项目文件内加载。jar包可从mybatis官网下载,源码包建议一起下载。
2.从XML构建SqlSessionFactory,参照源码示例:
String resource="org/mybatis/mybatis-config.xml";
InputStream inputStream=Resource.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
mybatis采用Builder模式来构建SqlSessionFactory,类SqlSessionFactoryBuilder为构造器,查看该类的的部分源码
public class SqlSessionFactoryBuilder{
public SqlSessionFactory build(Reader reader){
return build(read,null,null);
}
public SqlSessionFactory build(InputStream inputStream){
return build(inputStream,null,null);
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("构建SqlSession出错", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
// 忽略的异常
}
}
}
}
从上面的代码可知:通过调用build方法来生成SqlSessionFactory。
3.构建mybatis-config.xml配置文件(以下仅仅是一个简单的例子)
<?xml version="1.0" encoding="UTF-8" ?>
由此便可以通过XML来构建SqlSessionFactory了。
<?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>
4.同时我们也可以直接用java代码来构建SqlSessionFactory
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();//获取数据源信息(连接数据库相关的信息)
TransactionFactory transactionFactory = new JdbcTransactionFactory();//mybatis的jdbc事务管理
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);//配置环境
configuration.addMapper(BlogMapper.class);//映射
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);//通过SqlSessionFactoryBuilder构建
5.通过SqlSessionFactory来获得一个SqlSession
SqlSession session = sqlSessionFactory.openSession();//打开SqlSession会话
try {
Blog blog = session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);//此处也可改写为下方代码,更显简洁
} finally {
session.close();
}
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
注:BlogMapper是一个接口,接口中定义了的方法含有selectBlog方法,与下面的sql配置文件成映射关系。
6.映射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">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
当然这只是mybatis的一些小小的入门知识,mybatis的其他强大之处,在目前也是可见的。