Spring与mybatis整合
2.实际项目中,通常会用 spring 这个沾合剂来管理 datasource 等。充分利用spring 基于接口的编程,以及aop ,ioc 带来的方便。用spring 来管理 mybatis 与管理hibernate
有很多类似的地方。今天的重点就是数据源管理以及 bean的配置。
3.上午学习mybatis的时候,需要建立一个java文件,里面的内容都是写在static静态框中,重要用来建立SqlSession
后面的测试程序中,想要操作数据库的第一句必然是SqlSession session = Util.getSqlSessionFactory().openSession();
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Util {
public static SqlSessionFactory sqlmapper = null;
static {
//一般命名是config.xml文件
String rs = "com/jf/mybatis/user/Config.xml";// 为第二步中的配置文件名,主要是datasource的配置,链接数据库的配置文件,一般命名为Configuration.xml
Reader reader = null;
try {
reader = Resources.getResourceAsReader(rs);
} catch (IOException e) {
e.printStackTrace();
}
sqlmapper = new SqlSessionFactoryBuilder().build(reader);
sqlmapper.getConfiguration().addMapper(userOperation.class);
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlmapper;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
4.个人感觉使用spring整合的一个非常大的好处就是把上述的操作写入到xml文件中,但是要具体说出好处有哪些。。。。。。
xml文件在最近学习的过程中突出了它非常重要的位置,几乎接触到所有东西都要用到xml文件。
5.整合spring和mybatis需要导入相关的包---------------------------->C:\Users\liuquanyi\Desktop\spirng与mybatis\lib
5_1.最重要的一个文件applicationContent.xml文件
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
<!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="studymybatis"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource"/>
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="com/jf/mybatis/user/Config/Config.xml"/>
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" value="com.jf.mybatis.user.userOperation" />
</bean>
</beans>
---------------------------------------------------------<!程序解释和编写过程中遇到的错误总结>------------------------------------------------------------------------
上述内容就是applicationContent.xml中最重要的部分了,
1.其中<property name="configLocation" value="Config/Config.xml">中,value的值是mybatis核心的配置文件,路径不能用com.jf.mybatis.,
必须写成com/jf/mybatis/user/Config/Config.xml。
2.<property name="mapperInterface" value="com.jf.mybatis.user.userOperation" /> 就是早上建立的接口,早上刚刚学习过已接口的方式来操作(对数据库的操作)。
6.在程序中调用相关的操作方法:
//应该是读取xml文件的
private static ClassPathXmlApplicationContext ctx;
static {
//读取spring和mybatis整合中的关键文件,把sqlSessionFactory,userMapper写入xml文件,而不是写到程序中。
ctx = new ClassPathXmlApplicationContext("com/jf/mybatis/user/Config/applicationContext.xml");
}
public static void main(String[] args) {
//通过接口来操作
userOperation uOperation = (userOperation)ctx.getBean("userMapper");
//userOperation接口中已经定义了相关额方法,调用即可。
User user = uOperation.selectById(1);
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getSex());
System.out.println(user.getAddress());
}
7. 通过这两天的学习发现从刚开始接触mybatis开始,感觉各种烦,到现在的调用只是简单的两句话,虽然配置文件多了很多,但是整个程序写出来非常的简洁,
通过各种整合最终肯定能代替jdbc链接数据库的方法。而且简单很多。值得学习
************DBPC数据库链接池,一听就很高大上。。。。。************************
2.实际项目中,通常会用 spring 这个沾合剂来管理 datasource 等。充分利用spring 基于接口的编程,以及aop ,ioc 带来的方便。用spring 来管理 mybatis 与管理hibernate
有很多类似的地方。今天的重点就是数据源管理以及 bean的配置。
3.上午学习mybatis的时候,需要建立一个java文件,里面的内容都是写在static静态框中,重要用来建立SqlSession
后面的测试程序中,想要操作数据库的第一句必然是SqlSession session = Util.getSqlSessionFactory().openSession();
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Util {
public static SqlSessionFactory sqlmapper = null;
static {
//一般命名是config.xml文件
String rs = "com/jf/mybatis/user/Config.xml";// 为第二步中的配置文件名,主要是datasource的配置,链接数据库的配置文件,一般命名为Configuration.xml
Reader reader = null;
try {
reader = Resources.getResourceAsReader(rs);
} catch (IOException e) {
e.printStackTrace();
}
sqlmapper = new SqlSessionFactoryBuilder().build(reader);
sqlmapper.getConfiguration().addMapper(userOperation.class);
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlmapper;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
4.个人感觉使用spring整合的一个非常大的好处就是把上述的操作写入到xml文件中,但是要具体说出好处有哪些。。。。。。
xml文件在最近学习的过程中突出了它非常重要的位置,几乎接触到所有东西都要用到xml文件。
5.整合spring和mybatis需要导入相关的包---------------------------->C:\Users\liuquanyi\Desktop\spirng与mybatis\lib
5_1.最重要的一个文件applicationContent.xml文件
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
<!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="studymybatis"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource"/>
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="com/jf/mybatis/user/Config/Config.xml"/>
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" value="com.jf.mybatis.user.userOperation" />
</bean>
</beans>
---------------------------------------------------------<!程序解释和编写过程中遇到的错误总结>------------------------------------------------------------------------
上述内容就是applicationContent.xml中最重要的部分了,
1.其中<property name="configLocation" value="Config/Config.xml">中,value的值是mybatis核心的配置文件,路径不能用com.jf.mybatis.,
必须写成com/jf/mybatis/user/Config/Config.xml。
2.<property name="mapperInterface" value="com.jf.mybatis.user.userOperation" /> 就是早上建立的接口,早上刚刚学习过已接口的方式来操作(对数据库的操作)。
6.在程序中调用相关的操作方法:
//应该是读取xml文件的
private static ClassPathXmlApplicationContext ctx;
static {
//读取spring和mybatis整合中的关键文件,把sqlSessionFactory,userMapper写入xml文件,而不是写到程序中。
ctx = new ClassPathXmlApplicationContext("com/jf/mybatis/user/Config/applicationContext.xml");
}
public static void main(String[] args) {
//通过接口来操作
userOperation uOperation = (userOperation)ctx.getBean("userMapper");
//userOperation接口中已经定义了相关额方法,调用即可。
User user = uOperation.selectById(1);
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getSex());
System.out.println(user.getAddress());
}
7. 通过这两天的学习发现从刚开始接触mybatis开始,感觉各种烦,到现在的调用只是简单的两句话,虽然配置文件多了很多,但是整个程序写出来非常的简洁,
通过各种整合最终肯定能代替jdbc链接数据库的方法。而且简单很多。值得学习
************DBPC数据库链接池,一听就很高大上。。。。。************************