一篇文章學會Spring+SpringMVC+Mybatis+Maven搭建和部署,記一次Spring+SpringMVC+Mybatis+Maven的整合
之前一直沒有用過maven和Mybatis最近自己搭建一個基於Maven的Spring+SpringMVC+Mybatis開發環境。
注:有的時候沒有自己動手真正搭過環境(脫離教學的方式),可能有些地方的問題注意不到的。
會在介紹搭建的同時記錄一些遇見的坑和一些知識點。
首先放上Maven配置檔案。
1、POM.xml(至於maven裡面支援的一些服務外掛什麼的可以自己另行百度)
注:
①、pom檔案要放到專案根目錄下
②、maven的檔案結構
src/main/java 該source floder用來存放java程式碼
src/test/java 該source floder用來存放測試程式碼
src/main/webapp 該source floder用來存放jsp和web配置檔案,相當於web專案的webroot
target 用來存放maven打包編譯的檔案
注:普通專案如何改為maven專案
①、將專案更改為maven結構
②、然後在該專案下執行mvn命令:mvn eclipse:eclipse 專案根目錄必須有pom.xml否則maven無法生成相應目錄結構
③、執行成功後在.project中相應位置新增如下配置
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
</buildCommand>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
④、在專案的properties選項中,找到Deployment Assembly選項更改專案釋出時的路徑(看是否如下圖,如果上面一切順利,一般不用手動修改)
至此,你的專案就轉化為了maven專案了。
maven所需要的jar包可以在 這裡 進行搜尋,還能看到其依賴包
<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>E-LearningSchool</groupId>
<artifactId>E-LearningSchool</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>E-LearningSchool</name>
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.3.12.RELEASE</spring.version>
<mybatis.version>3.4.1</mybatis.version>
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<mysql.version>5.1.38</mysql.version>
<commons.logging.version>1.2.17</commons.logging.version>
<fastjson.version>1.2.44</fastjson.version>
<aspectjweaver.version>1.8.13</aspectjweaver.version>
</properties>
<dependencies>
<!-- junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.2</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</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-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-jdbc</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-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver springaop 依賴 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<!-- commons -->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- data source -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!-- mybatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- mysql -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- fastjson -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- javaxmail -->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<!-- <dependency> -->
<!-- <groupId>javax.mail</groupId> -->
<!-- <artifactId>mail</artifactId> -->
<!-- <version>1.4.7</version> -->
<!-- </dependency> -->
<!-- log -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${commons.logging.version}</version>
</dependency>
</dependencies>
</project>
Spring+SpringMVC的配置:
注:寫在前面的話
一、在配置web.xml的時候尋找spring相關的配置檔案:
1、沒有手動指定(contextConfigLocation)路徑的話,則預設去web-info下去找(spring預設找applicationContext.xml,springmvc預設找XXX-servlet.xml)。
2、如果我們把配置檔案放到src下面,或者自定義包路徑下的時候,可以使用classpath方式尋找。(classpath:後跟配置檔案所在包的路徑,可以使用萬用字元)
3、classpath會在當前專案去找,而classpath* 則會連所引用的包中也搜尋
4、具體配置介紹請移步這裡:點選開啟連結
①、Web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-context.xml</param-value>
</context-param>
<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*:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
②Spring配置
<?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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.scarecrow.elearning.uac.service">
<!-- 如果service和controller在同一個父包中,在掃描父包的時候需要使用該句進行排除,springmvc中同樣需要排除 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本屬性 url,user,pass -->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小,最小增長,最大活動連線 -->
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<!-- 獲取連線超時時間,單位毫秒 -->
<property name="maxWait" value="60000"/>
<!-- 設定間隔多久進行一次檢查關閉空閒連結,單位毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="30000"/>
<!-- 設定每個連線在池中的最小生存時間,單位毫秒 -->
<property name="minEvictableIdleTimeMillis" value="30000"/>
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 開啟PSCache,並且指定每個連線上PSCache的大小 如果用Oracle,則把poolPreparedStatements配置為true,mysql可以配置為false。-->
<!-- <property name="poolPreparedStatements" value="false" /> -->
<!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
<!-- 配置監控統計攔截的filters -->
<!-- <property name="filters" value="wall,stat" /> -->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/scarecrow/elearning/uac/resources/*/*Mapper.xml"></property>
<property name="typeAliasesPackage" value="com.scarecrow.elearning.uac.entity"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.scarecrow.elearning.uac.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="upd*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.scarecrow.elearning.uac.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
</beans>
③、SpringMVC配置
<?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/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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 預設的註解對映的支援,自動註冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 自動掃描的包名 -->
<context:component-scan base-package="com.scarecrow.elearning.uac.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!-- 檢視解釋類 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Mybatis的注意:
因為Mybatis的配置(包括mapper代理生成,mapper掃描等操作)交給了Spring進行管理()
SqlSessionFactoryBean--------->用來掃描mapper.xml檔案的,以及對entity生成別名,以供mapper sql檔案使用。
MapperScannerConfigurer-------->用來掃描mapper介面,並給其注入sqlsession
mybatisMapper配置:
注:mybatis的mapper.xml和對應的介面可以不放在同一個包目錄下,在spring對其進行掃描的時候會根據 namespace 指定的介面去生成例項代理物件。所以,namespace,在這裡不止是為了sql的隔離作用,更重要的是和對應介面關聯起來。
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.scarecrow.elearning.uac.dao.UserMapper">
<select id="getUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
其他:因為在用mysql6.0+的開發包的時候出現了server time zone 錯誤,設定serverTimeZone一直亂碼無法解決所以換成了5的開發包。mysql用的是5.7的,需要指定ssl為false,不過不指定也不影響開發。
jdbc.url=jdbc:mysql://localhost:3306/elearning?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=GMT&useSSL=false
最後:附上一個簡陋的例子點選開啟連結