1. 程式人生 > >.Spring+Mybatis整合

.Spring+Mybatis整合

ont 4.3 cati 實例 users frame 數據庫 text manager

一:聲明了四個配置文件

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="jdbc:mysql://localhost:3306/practice"/>
<property name="username" value="root"/>
<property name="password" value="076634"/>

</bean>

<!--配置sessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件路徑 -->
<property name="configLocation" value="classpath:config.xml"/>
</bean>


<!-- 配置掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描cn.yunhe.spring.mybatis.dao這個包以及它的子包下的所有映射接口類 -->
<property name="basePackage" value="cn.yunhe.dao" />
<property name="sqlSessionFactoryBeanName" value="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整合