搭建ssm開發環境的步驟
阿新 • • 發佈:2019-02-20
as usual , this is also on my personal website , http://www.wenjingyi.top
概述
當把三個框架都稍微熟悉了,就會考慮該如何將這幾個框架整合,做一個小系統出來。步驟
1. 建立maven web專案
當熟悉maven之後,就不願意使用eclipse原有的專案工具。eclipse ee 4.3之後有外掛支援建立maven專案。2. 加入jar包
maven的好處,只需要在pom.xml配置檔案中加入dependency即可。在pom.xml配置檔案中找到dependencies節點,加入以下的依賴。
<!--java ee-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId >
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId >
<artifactId>spring-oxm</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--mysql驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.12</version>
</dependency>
<!--spring mybatis整合相關-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--資料庫連線池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
<!--jstl標籤庫-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--檔案上傳相關-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
3. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>selling system</display-name>
<!-- spring mvc Dispatcher Servlet -->
<servlet>
<servlet-name>selling</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>selling</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- encoding filter -->
<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-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在這裡配置了springmvc 進行分發請求的ServletDispatcher和處理中文亂碼的characterEncodingFilter
4. 配置jdbc配置檔案
既然是小系統,免不了需要連線資料庫,CRUD的。在類路徑下。maven的話需要新增到resources資料夾的根目錄,才會被複制到類路徑
命名為:jdbc.properties。習慣性命名,不是一定要這樣命。
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/zx
username=yourName
password=yourPassword
maxPoolSize=20
maxIdleTime=1800
這些屬性會在spring配置檔案中使用到
配置mybatis配置檔案
mybatis要根據這個配置檔案來得到mapper的位置,類的別名等命名:mybatisConfig.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>
<!-- 配置類別名 -->
<typeAliases>
<!-- 配置一個包可以讓包中的類名作為全類名的別名 -->
<package name="com.zhklong.selling.entity"/>
</typeAliases>
<!-- 配置mapper的xml檔案路徑 -->
<mappers>
<package name="com.zhklong.selling.mapper"/>
</mappers>
</configuration>
5. 配置spring配置檔案
spring , springmvc 都是在這個配置檔案配置,預設的情況下,spring(mvc)的配置檔案會在專案的/WEB-INF/目錄下,命名規則為:上面配置的ServletDispatcher的名字-servlet.xml,如: selling-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-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 預設掃描controller包 -->
<context:component-scan base-package="com.zhklong.selling.controller" />
<!-- 配置頁面路徑 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置靜態資源路徑 -->
<mvc:resources location="/WEB-INF/static/" mapping="/**" />
<!-- 配置註解驅動 -->
<mvc:annotation-driven />
<!-- 配置jdbc配置檔案路徑 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:sms.properties</value>
</list>
</property>
</bean>
<!-- 配置資料庫連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<property name="maxPoolSize" value="${maxPoolSize}" />
<property name="maxIdleTime" value="${maxIdleTime}" />
</bean>
<!-- SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatisConfig.xml" />
</bean>
<!-- 配置mapper路徑和sqlSessionFactory -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhklong.selling.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<beans>
somethings to notice
- 配置頁面路徑:springmvc的controller中的對於處理方法會返回一個結果的字串,springmvc會根據這個配置在這個字串加上字首和字尾,組成路徑,進而進行訪問
- 配置靜態資源路徑:由於springmvc是通過Servlet來進行請求分發的,而且在web.xml中配置了url-pattern為/,這以為著springmvc會攔截所有的請求,包括訪問.html等靜態資源。配置這個路徑會在訪問靜態資源時可以順利的訪問到。
- 配置檔案的匯入:做了這個可以讓需要修改資料庫相關的修改時,可以只改jdbc.properties,而不需要該這個xxx-servlet.xml
- 資料庫連線池配置:在這裡的例子是c3p0連線池,這裡用到${屬性名}來獲取配置檔案中的對應值。
- sqlSessionFactory的配置:這是spring和mybatis整合的關鍵,在這個配置時將上一步的dataSource資料庫連線池和mybatis的配置檔案路徑設定到這個類例項中。
6. 從資料庫到頁面的例子
做完上面的配置已經可以進行實際的開發了(編寫mapper,實體類,controller,頁面等)(1). mapper
我配置的路徑是 com.zhklong.selling.mapper,所以自然得在這個路徑中。package com.zhklong.selling.mapper;
import com.zhklong.selling.entity.Employee;
public interface EmployeeMapper {
Employee getByCell(String cellphone);
}
這只是一個介面,而用進行訪問資料庫,需要配置sql語句(mybatis),hibernate是不用配置sql語句的。配置sql語句需要在與mapper類在同一路徑建立與mapper同名的xml檔案,如 com/zhklong/selling/mapper/EmployeeMapper.xml,同樣maven需要放到resources/....目錄下
內容如下
<?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">
<!--namespace指定為mapper類-->
<mapper namespace="com.zhklong.selling.mapper.EmployeeMapper">
<!--select的id要與mapper類的方法名相同-->
<select id="getByCell" resultType="Employee">
select * from employee where cellphone=#{0}
</select>
</mapper>
然後,還需要在selling-servlet.xml配置檔案中配置,新增這個內容到</beans>標籤前
<!-- Mapper -->
<bean id="employeeMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.zhklong.selling.mapper.EmployeeMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
(2). Controller
在com.zhklong.celling.controller包建立controllerpackage com.zhklong.selling.controller;
/**
* @author paul
* @since 2016-11-16 職工控制類
*
* */
@Controller
@RequestMapping(path = "/employee")
public class EmployeeController {
private EmployeeMapper employeeMapper;
public void setEmployeeMapper(EmployeeMapper employeeMapper) {
this.employeeMapper = employeeMapper;
}
@RequestMapping(path = "/getByCell", method = RequestMethod.GET)
public String login(@RequestParam("cellphone")String cellphone,Model model) {
model.addAttribute("employee", employeeMapper.getByCell(cellphone));
return "page";
}
}
需要在selling-servlet.xml中新增
<!-- Controller -->
<bean id="employeeController" class="com.zhklong.selling.controller.EmployeeController">
<property name="employeeMapper" ref="employeeMapper" />
</bean>
需要在employeeMapper之後,beans節點之前
(3). 新增page.jsp
由於配置,需要在/WEB-INF/jsp/路徑下配置。在page.jsp中可以通過request域得到employee物件(4). 訪問
通過http://localhost:8080/selling/employee/getByCell?cellphone=111111111訪問。Action speak louder than words.