1
.
添加相关jar包
2.搭建环境
3.编写applicationContext.xml配置文件(
全部采用注解方式开发
)
<?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:aop="http://www.springframework.org/schema/aop";
xmlns:tx="http://www.springframework.org/schema/tx";
xmlns:mvc="http://www.springframework.org/schema/mvc";
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd";>
<!-- 采用注解开发,开启注解功能 开启mvc注解-->
<context:component-scan base-package="com.ruicai.ssm.*"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 1.数据连接池,首先得加载连接mysql的属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 2.配置mybatis的sqlSessionFactory
得到sqlSessionFactory需要属性:typeAliasesPackage,DataSource,mapper.xml映射文件,此映射文件相当于接口的实现类
所以需要sqlSessionFactory来生产SQLSession才能自动生成代理实现类
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 给指定的包取别名:包名用点,文件用/,且包不用加classpath,文件要加classpath -->
<property name="typeAliasesPackage" value="com.ruicai.ssm.entity"/>
<!-- 载入mapper.xml映射文件 -->
<property name="mapperLocations" value="classpath:com/ruicai/ssm/mapper/*.xml"/>
</bean>
<!-- 3.扫描mapper包,由spring自动生成mapper代理,并注册到spring里 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ruicai.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 4.配置视图解析器,mvc转发路径 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 5.配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>