我們繼續(xù)講講基礎(chǔ)內(nèi)容滥崩,適合項(xiàng)目3還沒提交的學(xué)員,如果你項(xiàng)目3已經(jīng)提交讹语,本文可以忽略夭委。
承接上一篇文章《Spring+SpringMVC+Mybatis整合開發(fā)思路及配置詳解(一)》,上一篇文章我們回顧了SSM的相關(guān)知識募强,講述了一個SSM項(xiàng)目的目錄結(jié)構(gòu)株灸,并用圖片的形式展示了SSM項(xiàng)目的架構(gòu)。前期準(zhǔn)備工作已經(jīng)完成擎值,接下來就要進(jìn)入真正的項(xiàng)目搭建了慌烧。
我現(xiàn)將上一講中的目錄結(jié)構(gòu)給出,以防有的同學(xué)只看了這一章的內(nèi)容鸠儿。
我們使用IDEA作為開發(fā)工具屹蚊,IDEA真的是一個非常強(qiáng)大的開發(fā)工具了,學(xué)習(xí)成本很低进每,沒有轉(zhuǎn)過來的小伙伴趕緊轉(zhuǎn)過來吧汹粤。
大家可以按照下面的步驟搭建環(huán)境:
后面的就一路Next下去就可以了。接著就需要等待IDEA幫助我們生成一個基本的WEB項(xiàng)目田晚。
IDEA生成項(xiàng)目完成以后嘱兼,初始的目錄是這個樣子的:
接著,我們就在main中新建一個java贤徒,并標(biāo)記為sources root芹壕。然后在java中新建包汇四,這個前面最好和一開始的組名相同,后面就是項(xiàng)目名踢涌,所以我的就是com.roobtyan.ssm
然后就可以在包中新建目錄了通孽,使用快捷鍵組合Ctrl+Shift+Alt+S,打開項(xiàng)目配置睁壁,找到Modules背苦,如下圖。
然后潘明,右鍵新建目錄行剂,所有目錄創(chuàng)建完成后是這個樣子的:
其實(shí)我們的配置文件,除了SpringMVC的钉疫,都是在resouces這個目錄中的,為了能夠讓Spring等初始化的時候找到我們的配置文件巢价,需要標(biāo)記這個目錄為資源目錄牲阁。右鍵,找到Mark as Resources root這一項(xiàng)壤躲。
最后是這個樣子的:
編寫Maven依賴
ok城菊,基本的目錄我們已經(jīng)配置好了,接下來要做的事情就是引入項(xiàng)目依賴的jar文件了碉克。
jar文件名 作用
spring-webmvc 這是SpringMVC的jar包
spring-jdbc SpringJDBC凌唬,我們需要用到,屬于依賴的依賴
spring-test Spring的單元測試整合jar漏麦,在單元測試的時候會用到
spring-aspects Spring的APO模塊jar文件
mybatis mybatis的核心
mybatis-spring mybatis-Spring的整合適配器
c3p0 為了保證項(xiàng)目的效率客税,引入數(shù)據(jù)庫連接池,同樣可以替換為DBPC或者阿里的連接池
mysql-connector-java mysql的連接器撕贞,這個版本號要看你裝的mysql的版本更耻,因?yàn)槲业氖?,所以版本可能和你們的有些區(qū)別
jstl 引入jsp的JSTL支持
javax.servlet-api Servlet的支持jar文件
junit 單元測試需要的jar文件捏膨,與spring-test整合在一起
log4j 日志jar文件秧均,這個jar在配置后,可以在控制臺打印運(yùn)行時日志号涯,有必要還可以存入文件
下面給出配置文件:pom.xml
<?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>com.roobtyan</groupId>
? <artifactId>ssm</artifactId>
? <version>1.0-SNAPSHOT</version>
? <packaging>war</packaging>
? <name>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.7</maven.compiler.source>
? ? <maven.compiler.target>1.7</maven.compiler.target>
? </properties>
? <dependencies>
? ? <!-- SpringMVC -->
? ? <dependency>
? ? ? <groupId>org.springframework</groupId>
? ? ? <artifactId>spring-webmvc</artifactId>
? ? ? <version>4.3.7.RELEASE</version>
? ? </dependency>
? ? <!-- Spring-Jdbc -->
? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
? ? <dependency>
? ? ? <groupId>org.springframework</groupId>
? ? ? <artifactId>spring-jdbc</artifactId>
? ? ? <version>4.3.7.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.7.RELEASE</version>
? ? </dependency>
? ? <!-- Spring面向切面編程 -->
? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
? ? <dependency>
? ? ? <groupId>org.springframework</groupId>
? ? ? <artifactId>spring-aspects</artifactId>
? ? ? <version>4.3.7.RELEASE</version>
? ? </dependency>
? ? <!--MyBatis -->
? ? <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
? ? <dependency>
? ? ? <groupId>org.mybatis</groupId>
? ? ? <artifactId>mybatis</artifactId>
? ? ? <version>3.4.2</version>
? ? </dependency>
? ? <!-- MyBatis整合Spring的適配包 -->
? ? <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
? ? <dependency>
? ? ? <groupId>org.mybatis</groupId>
? ? ? <artifactId>mybatis-spring</artifactId>
? ? ? <version>1.3.1</version>
? ? </dependency>
? ? <!-- 數(shù)據(jù)庫連接池目胡、驅(qū)動 -->
? ? <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
? ? <dependency>
? ? ? <groupId>c3p0</groupId>
? ? ? <artifactId>c3p0</artifactId>
? ? ? <version>0.9.1</version>
? ? </dependency>
? ? <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
? ? <dependency>
? ? ? <groupId>mysql</groupId>
? ? ? <artifactId>mysql-connector-java</artifactId>
? ? ? <version>5.1.41</version>
? ? </dependency>
? ? <!-- (jstl,servlet-api,junit) -->
? ? <!-- https://mvnrepository.com/artifact/jstl/jstl -->
? ? <dependency>
? ? ? <groupId>jstl</groupId>
? ? ? <artifactId>jstl</artifactId>
? ? ? <version>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>
? ? <dependency>
? ? ? <groupId>junit</groupId>
? ? ? <artifactId>junit</artifactId>
? ? ? <version>4.11</version>
? ? ? <scope>test</scope>
? ? </dependency>
? </dependencies>
? <build>
? ? <finalName>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.0.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.7.0</version>
? ? ? ? </plugin>
? ? ? ? <plugin>
? ? ? ? ? <artifactId>maven-surefire-plugin</artifactId>
? ? ? ? ? <version>2.20.1</version>
? ? ? ? </plugin>
? ? ? ? <plugin>
? ? ? ? ? <artifactId>maven-war-plugin</artifactId>
? ? ? ? ? <version>3.2.0</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>
依賴已經(jīng)配置完成舷手,接下來就來配置Spring和Mybatis戒职,但是在此之前,還需要做一些額外的配置巫延。
額外配置
這里我們在resources中新建兩個文件效五,一個是jdbc.properties(數(shù)據(jù)庫連接配置文件,我們的測試數(shù)據(jù)庫的名字就叫ssm),另外一個是log4j.properties(log4j日志配置)炉峰。
jdbc.properties
# 連接url
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm
# 驅(qū)動器
jdbc.driverClassName=com.mysql.jdbc.Driver
# 用戶名
jdbc.usernaem=root
# 密碼
jdbc.password=root
log4j.properties
# Global logging configuration(全局配置)
log4j.rootLogger=DEBUG, stdout
# Console output...(控制臺打游费)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
配置Spring
在resources中新建applicationContext.xml文件,因?yàn)檫@個單獨(dú)解釋不太好解釋疼阔,我就在配置文件中以注釋的形式為大家解釋戒劫。
文件名:applicationContext.xml
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<?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-4.3.xsd">
? ? <!--
? ? ? ? 此標(biāo)簽用來掃描包以及包下面的類,被掃描到的類就可以使用注解婆廊,如@Service等迅细,同時被掃描到后就會被放入IOC容器中,
? ? ? ? 之所以不掃描Controller淘邻,就是因?yàn)镃ontroller是要交給SpringMVC掃描的茵典,后面會看到
? ? -->
? ? <context:component-scan base-package="com.roobtyan.ssm">
? ? ? ? <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
? ? </context:component-scan>
? ? <!--這里引入之前創(chuàng)建的jdbc配置文件,這樣可以很靈活的改變配置宾舅,而不需要修改xml文件-->
? ? <context:property-placeholder location="classpath:jdbc.properties"/>
? ? <!--配置連接池统阿,引入了jdbc.properties以后,就可以很方便的使用配置信息筹我,但是必須要用${}這種形式調(diào)用-->
? ? <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
? ? ? ? <property name="driverClass" value="${jdbc.driverClassName}"/>
? ? ? ? <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
? ? ? ? <property name="user" value="${jdbc.usernaem}"/>
? ? ? ? <property name="password" value="${jdbc.password}"/>
? ? </bean>
? ? <!--
? ? ? ? 配置mybatis整合開發(fā)
? ? ? ? Mybatis首先要有一個數(shù)據(jù)源扶平,所以我們就將剛剛配置的連接池注入進(jìn)來
? ? ? ? 然后還需要mapper.xml文件的路徑,之前學(xué)Mybatis的時候都是手動創(chuàng)建**.xml文件蔬蕊,然后用接口去映射结澄,
? ? ? ? 好消息是mybatis能夠幫助我們根據(jù)數(shù)據(jù)庫逆向生成這個文件及接口,極大的方便了我們的開發(fā)
? ? ? ? 最后就是mybatis的配置文件岸夯,我們會在后面看到
? ? ? ? -->
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <property name="mapperLocations" value="classpath:mapper/*.xml"/>
? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml"/>
? ? </bean>
? ? <!--
? ? ? ? 配置SqlSession
? ? ? ? sqlsession的作用就是sql會話麻献,沒有這個mybatis就無法真正作用起來,如果你學(xué)過mybatis一定知道這個配置的意義猜扮,
? ? ? ? 將sqlSessionFactory注入到其中赎瑰,至于下面那個參數(shù),暫時先不用理解
? ? ? ? -->
? ? <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
? ? ? ? <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
? ? ? ? <constructor-arg name="executorType" value="BATCH"/>
? ? </bean>
? ? <!--
? ? ? ? 將dao接口實(shí)現(xiàn)放入到IOC容器中破镰,由于我們還沒有配置mapper的接口文件餐曼,所以需要將接口實(shí)現(xiàn)文件放入到IOC中,注入的時候就能夠直接使用
? ? -->
? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? <property name="basePackage" value="com.roobtyan.ssm.dao"/>
? ? </bean>
? ? <!--
? ? ? ? 事務(wù)控制鲜漩,這個是Spring本身的配置了源譬,如果你不明白的話,還是建議再去學(xué)學(xué)Spring
? ? ? ? 這個最主要的作用就是:比如插入一條數(shù)據(jù)出錯的時候孕似,前面已經(jīng)插入到數(shù)據(jù)庫中部分信息踩娘,出錯了就需要先將數(shù)據(jù)從數(shù)據(jù)庫中刪除,不對數(shù)據(jù)造成污染
? ? -->
? ? <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? ? ? <!--控制數(shù)據(jù)源-->
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>
? ? <!--開啟基于注解的控制,一般使用配置方式進(jìn)行控制-->
? ? <aop:config>
? ? ? ? <!--切入點(diǎn)表達(dá)式-->
? ? ? ? <aop:pointcut id="txPoint" expression="execution(* com.roobtyan.ssm.service..*(..))"/>
? ? ? ? <!--事務(wù)增強(qiáng)配置-->
? ? ? ? <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
? ? </aop:config>
? ? <!--事務(wù)增強(qiáng)-->
? ? <tx:advice id="txAdvice">
? ? ? ? <tx:attributes>
? ? ? ? ? ? <tx:method name="*"/>
? ? ? ? ? ? <!--以get開始的所有方法-->
? ? ? ? ? ? <tx:method name="get*" read-only="true"/>
? ? ? ? </tx:attributes>
? ? </tx:advice>
? ? <!--Spring配置的核心:數(shù)據(jù)源\mybatis整合\事務(wù)控制\事務(wù)增強(qiáng)-->
</beans>
配置Mybatis
我們需要將mybatis也配置一下,相比較而言养渴,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.roobtyan.ssm.bean"/>
? ? </typeAliases>
</configuration>
就是這么簡單雷绢。
SpringMVC的配置文件位置并不在resources中,而在WEB-INF文件目錄下理卑,我們新建一個dispatcherServlet-servlet.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/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">
? ? <!--SpringMVC的配置文件翘紊,包含網(wǎng)站跳轉(zhuǎn)邏輯的控制,配置 -->
? ? <!-- 此時的掃描才是掃描Controller藐唠,開啟Controller注解 -->
? ? <context:component-scan base-package="com.roobtyan.ssm" 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 name="suffix" value=".jsp"/>
? ? </bean>
? ? <!--兩個標(biāo)準(zhǔn)配置,就是無論如何都需要配置的內(nèi)容? -->
? ? <!-- 將springmvc不能處理的請求交給tomcat -->
? ? <mvc:default-servlet-handler/>
? ? <!-- 能支持springmvc更高級的一些功能,JSR303校驗(yàn)宇立,快捷的ajax...映射動態(tài)請求 -->
? ? <mvc:annotation-driven/>
</beans>
你以為配置完成了嗎踪宠?不,并沒有妈嘹,還有一個最重要的柳琢,為了能夠讓SpringIOC容器能夠在服務(wù)器啟動的時候一起啟動,就需要將其配置在web.xml文件中润脸,同樣的還有一些必要的過濾器的配置柬脸,如字符編碼過濾器。
web.xml配置
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
? ? ? ? http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
? ? ? ? version="3.1">
? <display-name>Archetype Created Web Application</display-name>
? <!--
? ? ? 啟動spring容器,并且需要配置Spring的配置文件津函,這樣Spring容器才能夠正確的啟動
? -->
? <context-param>
? ? <param-name>contextConfigLocation</param-name>
? ? <param-value>classpath:applicationContext.xml</param-value>
? </context-param>
? <!--字符編碼過濾器配置-->
? <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>
? ? <init-param>
? ? ? <param-name>forceEncoding</param-name>
? ? ? <param-value>true</param-value>
? ? </init-param>
? </filter>
? <!--配置filter的映射-->
? <filter-mapping>
? ? <filter-name>characterEncodingFilter</filter-name>
? ? <url-pattern>/*</url-pattern>
? </filter-mapping>
? <!--配置監(jiān)聽器-->
? <listener>
? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? </listener>
? <!--
? ? SpringMVC攔截所有請求肖粮,這樣就能把請求交給SpringMVC了孤页,而不再是最初的樣子尔苦,但本質(zhì)上還是servlet
? -->
? <servlet>
? ? <servlet-name>dispatcherServlet</servlet-name>
? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? <load-on-startup>1</load-on-startup>
? </servlet>
? <servlet-mapping>
? ? <servlet-name>dispatcherServlet</servlet-name>
? ? <url-pattern>/</url-pattern>
? </servlet-mapping>
</web-app>
對了,最后不要忘了將配置文件引入環(huán)境行施,打開applicationContext.xml允坚,你會看到: