SSM(Spring+Spring MVC + MyBatis)
2019-07-01 本文已影响0人
17f6ebdcf197
database.properties
druid.name=CloudDemo
druid.url=jdbc:mysql://localhost:3306/clouddemo?useUnicode=true&characterEncoding=UTF-8&&zeroDateTimeBehavior=convertToNull&useSSL=false
druid.driverClassName=com.mysql.jdbc.Driver
druid.username=root
druid.password=mysqladmin
druid.filters=stat
druid.maxActive=20
druid.initialSize=1
druid.maxWait=60000
druid.minIdle=10
druid.maxIdle=15
druid.timeBetweenEvictionRunsMillis=60000
druid.minEvictableIdleTimeMillis=300000
druid.validationQuery=SELECT 'x'
druid.testWhileIdle=true
druid.testOnBorrow=false
druid.testOnReturn=false
druid.maxOpenPreparedStatements=20
druid.removeAbandoned=true
druid.removeAbandonedTimeout=1800
druid.logAbandoned=true
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--加载数据库参数-->
<context:property-placeholder location="classpath:conf/database.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<!--数据库基本信息配置-->
<property name="driverClassName" value="${druid.driverClassName}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="filters" value="${druid.filters}"/>
<!-- 最大并发连接数 -->
<property name="maxActive" value="${druid.maxActive}"/>
<!-- 初始化连接数量 -->
<property name="initialSize" value="${druid.initialSize}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}"/>
<!-- 最小空闲连接数 -->
<property name="minIdle" value="${druid.minIdle}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name = "minEvictableIdleTimeMillis" value ="${druid.minEvictableIdleTimeMillis}" />
<property name = "validationQuery" value = "${druid.validationQuery}" />
<property name = "testWhileIdle" value = "${druid.testWhileIdle}" />
<property name = "testOnBorrow" value = "${druid.testOnBorrow}" />
<property name = "testOnReturn" value = "${druid.testOnReturn}" />
<property name = "maxOpenPreparedStatements" value ="${druid.maxOpenPreparedStatements}" />
<!-- 打开 removeAbandoned 功能 -->
<property name = "removeAbandoned" value = "${druid.removeAbandoned}" />
<!-- 1800 秒,也就是 30 分钟 -->
<property name = "removeAbandonedTimeout" value ="${druid.removeAbandonedTimeout}" />
<!-- 关闭 abanded 连接时输出错误日志 -->
<property name = "logAbandoned" value = "${druid.logAbandoned}" />
</bean>
<!-- 配置transactionManager事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--配置MyBatis的sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBatis的核心配置文件所在位置-->
<property name="configLocation" value="classpath:conf/mybatis-config.xml"/>
<!-- mapper配置文件的位置 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.niit.clouddemo.dao"/>
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
</bean>
<!-- 配置事物通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事物传播特性 -->
<tx:attributes>
<tx:method name="insert" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="check*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面-->
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* com.niit.clouddemo.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>
<!--自动扫描包-->
<context:component-scan base-package="com.niit.clouddemo"/>
</beans>
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">
<!-- MyBatis配置属性 ,注意元素配置顺序-->
<configuration>
<!-- 为POJO定义别名 -->
<typeAliases>
<package name="com.niit.clouddemo.pojo"/>
</typeAliases>
</configuration>
springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 1. 配置包扫描器,扫描@Controller注解的类 -->
<context:component-scan base-package="com.niit.test.controller"/>
<!-- 2. 加载注解驱动 -->
<mvc:annotation-driven/>
<!-- 3. 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>CloudDemo</display-name>
<!-- 欢迎首页 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置加载Spring文件监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring MVC前端核心控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/springmvc-config.xml</param-value>
</init-param>
<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- / : 拦截所有请求(处理jsp)-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--静态资源处理,默认不拦截-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>/assets/*</url-pattern>
</servlet-mapping>
</web-app>