首页 > 代码库 > SSM——WEB环境下搭建框架(总结)

SSM——WEB环境下搭建框架(总结)

SSM——WEB环境下搭建框架(总结)

(步骤跟随程序执行顺序)

一、web应用开始运行,首先进入web.xml中,执行spring环境搭建工作

1 <!-- 配置spring环境 -->
2   <context-param>
3       <param-name>contextConfigLocation</param-name>
4       <param-value>classpath:applicationContext.xml</param-value>
5   </context-param>
6   <!-- 监听器 -->
7   <listener>
8       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
9   </listener>

二、程序进入到控制spring环境的applicationContext.xml文件中(该文件是放在类路径下),执行获取数据源配置文件的工作

 

1 <!-- 1. 获取数据源配置文件db.properties -->
2     <context:property-placeholder location="classpath:db.properties"/>

 

三、在类路径下创建数据源配置文件,注意key的命名前最好加上jdbc

 

jdbc.driverclassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username = scott
jdbc.password = tiger

 

四、applicationContext.xml文件中,配置数据源

 

1 <!-- 2. 配置数据源 -->
2     <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
3         <property name="driverClassName" value="${jdbc.driverclassName}"></property>
4         <property name="url" value="${jdbc.url}"></property>
5         <property name="username" value="${jdbc.username}"></property>
6         <property name="password" value="${jdbc.password}"></property>
7     </bean>

 

五、applicationContext.xml文件中,配置SqlSessionFactory

 

1 <!-- 3.配置SqlSessionFactory -->
2     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
3         <property name="dataSource" ref="dataSource"></property>
4         <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
5         <!-- 当mapper.java接口与mapper.xml文档分包管理时,需配置并指定mapper.xml所在包的路径 -->
6         <property name="mapperLocations" value="classpath:com/neuedu/dao/mapper/*.xml"></property>
7     </bean>

 

五(附加、可省略)、applicationContext.xml文件中,配置SqlSessionTemplate

1 <!-- 3.1  配置SqlSessionTemplate(可省略) -->
2     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
3         <constructor-arg ref="sqlSessionFactory"></constructor-arg>
4     </bean>

六、类路径下,建立管理数据库的sqlMapConfig.xml文件,批量扫描所有的mapper.xml映射文件(用Mybatis实现)

1 <!-- 映射文件 -->
2     <mappers>
3         <!-- 批量扫描:基于mapper配置方法 -->
4         <package name="com.neuedu.dao.mapper"/>
5     </mappers>

七、创建对应的包,编写dao层接口和mapper映射文件,注意编写规范

接口:

 1 package com.neuedu.dao;
 2 
 3 import com.neuedu.model.Emp;
 4 
 5 public interface EmpMapper {
 6     
 7     public Emp getEmpByEmpno(int empno);
 8     
 9     //<update id="updateEmpByEmpno" parameterType="com.neuedu.model.Emp">
10     public int updateEmpByEmpno(Emp emp);
11 }

映射文件:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.neuedu.dao.EmpMapper">
 4 
 5     <select id="getEmpByEmpno" parameterType="int" resultType="com.neuedu.model.Emp">
 6         select * from emp where empno = #{empno}
 7     </select>
 8     
 9     <update id="updateEmpByEmpno" parameterType="com.neuedu.model.Emp">
10         update emp set comm=(nvl(comm,0)+#{comm}) where empno=#{empno}
11     </update>
12     
13 </mapper>

八、sqlMapConfig.xml文件中,管理所有mapper代理

1 <!-- 4. 管理所有Mapper接口 -->
2     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
3         <!-- 配置mapper接口所在包 ,多个包之间","分隔-->
4         <property name="basePackage" value="com.neuedu.dao"></property>
5         <!-- 配置sqlSessionFactory(可省略,会自动注入) -->
6         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
7     </bean>

九、sqlMapConfig.xml文件中,配置事务管理器

 1 <!-- 5. 配置事务管理器 -->
 2     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 3         <property name="dataSource" ref="dataSource"></property>
 4     </bean>
 5     
 6     <!-- 5.1 开启注解事务 -->
 7     <tx:annotation-driven transaction-manager="transactionManager"/>
 8     
 9     <!-- 5.2 扫描所有的@service组件-->
10     <context:component-scan base-package="com.neuedu.service"></context:component-scan>
11 </beans>

此时,spring环境已全部搭建完成,当用户点击时,会开始配置spring-mvc环境

十、web.xml文件中,配置DispatcherServlet

 1 <!-- 配置spring-mvc环境:将所有请求与响应交给DispatcherServlet管理 -->
 2   <servlet>
 3       <servlet-name>springmvc</servlet-name>
 4       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 5       <init-param>
 6           <param-name>contextConfigLocation</param-name>
 7           <param-value>classpath:spring-mvc.xml</param-value>
 8       </init-param>
 9   </servlet>
10   <servlet-mapping>
11       <servlet-name>springmvc</servlet-name>
12       <url-pattern>*.action</url-pattern>
13   </servlet-mapping>

十一、在类路径下创建spring-mvc的配置文件spring-mvc.xml,配置适配器、映射器、处理器、视图解析器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xmlns:context="http://www.springframework.org/schema/context"
 5         xmlns:mvc="http://www.springframework.org/schema/mvc"
 6         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 8                             http://www.springframework.org/schema/context 
 9                             http://www.springframework.org/schema/context/spring-context-3.2.xsd
10                             http://www.springframework.org/schema/mvc 
11                             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" >
12 
13     <!-- 【注解】处理器适配器、处理器映射器 -->
14     <mvc:annotation-driven></mvc:annotation-driven>
15     
16     <!-- 处理器(扫描仪扫描所有@controller、@service、@repository、@component等) -->
17     <context:component-scan base-package="com.neuedu.controller"></context:component-scan>
18     
19     <!-- 视图解析器 -->
20     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
21         <property name="prefix" value="/"></property><!-- 前缀 -->
22         <property name="suffix" value=".jsp"></property><!-- 后缀 -->
23     </bean>
24 </beans>

十二、建包编写Model类,编写格式为JavaBean

十三、建包编写Service类,采取接口、实现方式,通过注解方式,声明此类为Service类,并注入mapper(以mapper接口的名字首字母小写自动取名)

 1 package com.neuedu.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 import com.neuedu.dao.EmpMapper;
 7 import com.neuedu.model.Emp;
 8 
 9 @Service("empService")
10 public class EmpServiceImpl implements IEmpService {
11 
12     @Autowired
13     private EmpMapper empMapper;
14     
15     public void setEmpMapper(EmpMapper empMapper) {
16         this.empMapper = empMapper;
17     }
18 
19     @Override
20     public Emp getEmpByEmpno(int empno) {
21         
22         return empMapper.getEmpByEmpno(empno);
23     }
24 
25     @Override
26     public boolean updateEmps(Integer[] empnos, double comm) {
27         
28         int count=0;
29         
30         for(int i=0;i<empnos.length;i++)
31         {
32             Emp emp = new Emp();
33             emp.setEmpno(empnos[i]);
34             emp.setComm(comm);
35             
36             //没遍历一次,修改一位雇员奖金
37             int result = empMapper.updateEmpByEmpno(emp);
38             if(result>0)
39             {
40                 count++;
41             }
42             
43             //制造异常
44             if(count==2)
45             {
46                 int j = 1/0;
47             }
48         }
49         
50         if(count ==empnos.length )
51         {
52             return true;
53         }
54         else
55         {
56             return false;
57         }
58     }
59 
60 }

注意:

在需使用事务的接口声明或方法处,加@Transactional修饰
public interface IEmpService {
//事务管理
@Transactional(rollbackForClassName="java.lang.Exception")
public boolean updateEmps(Integer[] empnos,double comm);

}

十四、建包编写控制器类,通过注解实现

 1 package com.neuedu.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 
 8 import com.neuedu.model.Emp;
 9 import com.neuedu.service.IEmpService;
10 
11 @Controller
12 public class EmpController {
13 
14     @Autowired
15     private IEmpService empService;
16     
17     public void setEmpService(IEmpService empService) {
18         this.empService = empService;
19     }
20 
21     @RequestMapping("/getEmpByEmpno")
22     public String getEmpByEmpno(Model model,Integer empno)
23     {
24         Emp emp = empService.getEmpByEmpno(empno);
25         
26         model.addAttribute("emp", emp);
27         
28         return "index";
29     }
30     @RequestMapping("/updateEmps")
31     public String updateEmps(Model model,String empnos,double comm)//123,456,789
32     {
33         //将"7698,7782,7839"字符串转换为int数组
34         String[] strEmpnos = empnos.split(",");
35         Integer[] intEmpnos = new Integer[strEmpnos.length];
36         for (int i=0;i<strEmpnos.length;i++) {
37             intEmpnos[i] = Integer.valueOf(strEmpnos[i]);
38         }
39         
40         boolean result = empService.updateEmps(intEmpnos, comm);
41         
42         model.addAttribute("result", result);
43         
44         return "index";
45     }
46     
47 }

十五、编写页面JSP

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="getEmpByEmpno.action">
11         根据雇员编号查询雇员信息:<input type="text" name="empno"><br/><br/>
12         
13         <input type="submit" value="查询"> 
14     </form>
15     
16     ${emp}
17     
18     <br/><br/>
19     
20     
21     <form action="updateEmps.action">
22         雇员编号:<input type="text" name="empnos">格式:7698,7782,7839...<br/><br/>
23         薪水调整幅度:<input type="text" name="comm"><br/><br/>
24         <input type="submit" value="更新"> 
25     </form>
26     
27     批量更新结果:${result}
28 </body>
29 </html>

 

SSM——WEB环境下搭建框架(总结)