首页 > 代码库 > .Spring+Mybatis整合
.Spring+Mybatis整合
一:声明了四个配置文件
1.spring-Mybatis的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--定义数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="close">
<property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/practice"/>
<property name="username" value="http://www.mamicode.com/root"/>
<property name="password" value="http://www.mamicode.com/076634"/>
</bean>
<!--配置sessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件路径 -->
<property name="configLocation" value="http://www.mamicode.com/classpath:config.xml"/>
</bean>
<!-- 配置扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描cn.yunhe.spring.mybatis.dao这个包以及它的子包下的所有映射接口类 -->
<property name="basePackage" value="http://www.mamicode.com/cn.yunhe.dao" />
<property name="sqlSessionFactoryBeanName" value="http://www.mamicode.com/sqlSessionFactory" />
</bean>
<!--=====================================分割线=============================================-->
<!-- 配置Spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--注解配置事物-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2.spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--自动扫描包-->
<context:component-scan base-package="cn.yunhe"/>
<!--把上面的spring-mybatis的配置注入-->
<import resource="spring-mybatis.xml"/>
</beans>
3.Mybatis的mapper.xml文件
<?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">
<!--指向dao层的接口,一个mapp对应一个xml文件-->
<mapper namespace="cn.yunhe.dao.UserMapper">
<select id="selectUser" parameterType="int" resultType="map">
select * from user where id = #{id}
</select>
</mapper>
4.Mybatis的config.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>
<!--指向文件的mapper应收地址-->
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
实体类等进行注解方法省略....
测试类:
ClassPathXmlApplicationContext cts = new ClassPathXmlApplicationContext("spring.xm;l")
IUserService ad = (IUserService) cts.getBean("userServiceImpl");
ad.selectUser(1);
.Spring+Mybatis整合