IDEA中使用maven搭建 ssm+druid

1、使用IDEA建立maven項目

1.首先選擇 File => New Project ,選擇建立maven項目,勾選webapp骨架(使用骨架建立項目幫咱們事先構建好web結構)

2.接下來按照圖片選擇並建立maven項目

3.建立成功後的項目結構展現

其中的src下的 main/java main/resource test/java test/resource 須要自行建立css

2、向pom文件中導入相關jar包座標

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>vue_demo_ssm</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>vue_demo_ssm Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.0.2.RELEASE</spring.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <mysql.version>5.1.6</mysql.version>
    <mybatis.version>3.4.5</mybatis.version>
  </properties>

  <dependencies>
    <!--spring-->
    <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>org.springframework</groupId>
      <artifactId>spring-context</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-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!--mybatis依賴-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <!--mysql鏈接-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>

    <!--數據庫鏈接池-->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.24</version>
    </dependency>

    <!--log日誌-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <!--json-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.11.2</version>
    </dependency>

    <!--junit-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!--servlet 核心api-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!--hutool工具類-->
    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.4.3</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>vue_demo_ssm</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3、添加ssm和druid所需配置文件

pom座標導入後,開始建立ssm配置文件
注意:全部配置文件均存放在 src/main/resources
tips:若是你想使用spring的test,建議查看最後一節html

1.db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
db.username=root
db.password=root

db.initialSize=10
db.maxActive=50
db.minIdle=6
db.maxWait=60000
db.timeBetweenEvictionRunsMillis=60000
db.minEvictableIdleTimeMillis=300000
db.validationQuery=SELECT 'x'
db.testWhileIdle=true
db.testOnBorrow=false
db.testOnReturn=false

db.poolPreparedStatements=false
db.maxPoolPreparedStatementPerConnectionSize=20
db.filters=wall,stat
2.spring-context.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: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/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--開啓註解掃描-->
    <!--<context:annotation-config/>-->
    <context:component-scan base-package="cn.wuyiz">
        <!--配置不掃描的包-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!--配置Spring框架聲明式事務管理-->
    <!--配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 使用annotation註解方式配置事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!--配置事務通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--配置aop加強-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.wuyiz.service.impl.*ServiceImpl.*(..))"/>
    </aop:config>
</beans>
3.spring-context-mybatis.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: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/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置SqlSessionFactory工廠-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--自動掃描Mapping.xml文件-->
        <property name="mapperLocations" value="classpath:cn/wuyiz/mapper/*.xml"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 掃描model包 xml中parameterType就可使用類名,不用全路徑 -->
        <property name="typeAliasesPackage" value="cn.wuyiz.entity"/>
    </bean>
    <!--配置mapper接口所在包-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.wuyiz.mapper"/>
        <!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />-->
    </bean>

</beans>
4.spring-context-druid.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: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/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置鏈接池-->
    <!--c3p0鏈接池配置-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>
    <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
    <!--    <property name="driverClass" value="${db.driver}"/>-->
    <!--    <property name="jdbcUrl" value="${db.url}"/>-->
    <!--    <property name="user" value="${db.username}"/>-->
    <!--    <property name="password" value="${db.password}"/>-->
    <!--</bean>-->

    <!--Druid鏈接池配置,用這個鏈接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
        <!-- 初始化鏈接數量 -->
        <property name="initialSize" value="${db.initialSize}" />
        <!-- 最小空閒鏈接數 -->
        <property name="minIdle" value="${db.minIdle}" />
        <!-- 最大併發鏈接數 -->
        <property name="maxActive" value="${db.maxActive}" />
        <!-- 配置獲取鏈接等待超時的時間 -->
        <property name="maxWait" value="${db.maxWait}" />

        <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" />

        <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="${db.validationQuery}" />
        <property name="testWhileIdle" value="${db.testWhileIdle}" />
        <property name="testOnBorrow" value="${db.testOnBorrow}" />
        <property name="testOnReturn" value="${db.testOnReturn}" />

        <!--打開PSCache,而且指定每一個鏈接上PSCache的大小 -->
        <!--若是用Oracle,則把poolPreparedStatements配置爲true,mysql能夠配置爲false-->
        <property name="poolPreparedStatements" value="${db.poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
                  value="${db.maxPoolPreparedStatementPerConnectionSize}" />

        <!-- 配置監控統計攔截的filters -->
        <property name="filters" value="${db.filters}" />
    </bean>
</beans>
5.mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--
 |   plugins在配置文件中的位置必須符合要求,不然會報錯,順序以下:
 |   properties?, settings?,
 |   typeAliases?, typeHandlers?,
 |   objectFactory?,objectWrapperFactory?,
 |   plugins?,
 |   environments?, databaseIdProvider?, mappers?
 |-->
<configuration>
    <!--
     | 全局配置設置
     |
     | 可配置選項                   默認值,     描述
     |
     | aggressiveLazyLoading       true,     當設置爲‘true’的時候,懶加載的對象可能被任何懶屬性所有加載。不然,每一個屬性都按需加載。
     | multipleResultSetsEnabled   true,     容許和不容許單條語句返回多個數據集(取決於驅動需求)
     | useColumnLabel              true,     使用列標籤代替列名稱。不一樣的驅動器有不一樣的做法。參考一下驅動器文檔,或者用這兩個不一樣的選項進行測試一下。
     | useGeneratedKeys            false,    容許JDBC 生成主鍵。須要驅動器支持。若是設爲了true,這個設置將強制使用被生成的主鍵,有一些驅動器不兼容不過仍然能夠執行。
     | autoMappingBehavior         PARTIAL,  指定MyBatis 是否而且如何來自動映射數據表字段與對象的屬性。PARTIAL將只自動映射簡單的,沒有嵌套的結果。FULL 將自動映射全部複雜的結果。
     | defaultExecutorType         SIMPLE,   配置和設定執行器,SIMPLE 執行器執行其它語句。REUSE 執行器可能重複使用prepared statements 語句,BATCH執行器能夠重複執行語句和批量更新。
     | defaultStatementTimeout     null,     設置一個時限,以決定讓驅動器等待數據庫迴應的多長時間爲超時
     | -->
    <settings>
        <!-- 這個配置使全局的映射器啓用或禁用緩存 -->
        <setting name="cacheEnabled" value="true"/>
        <!-- 全局啓用或禁用延遲加載。當禁用時,全部關聯對象都會即時加載 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--當啓用時,有延遲加載的使用對象在被調用時會徹底加載任意屬性。不然,每種屬性將會按需加載-->
        <setting name="aggressiveLazyLoading" value="true"/>
        <!--是否容許單條SQL返回多個數據集(取決於驅動的兼容性)default:true-->
        <setting name="multipleResultSetsEnabled" value="true"/>
        <!--是否可使用列的別名(取決於驅動的兼容性)default:true-->
        <setting name="useColumnLabel" value="true"/>
        <setting name="defaultExecutorType" value="REUSE"/>
        <setting name="defaultStatementTimeout" value="25000"/>
        <!-- 讓控制檯打印sql語句,註釋掉則沒有 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
        <setting name="callSettersOnNulls" value="true" />
    </settings>
</configuration>
6.spring-mvc.xml
<?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">

    <!--開啓SpringMVC註解的支持-->
    <mvc:annotation-driven/>

    <!--開啓註解掃描  只掃描controller註解-->
    <context:component-scan base-package="cn.wuyiz">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--配置視圖解析器對象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--過濾靜態資源-->
    <!--<mvc:resources mapping="/css/**" location="/css/"/>-->
    <!--<mvc:resources mapping="/images/**" location="/images/"/>-->
    <!--<mvc:resources mapping="/js/**" location="/js/"/>-->

    <!--靜態資源映射-->
    <mvc:resources mapping="/static/**" location="/static/"/>
</beans>
7.web.xml

這裏的 web.xml 是在 src/main/webapp/WEB-INF/web.xml 目錄下自帶的前端

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--設置spring文件的路徑-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--此處的xml文件若是你的是整合在一塊兒的applicationContext.xml,則直接寫名字,如果像我這樣多個sping的xml文件則用*表示匹配-->
    <param-value>classpath:spring-context*.xml</param-value>
  </context-param>
  <!--配置Spring監聽器,默認只加載WEB-INF目錄下的applicationContext.xml配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--解決中文亂碼的過濾器-->
  <filter>
    <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加載springmvc.xml配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!--啓動服務器,建立該servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
8.log4j.properties(可選)

日誌文件,想添加就添加上(pom座標已導入)vue

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=e:axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

4、添加mvc的各類實現類(ssm搭建完結)

此時ssm項目配置文件搭建完成,結構以下圖所示,此時便可運行項目驗證java

5、applicationContext.xml文件(可選)

將剛纔的 spring-context.xml spring-context-mybatis.xml spring-context-druid.xml 文件內容整合到applicationContext.xml文件中,減小配置文件數量
注意:合併後須要將web.xml中的spring路徑修改爲 classpath:applicationContext.xmlmysql

<?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: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/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--開啓註解掃描-->
    <context:component-scan base-package="cn.wuyiz">
        <!--配置不掃描的包-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--Spring整合Mabatis框架-->
    <!--配置鏈接池-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>
    <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
    <!--    <property name="driverClass" value="${db.driver}"/>-->
    <!--    <property name="jdbcUrl" value="${db.url}"/>-->
    <!--    <property name="user" value="${db.username}"/>-->
    <!--    <property name="password" value="${db.password}"/>-->
    <!--</bean>-->

    <!--Druid鏈接池配置-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
        <!-- 初始化鏈接數量 -->
        <property name="initialSize" value="${db.initialSize}" />
        <!-- 最小空閒鏈接數 -->
        <property name="minIdle" value="${db.minIdle}" />
        <!-- 最大併發鏈接數 -->
        <property name="maxActive" value="${db.maxActive}" />
        <!-- 配置獲取鏈接等待超時的時間 -->
        <property name="maxWait" value="${db.maxWait}" />

        <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" />

        <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="${db.validationQuery}" />
        <property name="testWhileIdle" value="${db.testWhileIdle}" />
        <property name="testOnBorrow" value="${db.testOnBorrow}" />
        <property name="testOnReturn" value="${db.testOnReturn}" />

        <!--打開PSCache,而且指定每一個鏈接上PSCache的大小 -->
        <!--若是用Oracle,則把poolPreparedStatements配置爲true,mysql能夠配置爲false-->
        <property name="poolPreparedStatements" value="${db.poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
                  value="${db.maxPoolPreparedStatementPerConnectionSize}" />

        <!-- 配置監控統計攔截的filters -->
        <property name="filters" value="${db.filters}" />
    </bean>

    <!--配置SqlSessionFactory工廠-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--自動掃描Mapping.xml文件-->
        <property name="mapperLocations" value="classpath:cn/wuyiz/mapper/*.xml"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 掃描model包 xml中parameterType就可使用類名,不用全路徑 -->
        <property name="typeAliasesPackage" value="cn.wuyiz.entity"/>
    </bean>
    <!--配置mapper接口所在包-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.wuyiz.mapper"/>
    </bean>
    <!--配置Spring框架聲明式事務管理-->
    <!--配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事務通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--配置aop加強-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.wuyiz.service.impl.*ServiceImpl.*(..))"/>
    </aop:config>
</beans>

其餘:這裏是 spring test 的類文件展現

@RunWith(SpringJUnit4ClassRunner.class)
//這裏要加載spring配置文件,classpath後不要忘了加 ` * `
@ContextConfiguration(locations = "classpath*:applicationContext.xml")
//@ContextConfiguration(locations = "classpath*:spring-context*.xml")
public class springTest {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductService productService;

    private ProductController productController = new ProductController();

    @Test
    public void run1() {
        productService.getList();
    }
}
相關文章
相關標籤/搜索