码迷,mamicode.com
首页 > 编程语言 > 详细

.Spring+Mybatis整合

时间:2017-07-20 20:56:51      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:username   事物   root   数据库连接池   lib   tor   路径   nbsp   ace   

一:声明了四个配置文件

 

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整合

标签:username   事物   root   数据库连接池   lib   tor   路径   nbsp   ace   

原文地址:http://www.cnblogs.com/lxiaoyang/p/7214286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!