原文链接:https://blog.csdn.net/jimolangge123/article/details/49228255
MyBatis框架主要是围绕着SqlSessionFactory这个类进行的,这个的创建过程如下:
- 定义一个Configuration对象,其中包含数据源、事务、mapper文件资源以及影响数据库行为属性设置settings
- 通过配置对象,则可以创建一个SqlSessionFactoryBuilder对象
- 通过 SqlSessionFactoryBuilder 获得SqlSessionFactory 的实例。
- SqlSessionFactory 的实例可以获得操作数据的SqlSession实例,通过这个实例对数据库进行操作
具体看看SqlSessionFactory的创建
一、通过Configuration.xml配置文件进行创建
1.配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <properties resource= "ssm/jdbc.properties" ></properties> <environments default = "development" > <environment id= "development" > <transactionManager type= "JDBC" /> <dataSource type= "POOLED" > <property name= "driver" value= "${jdbc.driverClassName}" /> <property name= "url" value= "${jdbc.url}" /> <property name= "username" value= "${jdbc.username}" /> <property name= "password" value= "${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource= "ssm/BlogMapper.xml" /> </mappers> </configuration> |
2.读取配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package ssm; import java.io.IOException; import java.io.Reader; 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.sm.model.User; public class GetSqlSessionFactoryFromXML { public static void main(String[] args) throws IOException { //配置文件的名称 String resource = "ssm/configuration.xml" ; //通过Mybatis包中的Resources对象很轻松的获取到配置文件 Reader reader = Resources.getResourceAsReader(resource); //通过SqlSessionFactoryBuilder创建 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //获得session实例 SqlSession session =sqlSessionFactory.openSession(); User user = new User(); user.setId( 8 ); //完成数据库的插入 session.insert( "add" , user); session.commit(); session.close(); System.out.println(sqlSessionFactory); } } |
二、在程序中构建这些对象来创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package ssm; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.ibatis.datasource.pooled.PooledDataSource; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import com.sm.model.User; public class GetSqlSessionFactoryFromProgram { public static void main(String[] args) throws SQLException { String driver = "oracle.jdbc.OracleDriver" ; String url = "jdbc:oracle:thin:@localhost:1521:orcl" ; String username= "scott" ; String password= "tiger" ; //创建使用缓存池的数据源 /* * <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> */ DataSource dataSource = new PooledDataSource(driver,url,username,password); //创建事务 /* * <transactionManager type="JDBC" /> */ TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment( "development" , transactionFactory, dataSource); Configuration configuration = new Configuration(environment); //加入资源 /* * <mapper resource="ssm/BlogMapper.xml"/> */ configuration.addMapper(UserMapper. class ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); System.out.println(sqlSessionFactory); SqlSession session = sqlSessionFactory.openSession(); User user = new User(); user.setId( 6 ); session.insert( "add" , user); session.commit(); session.close(); } } |
三、通过与Spring集成,由Spring容器管理创建
1. 在spring的配置文件applicationContext.xml中配置
1
2
3
4
5
|
<bean id= "sessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "dataSource" /> <property name= "configLocation" value= "classpath*:conf/configuration.xml" /> </bean> |
如果使用则直接注入,获取SqlSessionFactory实例即可。