Maven+SSM框架项目实例

一、项目环境

  • 开发系统:Window10
  • 开发工具:IDEA
  • JDK:1.8
  • 框架:Maven+Spring+SpringMVC+Mybatis
  • 数据库:Mysql

二、项目结构

项目文件架构:
Maven+SSM框架项目实例_第1张图片

三、Maven配置

pom.xml:

    <properties>
        
        <spring.version>4.0.2.RELEASEspring.version>
        
        <mybatis.version>3.2.6mybatis.version>
        
        <slf4j.version>1.7.7slf4j.version>
        <log4j.version>1.2.17log4j.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-oxmartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.2version>
        dependency>
        
        <dependency>
            <groupId>javaxgroupId>
            <artifactId>javaee-apiartifactId>
            <version>7.0version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.6version>
        dependency>
        
        <dependency>
            <groupId>commons-dbcpgroupId>
            <artifactId>commons-dbcpartifactId>
            <version>1.2.2version>
        dependency>
        
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        
        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>${log4j.version}version>
        dependency>


        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.1.41version>
        dependency>


        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>${slf4j.version}version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>${slf4j.version}version>
        dependency>
        
        
        <dependency>
            <groupId>org.codehaus.jacksongroupId>
            <artifactId>jackson-mapper-aslartifactId>
            <version>1.9.13version>
        dependency>
        
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>commons-iogroupId>
            <artifactId>commons-ioartifactId>
            <version>2.4version>
        dependency>
        <dependency>
            <groupId>commons-codecgroupId>
            <artifactId>commons-codecartifactId>
            <version>1.9version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.30version>
        dependency>
        <dependency>
            <groupId>com.google.code.gsongroupId>
            <artifactId>gsonartifactId>
            <version>2.3.1version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.4.2version>
        dependency>
    dependencies>

四、SSM的配置文件

mybatis-config.xml:


<configuration>
    <settings>
        <setting name="cacheEnabled" value="true"/>
    settings>
    
    <typeAliases>
        <typeAlias type="com.model.entity.Student" alias="Student"/>
        <typeAlias type="com.model.entity.Teacher" alias="Teacher"/>
        <typeAlias type="com.model.entity.Classes" alias="Classes"/>
    typeAliases>
configuration>

spring-mybatis.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <context:component-scan base-package="com.*"/>
    
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
          destroy-method="close">
        <property name="driverClassName">
            <value>${jdbc_driverClassName}value>
        property>
        <property name="url">
            <value>${jdbc_url}value>
        property>
        <property name="username">
            <value>${jdbc_username}value>
        property>
        <property name="password">
            <value>${jdbc_password}value>
        property>
        
        <property name="maxActive">
            <value>20value>
        property>
        
        <property name="initialSize">
            <value>1value>
        property>
        
        <property name="maxWait">
            <value>60000value>
        property>
        
        <property name="maxIdle">
            <value>20value>
        property>
        
        <property name="minIdle">
            <value>3value>
        property>
        
        <property name="removeAbandoned">
            <value>truevalue>
        property>
        
        <property name="removeAbandonedTimeout">
            <value>180value>
        property>
        
        <property name="connectionProperties">
            <value>clientEncoding=UTF-8value>
        property>
    bean>

    
    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:configLocation="classpath:mybatis-config.xml"
          p:mapperLocations="classpath:/mapping/*.xml"/>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
          p:basePackage="com.model.dao"
          p:sqlSessionFactoryBeanName="sqlSessionFactory"/>

    
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource"/>

beans>

springmvc-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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.controller"/>
    <mvc:annotation-driven/>
    
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="prefix" value="/WEB-INF/JSP/">property>
        <property name="suffix" value=".jsp">property>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    bean>

beans>

jdbc.properties:

jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/xxxx?characterEncoding=utf-8
jdbc_username=xxxx
jdbc_password=xxxx

五、数据库表

student表:
Maven+SSM框架项目实例_第2张图片
classes表:
Maven+SSM框架项目实例_第3张图片
teacher表:
这里写图片描述

六、各层代码

实体类
Classes:

package com.model.entity;

public class Classes {
    private int cid;
    private String cname;

    @Override
    public String toString() {
        return "Classes{" +
            "cid=" + cid +
            ", cname='" + cname + '\'' +
            '}';
    }

    public Classes() {
    }

    public Classes(int cid, String cname) {

        this.cid = cid;
        this.cname = cname;
    }

    public int getCid() {

        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }
}

Student:

package com.model.entity;

public class Student {
    private int sid;//学号
    private String sname;//姓名
    private Classes classes;//所属班级
    private String sex;//性别
    private String birthplace;//出生地

    @Override
    public String toString() {
        return "Student{" +
            "sid=" + sid +
            ", sname='" + sname + '\'' +
            ", classes=" + classes +
            ", sex='" + sex + '\'' +
            ", birthplace='" + birthplace + '\'' +
            '}';
    }

    public Student() {
    }

    public Student(int sid, String sname, Classes classes, String sex, String birthplace) {

        this.sid = sid;
        this.sname = sname;
        this.classes = classes;
        this.sex = sex;
        this.birthplace = birthplace;
    }

    public int getSid() {

        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirthplace() {
        return birthplace;
    }

    public void setBirthplace(String birthplace) {
        this.birthplace = birthplace;
    }
}

Teacher:

package com.model.entity;

public class Teacher {
    private int tid;//教师编号
    private String tname;//姓名
    private Classes classes;//所教班级(student的cid外键)

    public Teacher() {
    }

    @Override
    public String toString() {
        return "Teacher{" +
            "tid=" + tid +
            ", tname='" + tname + '\'' +
            ", classes=" + classes +
            '}';
    }

    public Teacher(int tid, String tname, Classes classes) {
        this.tid = tid;
        this.tname = tname;
        this.classes = classes;
    }

    public int getTid() {

        return tid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }
}

持久层(Dao类)
StudentDao:

package com.model.dao;

import com.model.entity.Student;

import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentDao {

    public List findAll();//查找所有学生

    public void add(@Param("sname") String sname, @Param("cid") int cid,
                    @Param("sex") String sex, @Param("birthplace") String birthplace);//添加学生

    public Student findBySid(int sid);//根据学号查学生

}

业务层(Service类)
StudentService:

import com.model.entity.Student;

import java.util.List;

public interface StudentService {
    public List findAll();

    public void add(String sname, int cid, String sex, String birthplace);//添加学生

    public Student findBySid(int sid);//根据学号查学生
}

StudentServiceImpl:

package com.service.Impl;

import com.model.dao.StudentDao;
import com.model.entity.Student;
import com.model.service.StudentService;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

@Service
public class StudentServiceImpl implements StudentService {

    //这里的自动装配会报错,可以忽视,因为在spring-mybatis.xml中有进行配置,通过mapper映射从Session工厂中获取
    @Resource
    private StudentDao studentDao;

    public List findAll() {
        return studentDao.findAll();
    }

    public void add(String sname, int cid, String sex, String birthplace) {

    }

    public Student findBySid(int sid) {
        return studentDao.findBySid(sid);
    }
}

控制器(Controller)
StudentAction:

package com.controller;

import com.model.entity.Student;
import com.model.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class StudentAction {

    @Autowired
    private StudentService service;//持有一个业务层对象

    @RequestMapping("/findall")
    public String findAll(Model model) {
        List list = service.findAll();
        model.addAttribute("studentlist", list);
        return "show";
    }

    //ajax查询数据
    @ResponseBody
    @RequestMapping("/findbysid")
    public Student findBySid(@RequestParam("sid") int sid) {
        return service.findBySid(sid);
    }
}

七、web.xml配置


<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" id="WebApp_ID"
         version="2.5">
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-mybatis.xml;
            classpath:springmvc-servlet.xml
        param-value>
    context-param>
    <context-param>
        <param-name>log4jConfigLocationparam-name>
        <param-value>classpath:log4j.propertiesparam-value>
    context-param>
    <context-param>
        <param-name>log4jRefreshIntervalparam-name>
        <param-value>6000param-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
    listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    <filter>
        <filter-name>characterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>characterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <load-on-startup>2load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>
    <display-name>ssm_studentdisplay-name>

    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>
web-app>

八、JSP页面

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Titletitle>
head>
<script type="text/javascript" src="js/jquery-2.1.1.min.js">script>

<script language="JavaScript">
    $(function () {
        $("#search").click(function () {
            $.ajax({
                       type: "post",
                       url: "findbysid.action",
                       data: $("#sid"),
                       dataType: "json",
                       success: function (data) {
                           var html = "";
                           html +=
                               "" + data.sid + "" + data.classes.cname + "" + data.sname
                               + "" + data.sex + "" + data.birthplace + "";
                           $("#student").html(html);
                       },
                       //null值不会被success回调函数捕获,因此可以通过error来实现该功能
                       error: function () {
                           alert("请输入正确的学号!");
                       }
                   })
        })
    })
script>
<body>
<center>
    <div style="margin-top: 25px">
        请输入所要查询的学号:<input type="text" id="sid" name="sid"/><input id="search" style="margin-left: 10px" type="button" value="搜索"><br/>
    div>
    <div style="margin-top: 50px">
        <table border="1">
            <tr>
                <td>学号td>
                <td>班级td>
                <td>姓名td>
                <td>性别td>
                <td>出生地td>
            tr>
            <tr id="student">

            tr>
        table>
    div>
center>
body>
html>

九、Tomcat测试

测试结果(成功):
Maven+SSM框架项目实例_第4张图片

输入1,没有对应学号,则弹出:
Maven+SSM框架项目实例_第5张图片

你可能感兴趣的:(实习)