IDEA環境下SSM整合------環境配置
宣告:本文純為個人筆記整理,如有不妥之處還望及時指出,歡迎轉載!
只為解決操作問題,可以從第二幅圖往後看!
一、做不出詳細的概念敘述和文字設計,本文主要以實戰步驟為主,少量解釋為輔助,下面請大家牢記兩幅圖:
截圖來自張開濤部落格
典型的jsp(java server page)是一種伺服器執行的元件,執行在標準HTML頁面的java技術,最終還是被編譯成伺服器可以識別的servlet程式碼,生成.class檔案,相比Servlet開發簡潔、方便,缺點是檢視顯示、業務控制比較複雜,而且把對後端操作放在jsp頁面很不安全; 故產生了Jsp+JavaBean開發,使用<jsp:usebean>標籤,自動將請求封裝為javabean元件,可以認為是為了增強jsp開發;這也是單一的開發思路,即下圖所示,這種模型同樣是沒有本子上解決問題,大量的業務處理依然在jsp頁面中,而bean負責的只有引數的請求處理;所以出現了我們的MVC模型; jsp+bean模型
MVC模型
在JvavEE的開發中,可以認為下圖就是我們的web mvc模型
控制器用servlet、模型用JavaBean、檢視採用Jsp
二、下面開始步入正題;
進行springmvc開發,寫業務邏輯之前,首先要做到:
1.開發環境(jdk+IDE+maven+mysql)
2.配置環境(web.xml、pom.xml、application.xml、springmvc.xml、mybatis.xml)
3.定義domian、生成dao、mapper
按照上述步驟進行開發,便於在後期調錯,另外 可以個那好的梳理業務邏輯,本文只介紹,檔案配置到連線上資料庫。
三、SpringMvc開發用到的通用配置檔案
1.pom.xml這裡貼上一個詳細的pom檔案,包含各種依賴和maven常用的幾個外掛
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Traing</groupId> <artifactId>springmvc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <webVersion>2.3</webVersion> </properties> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.3</version> </dependency> <!--以下四個是SpringMvc中資料校驗jar包jsr 303的相關依賴 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.3.Final</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.1.1.GA</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>bean-validator</artifactId> <version>3.0-JBoss-4.0.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.enterprise.deploy</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.jms</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.management.j2ee</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId> <version>2.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.resource</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.security.auth.message</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.security.jacc</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.servlet</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.servlet.jsp</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.servlet.jsp.jstl</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api-osgi</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jstl-impl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>webservices-api-osgi</artifactId> <version>2.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.jboss.weld</groupId> <artifactId>weld-osgi-bundle</artifactId> <version>1.0.1-SP3</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.servlet.jsp.jstl</artifactId> <version>1.2.1</version> </dependency> <!-- 專案其他依賴 --> <!-- mysql 依賴 https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.23</version> </dependency> <!-- junit 依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Mybatis --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> <!-- spring 依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.18.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.18.RELEASE</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.18.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.18.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.2.18.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver --> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.18.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.18.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.18.RELEASE</version> </dependency> <!-- spring mybatis 整合依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--生成程式碼外掛 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.3</version> <type>jar</type> </dependency> <!-- 連線池 --> <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool --> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <!-- 檔案上傳 --> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!-- 匯入c3p0依賴 --> <dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.3.1</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies> <build>
//打成jar時會自動打包依賴 <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.4</version> <configuration> <createDependencyReducedPom>true</createDependencyReducedPom> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.cmos.task.TaskManager</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins>
</build> </project>
2.web.xml 需要注意其中兩個配置檔案 一個是application.xml :主要配置dataSource、dao、service等跟持久化相近的非webBean
一個是springmvc.xml:配置controller、handlerResolver檢視解析器等web層元件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc</display-name> <welcome-file-list> <welcome-file>/views/login.jsp</welcome-file> </welcome-file-list> <!-- 處理字符集編碼 --> <filter> <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置springmvc核心過濾器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置spring的web工廠 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <!-- RESTful風格的配置:把POST請求轉為PUT、DELETE請求--> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.application.xml由於我主要用的註解驅動開發,所以這個配置檔案沒有相關的dao、servic的bean配置、我將mybatis的配置單獨寫在Mybatis.xml檔案裡,但是不要忘記引入到這裡。如果只有資料庫配置的話,也可以直接寫進來(不建議)
4.Mybatis.xml 注意 db.properties 檔案中的內容username、password注意不要配置的太通用 有可能會和本機的配置衝突
db.properties檔案中的內容
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/studentmanager?useUnicode=true&characterEncoding=UTF-8 jdbcUserName=root jdbcPassword=root
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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="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-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置資料來源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driverClassName}"> </property> <property name="url" value="${url}"> </property> <property name="username" value="${jdbcUserName}"> </property> <property name="password" value="${jdbcPassword}"> </property> </bean> <!--配置sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 載入mybatis的全域性配置檔案 <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/> --> <!-- 資料庫連線池 --> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:/Mapper/*Mapper.xml"> </property> <property name="typeAliasesPackage" value="classpath:com.cmos.domain"> </property> </bean> <!-- mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描包路徑,如果需要掃描多個包,中間使用半形逗號隔開--> <property name="basePackage" value="com.cmos.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"> </property> </bean> <!-- 使用annotation註解方式配置事務 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5.SpringMVC.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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 包含:webBean ViewResolver、handlerAdapter controller handlerMapping--> <!-- 配置自定義掃描的包 --> <context:component-scan base-package="com.cmos.controller,com.cmos.service,com.cmos.dao"/> <!-- 配置檢視解析器:如何把handler方法返回值解析為實際的物理檢視 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"> </property> <property name="suffix" value=".jsp"> </property> </bean> <!-- default-servlet-handler 會在SpringMvc 上下文中定義一個DefaultServletHttpRequestHandler, 它會對進入DispatcherServlet的請求進行篩查,如果發現沒有經過對映的請求,就將該請求交由web應用伺服器 預設的Servlet處理,如果不是靜態資源則用DispathcerServlet進行處理 --> <mvc:default-servlet-handler/> <mvc:annotation-driven> </mvc:annotation-driven> <!--攔截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/district"/> <mvc:mapping path="/organization"/> <mvc:mapping path="/unit"/> <mvc:mapping path="/student"/> <!--com.coms...是我專案的包的名稱不要誤解 --> <bean class="com.cmos.handlers.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!-- 配置SimpleMappingExceptionResolver --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop> </props> </property> </bean> </beans>
6.dao和mapper.xml 我這裡只粘了一個dao和mapper的樣例
特別注意:
1.dao引數型別和Mapper的引數型別嚴格對稱
2.mapper中的resultMap配置,前面是資料庫欄位名、後面是對應物件名,要嚴格一致
3.不要在Mybtais.xml中引入dao和mapper的位置
package com.cmos.dao; import com.cmos.domain.District; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface DistrictDao { int deleteByPrimaryKey(Integer disId); int insert(District record); int updateByExample(@Param("example") District example); List<District> selectAll(); } Mapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cmos.dao.DistrictDao"> <resultMap id="BaseResultMap" type="com.cmos.domain.District"> <id column="dis_id" jdbcType="INTEGER" property="disId" /> <result column="dis_name" jdbcType="VARCHAR" property="disName" /> </resultMap> <select id="selectAll"resultMap="BaseResultMap"> select dis_id,dis_name from district </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from district where dis_id = #{disId,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.cmos.domain.District"> insert into district (dis_id, dis_name) values (#{disId,jdbcType=INTEGER}, #{disName,jdbcType=VARCHAR}) </insert> <update id="updateByExample" parameterType="com.cmos.domain.District"> update district set dis_name = #{example.disName,jdbcType=VARCHAR} where dis_id = #{example.disId,jdbcType=INTEGER} </update> </mapper>