spring整合mybatis文件
2016-11-02 16:21
183 查看
文件框架:
spring的配置文件(mybatis配置文件里面为空,全部在spring中配置,但要保留mybatis配置文件,例如添加 mybatis 拦截器时)
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- spring读取配置文件 ,以下读取多个文件 -->
<bean id="common.propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<!-- <value>classpath:log4j.properties</value> -->
</list>
</property>
</bean>
<!-- 配置数据源-->
<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<!-- 添加mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com.test.dao.*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 单个DAO接口手动租入
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.test.dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
-->
</beans>
测试文件:
package com.test.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.dao.UserMapper;
import com.test.model.User;
import com.test.service.UserService;
public class TestUser {
private UserMapper userDao;
@Before
public void setUp() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-commons.xml");
userDao= ctx.getBean(UserMapper.class);
}
@Test
public void run(){
System.out.println("-------------------------------------");
User user = this.userDao.queryUserById(3);
System.out.println(user);
System.out.println("-------------------------------------");
}
}
其他和mybatis整合mysql相同
spring的配置文件(mybatis配置文件里面为空,全部在spring中配置,但要保留mybatis配置文件,例如添加 mybatis 拦截器时)
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- spring读取配置文件 ,以下读取多个文件 -->
<bean id="common.propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<!-- <value>classpath:log4j.properties</value> -->
</list>
</property>
</bean>
<!-- 配置数据源-->
<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<!-- 添加mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com.test.dao.*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 单个DAO接口手动租入
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.test.dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
-->
</beans>
测试文件:
package com.test.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.dao.UserMapper;
import com.test.model.User;
import com.test.service.UserService;
public class TestUser {
private UserMapper userDao;
@Before
public void setUp() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-commons.xml");
userDao= ctx.getBean(UserMapper.class);
}
@Test
public void run(){
System.out.println("-------------------------------------");
User user = this.userDao.queryUserById(3);
System.out.println(user);
System.out.println("-------------------------------------");
}
}
其他和mybatis整合mysql相同
相关文章推荐
- Spring与Mybatis整合配置文件
- 使用spring整合mybatis时映射文件的导入方法
- Spring+mybatis+maven整合过程加载配置文件jdbc.properties
- spring-mybatis.xml配置文件,spring与mybatis整合在一起
- mybatis与spring整合配置文件
- mybatis与spring整合(基于配置文件)
- struts2 + spring + mybatis 整合配置文件
- mybatis与spring整合(基于配置文件)
- spring与mybatis整合配置文件
- Spring SpringMvc 3.0 + MyBatis 整合--补充关于.properties文件的读取
- mybatis与spring整合(基于配置文件)
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- mybatis与spring整合(基于配置文件)
- javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits-spring配置文件的几种方式)之一SqlSessionDaoSupport方式
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- spring与mybatis整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- Spring整合Mybatis在引入属性文件的时候遇到的问题
- mybatis与spring整合(基于配置文件)