标签:
每一个Mybatis的应用程序都以一个SqlSessionFactory对象的实例为核心。SqlSessionFactory对象实例可以通过SqlSessionFactoryBuilder对象获得。SqlSessionFactoryBuilder对象可以从XML配置文件或从Configuration类的习惯准备的实例中构建SqlSessionFactory对象。
从XML文件中构建SqlSessionFactory的实例非常简单。这里建议使用类的路径的资源文件来配置,这样我们就可以使用任意的Reader实例,这个实例包括有文字形式的文件路径或URL形式的文件路径file://来创建。Mybatis包含了一些工具类,称作为资源,这些工具类包含一些方法,这些方法使得从类路径或其他位置加载资源文件更加简单。
<span style="font-size:18px;">String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);</span>
<span style="font-size:18px;"><?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></span>
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
但是事实上,SQLSessionFactoryBuilder类负责构建SqlSessionFactory,并且提供了多个build的重载方法,丹其实很多都是在调用同意签名的方法,例如public SqlSessionFactory build(InputStream inputStream,String environment,Properties properties),只是由于方法参数environment和properties都可以为null,所以为了提供调用的便利性,才提供了下面的三个方法。
<span style="font-size:18px;">public SqlSessionFactory build(InputStream inputStream) public SqlSessionFactory build(InputStream inputStream, String environment) public SqlSessionFactory build(InputStream inputStream, Properties properties)</span>
<span style="font-size:18px;">public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) public SqlSessionFactory build(Reader reader, String environment, Properties properties) public SqlSessionFactory build(Configuration config)</span>
(1)读取XML文件构造方式
<span style="font-size:18px;">String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream) ;</span>
<span style="font-size:18px;">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) ;</span>
<span style="font-size:18px;">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("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { inputStream.close(); } catch (IOException e) { // Intentionally ignore. Prefer previous error. } } }</span>
通过上面的几行代码,就能看出基于XML文件的这种构造方式,通过XML读取信息的工作之后,也是构造出Configuration对象之后,再继续进行SqlSessionFactory的构建工作的,只是多了些XML的解析工作,所以我们只需要单钩植入,直接分析变成构造方式的代码就可以了,或者是直接分析build(Parser.parse())这句代码
<span style="font-size:18px;"> public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); }</span>
以上代码使用了一个设计模式:建设者模式(Builder),SqlSessionFactoryBuilder扮演具体的建造者,Configuration类则负责建造的细节工作,SqlSession则是建造出来的产品。
看一下类图和创建者模式的基本形态图:
目录结构:
Goods:
<span style="font-size:18px;">package com.csdn.kane.domain; import java.sql.Timestamp; public class Goods { private int id; private int categoryId; private String name; private float price; private String description; private int acount; private Timestamp updateTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getCategoryId() { return categoryId; } public void setCategoryId(int categoryId) { this.categoryId = categoryId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getAcount() { return acount; } public void setAcount(int acount) { this.acount = acount; } public Timestamp getUpdateTime() { return updateTime; } public void setUpdateTime(Timestamp updateTime) { this.updateTime = updateTime; } } </span>
GoodsMapper:
<span style="font-size:18px;">package com.csdn.kane.dao; import org.apache.ibatis.annotations.Select; import com.csdn.kane.domain.Goods; public interface GoodsMapper { @Select("SELECT * FROM Goods WHERE id=#{id}") public Goods selectGoods(int id); } </span>
<span style="font-size:18px;"><?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="Goods" type="com.csdn.kane.domain.Goods"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <mapper class="com.csdn.kane.dao.GoodsMapper"/> </mappers> </configuration> </span>
<span style="font-size:18px;">package com.csdn.kane.test; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.csdn.kane.dao.GoodsMapper; import com.csdn.kane.domain.Goods; public class TestMybitas { public static void main(String[] args) throws IOException { //最基本的mybitas示例方法 TestMybitas.testMethod(); } public static void testMethod() throws IOException{ String resource = "applicationContext.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); Properties properties = new Properties(); properties.setProperty("jdbc.driver", "com.mysql.jdbc.Driver"); properties.setProperty( "jdbc.url", "jdbc:mysql://localhost:3306/qmxtest1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"); properties.setProperty("jdbc.username", "root"); properties.setProperty("jdbc.password", "root"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,properties); SqlSession session = sqlSessionFactory.openSession(); try { GoodsMapper mapper = session.getMapper(GoodsMapper.class); Goods goods = mapper.selectGoods(1); System.out.println("good description:"+goods.getDescription()); } finally { session.close(); } } } </span>
标签:
原文地址:http://blog.csdn.net/qiumuxia0921/article/details/51886346