一、项目环境

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

二、项目结构

项目文件架构: 

三、Maven配置

pom.xml

<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency> <!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>

四、SSM的配置文件

mybatis-config.xml

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<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"> <!-- 导入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注解的扫描路径 -->
<context:component-scan base-package="com.*"/>
<!-- 连接池 -->
<!-- 配置1个阿里连接池 -->
<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>20</value>
</property>
<!-- 初始化连接大小 -->
<property name="initialSize">
<value>1</value>
</property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait">
<value>60000</value>
</property>
<!-- 连接池最大空闲 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 连接池最小空闲 -->
<property name="minIdle">
<value>3</value>
</property>
<!-- 自动清除无用连接 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 清除无用连接的等待时间 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 连接属性 -->
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean> <!-- mybatis的sessionfatory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:mybatis-config.xml"
p:mapperLocations="classpath:/mapping/*.xml"/> <!-- dao的实现类对象,既mapper对象 。从上面的session工厂中,自动创建出所有mapper文件下的实现类对象 -->
<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/>
<!-- springmvc的视图解析器 -->
<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表: 
 
classes表: 
 
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<Student> 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);//根据学号查学生 }
package com.model.dao;

import com.model.entity.Student;

import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentDao {

    public List<Student> 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<Student> 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<Student> 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<Student> list = service.findAll();
model.addAttribute("studentlist", list);
return "show";
} //ajax查询数据
@ResponseBody
@RequestMapping("/findbysid")
public Student findBySid(@RequestParam("sid") int sid) {
return service.findBySid(sid);
}
}
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<Student> 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配置

<?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" id="WebApp_ID"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml;
classpath:springmvc-servlet.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <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>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<display-name>ssm_student</display-name> <welcome-file-list>
<welcome-file>index.jsp</welcome-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>Title</title>
</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 +=
"<td>" + data.sid + "</td><td>" + data.classes.cname + "</td><td>" + data.sname
+ "</td><td>" + data.sex + "</td><td>" + data.birthplace + "</td>";
$("#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测试

测试结果(成功): 

输入1,没有对应学号,则弹出: 

Maven+SSM框架项目实例——IDEA的更多相关文章

  1. [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World

    来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral ...

  2. 01 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之业务分析与DAO层

    作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 这是一个整合IDEA+Maven+SSM框架的高并发的商品秒杀项目.我们将分为以下几篇文章来进行详 ...

  3. Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...

  4. Maven+SSM框架实现简单的增删改查

    Spring介绍: spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情.然而, Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Ja ...

  5. Maven+SSM框架(Spring+SpringMVC+MyBatis)(二)

    1.基本概念 2.开发环境搭建 3.Maven Web项目创建 4.SSM整合 此次整合我分两个配置文件: 1)分别是spring-mybatis.xml,包含spring和mybatis的配置文件, ...

  6. 使用Maven+ssm框架搭建一个web项目

    1,前期准备:Eclipse(Mars.2 Release (4.5.2)).jdk1.7.tomcat7.maven3.2.1 2.使用eclipse中的maven新建一个web项目 点击next: ...

  7. 史上最详细的IDEA优雅整合Maven+SSM框架(详细思路+附带源码)

    目录 前言: 1. 搭建整合环境 2.Spring框架代码的编写 3.SpringMVC框架代码的编写 4. Spring整合SpringMVC的框架 5.MyBatis框架代码的编写 6. Spri ...

  8. Maven+SSM框架搭建【spring+springmvc+mybatis】

    本案例用到:ssm[spring+springmvc+mybatis]框架 数据库:mysql (推荐使用mysql 或者 sqlserver  .oracle太大,一般大型项目才会用到) 开发工具: ...

  9. IDEA + Maven + SSM 框架整合步骤

    因为前段时间自己想写个SSM的demo,然而不知怎么回事,配置完之后出现错误,怎么都调不好.最后从朋友那里拷了一个SSM的demo过来搭建成功,写这篇东西也是为了以后如果还有需要可以方便的查阅,并且也 ...

随机推荐

  1. dateframe取数据

    import numpy as npimport pandas as pd## x1=[1,2,3,4]# x2=[4,5,6,7]# x3=[7,8,9,10]# df=pd.DataFrame(# ...

  2. asp.net (jquery easy-ui datagrid)通用Excel文件导出(NPOI)

    http://www.cnblogs.com/datacool/archive/2013/03/12/easy-ui_datagrid_export_excel_asp_net.html

  3. 强制另存文件和加扩展名的代码c#

    强制另存为文件+扩展名的代码using System;using System.Collections.Generic;using System.Linq;using System.Web; name ...

  4. IE10中KendoUI treeview 点击后出现虚线框的解决方案

    在head中添加:<meta http-equiv="X-UA-Compatible" content="IE=edge"> 修改渲染模式即可.

  5. Jigloo 下载 安装 GUI

    这个需要授权,一直不能解决!! 网上找了很多,都觉不能访问,这个可以用Eclipse直接更新的 http://www.cloudgardensoftware.com/jigloo/update-sit ...

  6. 前端之JavaScript笔记1

    一 JavaScript的引入方式 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  7. struts2 action 中autowired 不能注入

    一.pom.xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>stru ...

  8. PhotoModeler Scanner教程

    Marking and Referencing 标记和校正 标记和校正是通过多张照片人工匹配共同特征的过程.点击一张图片的明显视觉特征,然后将它匹配到不同角度拍摄的其他图片的相同特征上. Approp ...

  9. 高翔《视觉SLAM十四讲》从理论到实践

    目录 第1讲 前言:本书讲什么:如何使用本书: 第2讲 初始SLAM:引子-小萝卜的例子:经典视觉SLAM框架:SLAM问题的数学表述:实践-编程基础: 第3讲 三维空间刚体运动 旋转矩阵:实践-Ei ...

  10. (KMP)Simpsons’ Hidden Talents -- hdu -- 2594

    http://acm.hdu.edu.cn/showproblem.php?pid=2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Ja ...