1. 程式人生 > >使用maven實現ssm整合配置檔案

使用maven實現ssm整合配置檔案

1.引入專案依賴的jar包(pop.xml)

包含:springspringmvcmybaticsMybatis整和Spring的適配包、資料庫連線池、mysql驅動、單元測試

(注意版本問題!!!)

參考網址:https://mvnrepository.com/    (maven repository)

<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>com.atguigu</groupId>  
  <artifactId>ssm-crud</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <packaging>war</packaging>  
    
  <!-- 引入專案依賴的jar包    maven repository-->  
    
    <dependencies> 
       <!-- 支援JSR303校驗 -->
    	<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-validator</artifactId>
		    <version>5.4.2.Final</version>
		</dependency>
    	
        <!-- 支援json格式資料 -->
    	<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.8.10</version>
		</dependency>
    
    	<!--引入pageHelper分頁外掛 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.0.0</version>
		</dependency>
		
        <!-- MBG -->  
        <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->  
        <dependency>  
            <groupId>org.mybatis.generator</groupId>  
            <artifactId>mybatis-generator-core</artifactId>  
            <version>1.3.6</version>  
        </dependency>  
      
        <!-- SpringMVC、Spring 搜尋:spring webmvc-->  
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>4.3.15.RELEASE</version>  
        </dependency>  
          
        <!-- Spring JDBC  搜尋:spring jdbc-->  
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>4.3.15.RELEASE</version>  
        </dependency>  
          
        <!-- Spring Test -->  
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>4.3.15.RELEASE</version>  
            <scope>test</scope>  
        </dependency>  
          
        <!-- Spring面向切面程式設計  搜尋:spring aspect-->  
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aspects</artifactId>  
            <version>4.3.15.RELEASE</version>  
        </dependency>  
          
        <!-- mybatis 搜尋:mybatis-->  
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis</artifactId>  
            <version>3.4.6</version>  
        </dependency>  
          
        <!-- Mybatis整和Spring的適配包  -->  
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis-spring</artifactId>  
            <version>1.3.2</version>  
        </dependency>  
          
        <!-- 資料庫連線池、驅動 -->  
        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->  
        <!--c3p0  資料連線池 jar-->  
        <dependency>  
          <groupId>com.mchange</groupId>  
          <artifactId>c3p0</artifactId>  
          <version>0.9.5.1</version>  
        </dependency>  
        <!--C3P0額外依賴的一個jar包-->  
        <dependency>  
          <groupId>com.mchange</groupId>  
          <artifactId>mchange-commons-java</artifactId>  
          <version>0.2.10</version>  
        </dependency>  
              
        <!-- mysql驅動 -->  
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>5.1.10</version>  
        </dependency>   
          
        <!-- (jstl,servlet-api,junit) -->   
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->  
        <dependency>  
            <groupId>jstl</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.1.2</version>  
        </dependency>  
        
         <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
          
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
        <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>javax.servlet-api</artifactId>  
            <version>3.0.1</version>  
            <scope>provided</scope>  
        </dependency>  
  
  
        <!-- junit -->  
        <!-- https://mvnrepository.com/artifact/junit/junit -->  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.12</version>  
        </dependency>  
          
    </dependencies>  
</project>  

    此處一定要注意版本,以及依賴jar包的問題,不然很容易出問題(比如c3p0,除了匯入c3p0的jar包外,還要匯入一個依賴jar包!!!!!)

2.配置mybatis(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>
   <settings>
      <setting name="mapUnderscoreToCamelCase" value="true"/>
   </settings>
  
   <typeAliases>
      <package name="com.atguigu.crud.bean"/>
   </typeAliases>
 
</configuration>

3.配置spring

3.1配置dao(applicationContext-dao.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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 
   <!-- Spring的配置檔案,這裡主要配置和業務邏輯有關的 -->
   <!--=================== 資料來源,事務控制,xxx ================-->
   <context:property-placeholder location="classpath:dbconfig.properties" />
   <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
      <property name="driverClass" value="${jdbc.driverClass}"></property>
      <property name="user" value="${jdbc.user}"></property>
      <property name="password" value="${jdbc.password}"></property>
   </bean>
 
   <!--================== 配置和MyBatis的整合=============== -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <!-- 指定mybatis全域性配置檔案的位置 -->
      <property name="configLocation" value="classpath:mybatis-config.xml"></property>
      <property name="dataSource" ref="pooledDataSource"></property>
      <!-- 指定mybatis,mapper檔案的位置 -->
      <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
   </bean>
 
   <!-- 配置掃描器,將mybatis介面的實現加入到ioc容器中 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <!--掃描所有dao介面的實現,加入到ioc容器中 -->
      <property name="basePackage" value="com.atguigu.crud.dao"></property>
   </bean>
  
   <!-- 配置一個可以執行批量的sqlSession -->
   <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
      <constructor-arg name="executorType" value="BATCH"></constructor-arg>
   </bean>
   <!--=============================================  -->
</beans>

資料庫配置檔案(dbconfig.properties)

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=1022zhang

3.2配置service(applicationContext-service.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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 
   <!-- 除了Controler層都掃描 -->
   <context:component-scan base-package="com.atguigu">
      <context:exclude-filter type="annotation"
         expression="org.springframework.stereotype.Controller" />
   </context:component-scan>
  
</beans>

3.3配置事務(applicationContext-transaction.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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  
 
   <!-- ===============事務控制的配置 ================-->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!--控制住資料來源  -->
      <property name="dataSource" ref="pooledDataSource"></property>
   </bean>
   <!--開啟基於註解的事務,使用xml配置形式的事務(必要主要的都是使用配置式)  -->
   <aop:config>
      <!-- 切入點表示式 -->
      <aop:pointcut expression="execution(* com.atguigu.crud.service..*(..))" id="txPoint"/>
      <!-- 配置事務增強 -->
      <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
   </aop:config>
  
   <!--配置事務增強,事務如何切入  -->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
         <!-- 所有方法都是事務方法 -->
         <tx:method name="*"/>
         <!--以get開始的所有方法  -->
         <tx:method name="get*" read-only="true"/>
      </tx:attributes>
   </tx:advice>
  
   <!-- Spring配置檔案的核心點(資料來源、與mybatis的整合,事務控制) -->
  
</beans>
  

4.配置springmvc(dispatcherServlet.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"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 
   <!-- Springmvc配置檔案 -->
 
   <!--SpringMVC的配置檔案,包含網站跳轉邏輯的控制,配置  -->
   <context:component-scan base-package="com.atguigu" use-default-filters="false">
      <!--只掃描控制器。  -->
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>
  
   <!--配置檢視解析器,方便頁面返回  -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/"></property>
      <property name="suffix" value=".jsp"></property>
   </bean>
  
   <!--兩個標準配置  -->
   <!-- 將springmvc不能處理的請求交給tomcat -->
   <mvc:default-servlet-handler/>
   <!-- 能支援springmvc更高階的一些功能,JSR303校驗,快捷的ajax...對映動態請求 -->
   <!-- 處理器對映器處理器介面卡 -->
   <mvc:annotation-driven/>
 
</beans>
 

5.專案結構