搭建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节点,加入以下的依赖。
<!--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
  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/....目录下
内容如下
<?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包创建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中添加
<!-- 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.
展开阅读全文

没有更多推荐了,返回首页