分享知识-快乐自己:SpringMvc中的四种数据源及相关配置(整合快速集成开发)
数据库连接:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://39.105.105.186:3306/SpringMybatis?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=****
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.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-3.2.xsd">
</beans>
引入jdbc配置:
<!--引入jdbc配置-->
<context:property-placeholder location="classpath:config.properties"/>
数据源:
1):
<!--此方式为自带的数据源配置-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
2):
<!--使用c3p0数据源配置--> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <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>
3):
<!--使用dbcp数据源配置-->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
4):
<!--使用druid数据源配置-->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
核心工厂):
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--绑定数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis大配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="mapperLocations">
<list>
<value>classpath:mapper/*.xml</value>
</list>
</property>
<!--根据需要自行更改-->
<!--PageHerlper 4-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
<!-- 数据库方言:不设置(Pagehepler)分页不起作用 -->
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
<!--PageHerlper 5-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value> helperDialect=mysql </value>
</property>
</bean>
</array>
</property>
</bean>
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">
<configuration>
<!--设置别名-->
<typeAliases>
<package name ="com.mlq.ben" />
</typeAliases>
</configuration>
装配Mapper接口:
<!--装配Mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mlq.mapper"></property>
</bean>
扫描全局注解:
<context:component-scan base-package=""/>
静态资源访问及View试图:
<!--配置静态资源--> <mvc:resources mapping="/static/**" location="/static/"/> <!--注解解析器适用于试图解析--> <mvc:annotation-driven/>
<!--配置试图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/Jsp/"/> <property name="suffix" value=".jsp"/> </bean>
注解事务配置:
<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--添加扫描事务注解-->
<tx:annotation-driven transaction-manager="txManager"/>
使用:
@Transactional(propagation = Propagation.REQUIRED)
XML事务配置:
<!--创建事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!--实务增强通知-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="bys*" isolation="DEFAULT" timeout="1000"></tx:method>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"></tx:method>
</tx:attributes>
</tx:advice>
<!--定义切面-->
<aop:config>
<!--定义切入点-->
<aop:pointcut id="mypory" expression="execution(* *.service..*.*(..))"></aop:pointcut>
<!--定义顾问-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypory"/>
</aop:config>
注解配置增强核心配置:
注解类:
@Aspect
public class MyAspectj {
//前置增强
@Before("execution(* *..service..*.eat())")
public void before()
{
System.out.println("===========前置=============");
}
}
注解Spring-Aspectj.xml:配置:
<!--增强类-->
<bean id="myAspectj" class="mlq.uitl.MyAspectj"/>
<!--Aspectj 自动扫描匹配-->
<aop:aspectj-autoproxy/>
XML配置增强核心配置:
实体类:
/**
* @author asus
*/
public class MyAspectj1 {
//前置增强
public void before()
{
System.out.println("\n===========前置=============");
}
//后置增强
public void after()
{
System.out.println("\n===========后置=============");
}
//异常增强
public void abnormal(RuntimeException e)
{
System.out.println("\n===========异常=============");
}
//环绕增强
public void surround(ProceedingJoinPoint joinpoint)
{
System.out.println("\n===========环绕前=============");
try {
joinpoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("\n===========环绕后=============");
}
//最终增强
public void finalAfter()
{
System.out.println("\n===========最终=============");
}
}
Spring-Aspectj1.xml:核心配置文件
<!--增强类-->
<bean id="myAspectj1" class="mlq.uitl.MyAspectj1"/>
<!--配置切面和切入点-->
<aop:config>
<!--定义到切入点表达式-->
<aop:pointcut id="myPointcut" expression="execution(* *..service..*.eat())"/>
<!--定义切面-->
<aop:aspect ref="myAspectj1">
<!--前置-->
<!--<aop:before method="before" pointcut-ref="myPointcut"/>-->
<!--后置-->
<!--<aop:after-returning method="after" pointcut-ref="myPointcut"/>-->
<!--异常-->
<!--<aop:after-throwing method="abnormal" pointcut-ref="myPointcut" throwing="e"/>-->
<!--环绕-->
<!--<aop:around method="surround" pointcut-ref="myPointcut"/>-->
<!--最终-->
<aop:before method="finalAfter" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
Spring 拦截器配置:
实体类:
<!--配置拦截器解析器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/Interceptor"/>
<bean class="uitl.MyInterceptor1"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/user/Interceptor"/>
<bean class="uitl.MyInterceptor2"/>
</mvc:interceptor>
......
</mvc:interceptors>
使用 HibernateValidator 注解后台验证核心配置:
<mvc:annotation-driven validator="MyfactoryBean"/>
<!--注册验证器-->
<bean id="MyfactoryBean" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
</bean>
SpringMvc文件上传核心配置:
<!--配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!--单个文件大小-->
<property name="maxUploadSizePerFile" value="100000"/>
<!--文件总大小-->
<property name="maxUploadSize" value="100000"/>
</bean>
Web Spring核心监听器:
<!--配置监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/Spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Web Log4j 核心监听器:
<!-- log4j日志输出 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Web Spring核心配置:
<!--核心配置文件-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--优先初始化-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/Spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Web Spring编码设置:
<!--设置乱码-->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SSM整合的JAR:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--spring对应的版本号-->
<spring.version>4.2.1.RELEASE</spring.version>
</properties>
<dependencies>
<!--字节码转换-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!--测试JAR-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<!--引入需要的spring 核心jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!--ServletAPI-->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<!--fastjson的jar包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<!--jacksonjar包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<!--数据验证-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.1.GA</version>
</dependency>
<!--jboss logging-->
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.0.Final</version>
</dependency>
<!--validation api-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<!--slf4j api-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!--文件上传的jar包-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>org.wisdom-framework</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34_1</version>
</dependency>
<!--mybatis jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<!--Mybatis+Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--配置jdbc需要的数据源-->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<!--SpringJDBC需要的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--aspectj需要的jar-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<!--spring整合aspect需要的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!--引入spring需要的事务jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--引入springmvc需要的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--引入需要的jstl jar-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--RandomUtils随机数JAR-->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!--JSON-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<!--logback异常日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency>
<!--分页Pagehelper的jar-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>12SSM</finalName>
<!-- 加载我们的各种mapper.xml-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>