SSM:SpringMVC、SpringBean
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配置成功!