SpringMVC,Spring,Mybatis整合配置(SSM整合配置)
SpringMVC Spring Mybatis整合步骤
导入相关jar包
1. dao层
dao层配置文件
1.1 db.properties mysql连接配置文件
配置内容如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm1?characterEncoding=utf8
user=root
password=root
1.2 mybatis核心配置文件 SqlMapConfig.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>
</configuration>
1.3 mybatis核心配置文件 applicationContext-dao.xml
schema约束和配置内容如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd”>
<!– 加载驱动配置文件 –>
<context:property-placeholder location=”classpath:db.properties”/>
<!– 配置c3p0连接池 本人使用的是C3p0连接池 此处可以更换 –>
<bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource”>
<property name=”driverClass” value=”${driver}”/>
<property name=”jdbcUrl” value=”${url}”/>
<property name=”user” value=”${user}”/>
<property name=”password” value=”${password}”/>
</bean>
<!– 配置sqlsessionFactory工厂 –>
<bean id=”sqlsessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>
<property name=”dataSource” ref=”dataSource”></property>
<property name=”configLocation” value=”classpath:SqlMapConfig.xml”></property>
</bean>
<!– 配置mapper扫描器 –>
<bean class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>
<property name=”basePackage” value=”com.ssm1.zz.mapper”></property>
</bean>
</beans>
2.service层
2.1 service层扫描注解配置 applicationContext-service.xml
schema约束和配置内容如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd”>
<!– 配置扫描service层的注解驱动 –>
<context:component-scan base-package=”com.ssm1.zz.service” />
</beans>
2.2 service层的Spring管理相关事务配置 applicationContext-transaction.xml
schema约束和配置内容如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd”>
<!– 配置事务平台管理器 –>
<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>
<!– 传播行为 service层只要以sava、insert、delete、update开头的方法均会开启事务管理–>
<tx:method name=”save*” propagation=”REQUIRED”/>
<tx:method name=”insert*” propagation=”REQUIRED”/>
<tx:method name=”delete*” propagation=”REQUIRED”/>
<tx:method name=”update*” propagation=”REQUIRED”/>
<tx:method name=”find*” propagation=”SUPPORTS” read-only=”true”/>
<tx:method name=”get*” propagation=”SUPPORTS” read-only=”true”/>
</tx:attributes>
</tx:advice>
<!–aop相应配置–>
<aop:config>
<aop:advisor advice-ref=”txAdvice” pointcut=”execution(* com.ssm1.zz.service.*.*(..))”/>
</aop:config>
</beans>
3.web层
3.1 SpringMVC相关配置 SpringMVC.xml
schema约束和配置内容如下:
<?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:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:dubbo=”http://code.alibabatech.com/schema/dubbo”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd”>
<!– 配置扫描controller层的注解驱动
作用:扫描web层的controller注解–>
<context:component-scan base-package=”com.ssm1.zz.controller”></context:component-scan>
<!– 配置处理器映射驱动
作用:自动配置相应的映射处理器和映射处理适配器–>
<mvc:annotation-driven></mvc:annotation-driven>
<!– 配置视图解析器 –>
<bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”prefix” value=”/WEB-INF/”></property>
<property name=”suffix” value=”.jsp”></property>
</bean>
</beans>
4.web.xml配置
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns=”http://java.sun.com/xml/ns/javaee”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
id=”WebApp_ID” version=”2.5″>
<display-name>ssm1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!– 配置全局参数 加载每一个applicationContext-.xml 开头的Spring容器.xml配置文件 –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!– 加载监听器 –>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!– 配置SpringMVC前端控制器 –>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!– 配置SpringMVC前端控制器初始化参数(SprinfMVC配置文件的路径) –>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!– 配置SpringMVC前端控制器的优先级 让它在项目启动的时候加载 –>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
5.日志文件配置
log4j.properties日志文件配置内容如下:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n
### set log levels – for more verbose logging change ‘info’ to ‘debug’ ###
log4j.rootLogger=info, stdout
注意:最好在项目路径下建立一个config文件夹,将出web.xml以外的其他配置文件放入此文件夹(目的:方便管理)
以上内容纯属学习框架知识后的个人体会。如侵权,请联系删除:QQ:763893274