Mybaits入门之起航

前言

  Mybaits技术现在很多公司都在使用,它提供了简单的API可以快速进行数据库操作,所以不管是自己做系统还是找工作都有必要了解一下。

学习一门技术如果是入门的话要么买书要么就是阅读官方的文档,而且官方的文档全且及时更新,所以建议阅读官方文档,本系列入门教程其实就是官方文档的简易汉化版(哈哈)

1.通过XML建立SqlSessionFactory

  Mybaits应用是一个SqlSessionFactory的实例,而SqlSessionFactory时通过SqlSessionFactoryBuilder获得的。SqlSessionFactoryBuilder可以使用xml配置文件或者一些预先准备配置类实例化一个SqlSessionFactory

  通过xml配置获得一个SqlSessionFactory实例十分简单,推荐你使用类路径,但是你也可以使用任何的InputStream实例(简单的讲,你把读取的文件放到InputStream流里面就行,文件随你放哪里),这里注意一下Mybaits

有一个工具类叫Resources,他提供了很多简单的方法获取资源。

  xml配置文件包含mybaits系统的核心,包括数据库链接、事物控制等 这里我们先贴一个简单的代码

1 String resource ="cn/dota/uc/test/cfg.xml";
2         InputStream is = Resources.getResourceAsStream(resource);
3         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
<?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=""/>
                <property name="url" value=""/>
                <property name="username" value=""/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/xxx/xxx/BlogMapper.xml" />
    </mappers>
</configuration>

注意图中标红的需要制定为类路径

还有一种方式时通过Configuration类设置属性,这种方式侵入了代码大家可以自己看这段的文档,本系列就不介绍了

2.通过SqlSessionFactory获得SqlSession

  通过SqlSession你就可以开始操作数据库了如下代码

  现在版本的Mybatis有了新的方式可以使用接口进行开发如下

  一种实在BlogMapper.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">
<mapper namespace="cn.dota.uc.test.BlogMapper">
    <select id="selectBlog" resultType="cn.dota.uc.test.Blog">
        select * from Blog where id = #{id}
    </select>
</mapper>
SqlSession session = sqlSessionFactory.openSession();
        try{
            Blog blog = session.selectOne("cn.dota.uc.test.BlogMapper.selectBlog", 100);
        } finally{
            session.close();
        }

还有一种就是使用接口,首先我们要定义一个接口BlogMapper,代码如下

<?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="cn.dota.uc.test.BlogMapper">
</mapper>
SqlSession session = sqlSessionFactory.openSession();
try{
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  Blog blog = mapper.selectBlog(101);
} finally{
  session.close();
}

这种方式需要注意的就是配置文件的namespace 中 value必须和 接口相对应

public interface BlogMapper {
    @Select("SELECT * FROM blog where id = #{id}")
    Blog selectBlog(int id);
}

3总结

所以简单来说 Mybatis开发的步骤就是

  1. 编写配置文件和接口映射文件
  2. 使用SqlSessionFactoryBuilder读取配置文件并且获得SqlSessionFactory;
  3. 使用SqlSessionFactory获得SqlSession 然后可以通过两种方式进行数据库操作

生命周期和作用域

SqlSessionFactoryBuilder 仅仅用于创造sqlSessionFactory 所以可以使用完丢弃

SqlSessionFactory 单例和静态类是最好的方式

SqlSession 局部作用域就可以了

posted @ 2016-04-17 22:25  bowLoveDog  阅读(263)  评论(0编辑  收藏  举报