ssm框架的搭建流程

1.新建一个Maven project

  (1)选中create a simple project,自动配置必要的文件

  (2)Packaging选择war类型。jar和war的区别就是一个是普通的javaSE程序,一个是javaWEB程序。

  (3)新建的项目会报错,这是因为没有web.xml文件,选中目录中Deployment Descriptor,右键单击弹出菜单,单击Generate Deployment Descriptor Stub ,生成一个部署描述符存根,既web.xml文件。

2.部署tomcat运行环境,

  项目名右键—— properties —— maven/Targeted Runtimes , 选中所要部署的服务器,apply and close;

3.导入ssm框架所需要的jar包

  在pom.xml文件配置,可以在Dependencies中add添加,也可以直接从阿里云的maven仓库中将路径复制过来,下面是我在项目中使用到的jar包

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.tesu</groupId>
  <artifactId>StudentDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <!-- 单元测试 -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.10</version>
  </dependency>
  <!-- 三大组件的依赖jar包 -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>3.2.8.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>commons-dbcp</groupId>
   <artifactId>commons-dbcp</artifactId>
   <version>1.4</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>3.2.8.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>1.3.1</version>
  </dependency>
  <dependency>
   <groupId>Mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.38</version>
  </dependency>
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.2.8</version>
  </dependency>
  <!-- jsp标准标签库 -->
  <dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  <!-- json依赖的jar包 -->
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-core</artifactId>
   <version>2.2.3</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-annotations</artifactId>
   <version>2.2.3</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.2.3</version>
  </dependency>
  <!-- 文件上传依赖jar包 -->
  <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3</version>
  </dependency>
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>1.4</version>
  </dependency>
  
  <!-- aop依赖的jar包 -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.0</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjtools</artifactId>
      <version>1.8.0</version>
    </dependency>
    <!-- 下载Excel的组件 -->
    <dependency>
      <groupId>org.apache.poi</groupId>
       <artifactId>poi-ooxml</artifactId>
       <version>3.16</version>
    </dependency>
 </dependencies>
</project>

  jar包导错了,会报出各种奇奇怪怪的错误,而且不好找,最好能使用经过验证确实能用的jar包来构建项目。

  在构建项目的过程中出现过导错jar包,删了重导,结果jar包冲突的情况,最后是将.m2中的所有文件都删除了重新导入才解决的。

  而且在更换了jdk后原先能用的项目也不可以用了,好像是版本不匹配,我将jdk8u161换成了jdk8u144才又可以用的,用161的时候总是找不到数据库连接池对象。

4.配置web.xml文件

  这个文件中配置了DispatcherServlet,是处理所有前端发送过来的请求的,也是在这儿加载了所有的spring配置文件。

<?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_2_5.xsd" version="2.5">
  <display-name>DAY08-01-SSM-Student</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
      <servlet-name>Spring-SpringMVC-MyBatis</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:conf/spring-*.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>Spring-SpringMVC-MyBatis</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
<!-- 字符编码过滤器,只能解决post请求的编码问题,对于get请求不管用 -->
 <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>
 </filter>
 <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

 

5.配置数据库连接池

  (1)配置db.properties文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/user
username=root
password=123456
initsize=2
maxActive=10

  (2)配置spring-dao.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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  
   
    <!-- 加载db.properties配置 -->
    <util:properties id="dbConfig" location="classpath:conf/db.properties"></util:properties>
 <!-- 配置数据库连接池 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName" value="#{dbConfig.driver}"></property>
     <property name="url" value="#{dbConfig.url}"></property>
     <property name="initialSize" value="#{dbConfig.initsize}"></property>
     <property name="username" value="#{dbConfig.username}"></property>
     <property name="password" value="#{dbConfig.password}"></property>
     <property name="maxActive" value="#{dbConfig.maxActive}"></property>
    </bean>
 
 <!-- 配置dao接口扫描 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="cn.tedu.store.mapper;cn.tedu.store.*.mapper" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  
 </bean>
 
 <!-- 配置sql会话工厂 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mapperLocations" value="classpath:mappers/*.xml" />
 </bean>
 
 
 <!-- 配置spring事物管理组件 -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <!-- 支持事物注解 -->
 <tx:annotation-driven transaction-manager="transactionManager" />
   
</beans>

  (3)测试数据库连接池是否正常

import java.util.Date;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.tedu.store.bean.User;
import cn.tedu.store.mapper.UserMapper;

/**
 * 测试dao接口
 * @author Administrator
 *
 */
public class DaoTest {
    

    @Test
    public void testDao() {
        //读取配置文件
        AbstractApplicationContext aac = new ClassPathXmlApplicationContext("conf/spring-dao.xml");
        //获取bean对象
        BasicDataSource bds = aac.getBean("dataSource",BasicDataSource.class);
        //测试获取的对象
        System.out.println(bds.getDriverClassName().toString());
        
        //获取一个SQL会话工厂对象
        SqlSessionFactory sqlSessionFactory = aac.getBean("sqlSessionFactory",SqlSessionFactory.class);
        
        //打开session对象
        SqlSession session = sqlSessionFactory.openSession();  
        
        //通过session自动创建mapper代理对象  
        UserMapper dao = session.getMapper(UserMapper.class);  
    
        //插入一个用户对象
        User user = new User("张冠", 29, "1989-08-12", new Date());
        dao.addUser(user);
        System.out.println("插入了一个对象了");
        
        
    }
    
}

 

6.配置spring-mvc.xml

  这个文件主要是配置了通用的注解驱动,对controller的组件扫描,以及返回的前端页面的内部资源视图解析。

  

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    
    <context:component-scan 
        base-package="cn.tedu.store.controller" />
        
        
    <mvc:annotation-driven />
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/web/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    
    <!-- 上传文件的处理组件 -->
    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="50000000"/>
    <property name="defaultEncoding" value="utf-8"></property>
</bean>
    
</beans>


7.配置service的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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <context:component-scan 
        base-package="cn.tedu.store.service" />
    
    
   
    
</beans>

 

8.配置dao接口的sql映射

<?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="cn.tedu.store.mapper.UserMapper">
        <!-- 
        private int userId;
        private String username;
        private Integer age;
        private String birthday;
        private Date createDate;
         -->
    <insert id="addUser" parameterType="cn.tedu.store.bean.User" useGeneratedKeys="true">
         insert into 
         User(
         userId,username,age,birthday,createDate) 
         values(
         #{userId},#{username},#{age},#{birthday},#{createDate});
    
    </insert>

    <select id="findAllUser" resultType="cn.tedu.store.bean.User">
        select 
        userId,username,age,birthday,createDate 
        from 
        user;
    </select>


</mapper>

 

9.写bean对象的实体类

package cn.tedu.store.bean;

import java.util.Date;

/**
 * 用户的实体类
 * @author Administrator
 * create table user(
 * userId int auto_increment primary key,
 * username varchar(20),
 * age int,birthday varchar(20),
 * createDate timestamp);
 */
public class User {

    private int userId;
    private String username;
    private Integer age;
    private String birthday;
    private Date createDate;
    
    
    
    public String getUsername() {
        return username;
    }
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    public User() {
        super();
    }
    public User(String username, Integer age, String birthday, Date createDate) {
        super();
        this.username = username;
        this.age = age;
        this.birthday = birthday;
        this.createDate = createDate;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", age=" + age + ", birthday=" + birthday + ", createDate=" + createDate
                + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
        result = prime * result + ((createDate == null) ? 0 : createDate.hashCode());
        result = prime * result + ((username == null) ? 0 : username.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (age == null) {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        if (birthday == null) {
            if (other.birthday != null)
                return false;
        } else if (!birthday.equals(other.birthday))
            return false;
        if (createDate == null) {
            if (other.createDate != null)
                return false;
        } else if (!createDate.equals(other.createDate))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }
    
    
}

10,写dao层的接口

package cn.tedu.store.mapper;

import java.util.List;

import org.springframework.stereotype.Component;

import cn.tedu.store.bean.User;

/**
 * dao接口
 * @author Administrator
 *
 */
@Component
public interface UserMapper {
    //插入一个用户
    int addUser(User user);
    //查询到所有的用户
    List<User> findAllUser();
}

11.写service的接口和实现类

package cn.tedu.store.service;

import java.util.List;

import cn.tedu.store.bean.User;

/**
 * 业务层
 * @author Administrator
 *
 */
public interface UserService {
    /**
     * 查询到所有的用户
     * @return
     */
    List<User> findAllUser();
}
package cn.tedu.store.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.tedu.store.bean.User;
import cn.tedu.store.mapper.UserMapper;

@Service("userService")
public class UserServiceImpl implements UserService{
    
    @Resource
    @Qualifier("userMapper")
    private UserMapper userMapper;
    
    public List<User> findAllUser() {
        System.out.println("业务层开始查询所有的用户了");
        return userMapper.findAllUser();
    }

}

12.写controller实现类

package cn.tedu.store.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.store.bean.User;
import cn.tedu.store.service.UserService;

/**
 * 用户的控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource(name="userService")
    private UserService userService;
    /**
     * 查询到所有的用户展示到页面上
     */
    @RequestMapping("/findAllUser.do")
    public String findAllUser(ModelMap map) {
        System.out.println("查询到所有的用户列表展示到页面上的控制器");
        List<User> list = userService.findAllUser();
        System.out.println(list);
        map.addAttribute("users", list);
        return "userList";
    }
    
}

13.写jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户展示</title>
</head>
<body>
    <h1>用户展示</h1>
    <c:forEach items="${users }" var="user">
        <tr>
        <td>${user.userId }</td>
        <td>${user.username }</td>
        <td>${user.age }</td>
        <td>${user.birthday }</td>
        <td>${user.createDate }</td>
        <td><a href="#">删除</a></td>
        <td><a href="#">修改</a></td>
        </tr>
        <br>
    </c:forEach>
    
    
    

</body>
<!-- 引入jQuery框架 -->
<script src="js/jquery-3.1.1.min.js"></script>
</html>

 

posted @ 2018-04-06 14:25  兰溪三日桃花雨  阅读(411)  评论(0编辑  收藏  举报