數據庫鏈接:java
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頭文件:mysql
<?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配置:git
<!--引入jdbc配置-->
<context:property-placeholder location="classpath:config.properties"/>
數據源:github
1):web
<!--此方式爲自帶的數據源配置-->
<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):spring
<!--使用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):sql
<!--使用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>
核心工廠):express
<!--配置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:apache
<?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>