您的位置:首页 > 其它

第二章 MyBatis入门

2017-08-07 14:09 232 查看

爱玩酷原创(https://www.iplayers.cn

文章来自(第二章   MyBatis入门)

一、从 XML 中创造 SqlSessionFactory SqlSessionFactory SqlSessionFactory

加载资源配置

String resource = "org/mybatis/example/Configuration.xml";Reader reader = Resources.getResourceAsReader(resource);sqlMapper = new SqlSessionFactoryBuilder().build(reader);

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>


二、不使用 XML 文件新建 SqlSessionFactory SqlSessionFactory SqlSessionFactory qlSessionFactor

使用java生成xml配置

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);

三、使用 SqlSessionFactory SqlSessionFactory SqlSessionFactory qlSessionFactory 获取 SqlSession SqlSession SqlSession

方式一使用

SqlSession session = sqlMapper.openSession();

try {

Blog blog = (Blog) session.select("org.mybatis.example.BlogMapper.selectBlog", 101);

} finally {

session.close();

}

方式二使用接口

SqlSession session = sqlSessionFactory.openSession();

try {

BlogMapper mapper = session.getMapper(BlogMapper.class);

Blog blog = mapper.selectBlog(101);

} finally {

session.close();

}

方式三

使用 JAVA 注解方式来取代。比如,上面的 XML 语句可以替换为:

package org.mybatis.example;

public interface BlogMapper {

@Select("SELECT * FROM blog WHERE id = #{id}")

Blog selectBlog(int id);

}

优点:一、它不依赖字符串,可以减少出错。二、如果你的 IDE 有代码自动完成功能,你可以很快导航到你的 SQL 语句(因为已经转化为方法名)。三、你不再需要设定返回值类型,因为接口限定了返回值和参数。

四、探究 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" parameterType="int" resultType="Blog">

select * from Blog where id = #{id}

</select>

</mapper>

五、作用域和生命周期

1.SqlSessionFactoryBuilder

已经创建好了一个 SqlSessionFactory 后就不用再保留它。是最好不要强行保留,因为 XML 的解析资源要用来做其它更重要的事。

2.SqlSessionFactory 

,SqlSessionFactory 就会在整个应用过程中始终存在也,不建议多次创建 SqlSessionFactory,作用域是 Application,单例生命周期。

3.SqlSession

每个线程都有自己的 SqlSession 实例,SqlSession 实例是不能被共享,也是不是线程安全的,最好使用 Request 作用域或者方法体作用域

4.Mapper 实例

Mapper 是一种你创建的用于绑定映射语句的接口。Mapper 接口的实例是用 SqlSession 来获得的。

同样,从技术上来说,最广泛的 Mapper 实例作用域像 SqlSession 一样,使用请求作用域。确切地说,

在方法被调用的时候调用 Mapper 实例,然后使用后,就自动销毁掉。不需要使用明确的注销。当一个请求执行正确无误的时候,像 SqlSession 一样,

你可以轻而易举地操控这一切。保持简单性,保持 Mapper 在方法体作用域内。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: