搭建ssm开发环境的步骤

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节点,加入以下的依赖。

<dependency>
  <groupId>javaxgroupId>
  <artifactId>javaee-web-apiartifactId>
  <version>7.0version>
dependency>


<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-txartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-webartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-oxmartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-webmvcartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-expressionartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-testartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-context-supportartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-contextartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-ormartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-messagingartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-aopartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-instrument-tomcatartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-coreartifactId>
  <version>4.2.7.RELEASEversion>
  dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-jmsartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-websocketartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-aspectsartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-jdbcartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-beansartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-instrumentartifactId>
  <version>4.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-webmvc-portletartifactId>
  <version>4.2.7.RELEASEversion>
dependency>


<dependency>
  <groupId>org.mybatisgroupId>
  <artifactId>mybatisartifactId>
  <version>3.4.1version>
dependency>

<dependency>
  <groupId>mysqlgroupId>
  <artifactId>mysql-connector-javaartifactId>
  <version>5.1.12version>
dependency>


<dependency>
  <groupId>org.mybatisgroupId>
  <artifactId>mybatis-springartifactId>
  <version>1.3.0version>
dependency>


<dependency>
  <groupId>c3p0groupId>
  <artifactId>c3p0artifactId>
  <version>0.9.1version>
dependency>


<dependency>
  <groupId>jstlgroupId>
  <artifactId>jstlartifactId>
  <version>1.2version>
dependency>


<dependency>
  <groupId>commons-fileuploadgroupId>
  <artifactId>commons-fileuploadartifactId>
  <version>1.2.2version>
dependency>
<dependency>
  <groupId>commons-iogroupId>
  <artifactId>commons-ioartifactId>
  <version>2.0.1version>
dependency>
 

3. 配置web.xml



	
  selling system
  
  
  
    selling
    org.springframework.web.servlet.DispatcherServlet
    1
  
  
    selling
    /
  
  
  
  
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    characterEncodingFilter
    /*
    

在这里配置了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



<configuration>
  
  <typeAliases>
    
    <package name="com.zhklong.selling.entity"/>
  typeAliases>
  
  
  <mappers>
    <package name="com.zhklong.selling.mapper"/>
  mappers>
configuration>

 

5. 配置spring配置文件

spring , springmvc 都是在这个配置文件配置,缺省的情况下,spring(mvc)的配置文件会在项目的/WEB-INF/目录下,命名规则为:上面配置的ServletDispatcher的名字-servlet.xml,如:   selling-servlet.xml
在该文件中应该存在这些内容

<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">

	
	<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 />
	
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.propertiesvalue>
				<value>classpath:sms.propertiesvalue>
			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>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatisConfig.xml" />
	bean>
	
	<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
  1. 配置页面路径:springmvc的controller中的对于处理方法会返回一个结果的字符串,springmvc会根据这个配置在这个字符串加上前缀和后缀,组成路径,进而进行访问
  2. 配置静态资源路径:由于springmvc是通过Servlet来进行请求分发的,而且在web.xml中配置了url-pattern为/,这以为着springmvc会拦截所有的请求,包括访问.html等静态资源。配置这个路径会在访问静态资源时可以顺利的访问到。
  3. 配置文件的导入:做了这个可以让需要修改数据库相关的修改时,可以只改jdbc.properties,而不需要该这个xxx-servlet.xml
  4. 数据库连接池配置:在这里的例子是c3p0连接池,这里用到${属性名}来获取配置文件中的对应值。
  5. 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/....目录下
内容如下





  
  
  

然后,还需要在selling-servlet.xml配置文件中配置,添加这个内容到标签前


  
  

(2). Controller

在com.zhklong.celling.controller包创建controller
 
package 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中添加


  

需要在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.

你可能感兴趣的:(web框架)