SSM配置web、pom
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.cn"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Spring-config.xml
按照需求,头部,另外配置mvc、context,上面的copy只需要改最后面的一个单词即可,下面的copy后共需要改3个
mvc:annotation-driven:注解驱动
context:component-scan:注解扫描,所有的包里基本上都有注解需要扫描,所以,base-package是所有Java包,即com.cn
然后,是试图解析,改变controller返回值,添加前缀和后缀
### 把日志信息输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
### 设置优先级别,以及输出源###
log4j.rootLogger=debug,stdout
log4j.properties
使控制台的运行记录、报错更加规范
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${pass}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="initialPoolSize" value="${initPool}"></property>
<property name="maxPoolSize" value="${maxPool}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.cn.model"></property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.mapper"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"></tx:method>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<tx:method name="*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.cn.services.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"></aop:advisor>
</aop:config>
</beans>
applicationContext-common.xml
Spring核心配置,bean的工厂
1.头部配置context、aop、tx
2.引入db.properties
user=root
pass=root
url=jdbc:mysql://localhost:3306/cn?characterEncoding=utf-8
driver=com.mysql.jdbc.Driver
initPool=3
maxPool=20
3.dataSource
使用c3p0的
ComboPooledDataSource
连接数据库
属性包括:用户名、密码、连接地址、数据库的驱动类、最小连接数、最大连接数
4.SqlSessionFactoryBean
(1)依赖注入dataSource
(2)别名配置
typeAliasesPackage
com.cn.model:实体类
5.MapperScannerConfigurer
Mapper扫描设置
basePackage:com.cn.mapper--dao包
6.DataSourceTransactionManager
数据源的事务管理
(1)依赖注入dataSource
7.tx:advice
切点配置
transactionManager为上面的数据源的事务管理
tx:attributes:指明哪些方法需要提交事务,只有insert、update、delete需要,其他的视为只读。
propagation="REQUIRED":事务传播特性
当前存在事务就继续使用,没有就创建
8.
aop:config 切面配置,aop是面向切面
aop:pointcut execution(* com.cn.services.*.*(..))
这个services(biz)包下,需要tx:advice
aop:advisor :advice-ref="txAdvice" pointcut-ref="pointCut" 为切面配置切点
applicationContext-common.xml配置成功!