需求:完成学生注册和信息浏览
1.建表Student
2.新建maven web项目
3.maven pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>SSM-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--Servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--jsp依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
</dependency>
<!--SpringMVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--事务相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--Jackson依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<!--mybatis-spring整合依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--数据库依赖-->
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!--druid连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
4.定义包,组织程序的结构
5.编写配置文件
Jdbc 属性配置文件 jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.user=system
jdbc.password=password
Spring 配置文件 applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--spring的配置文件,声明service,dao,工具类等对象-->
<!--加载文件-->
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<!--声明数据源,连接数据库-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--SqlSessionFactoryBean创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value="classpath:conf/mybatis.xml"/>
</bean>
<!--声明mybatis扫描器,创建dao对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="dao"/>
</bean>
<!--声明service的注解@Service所在的包名-->
<context:component-scan base-package="service"/>
<!--事务配置:注解的配置,aspectj的配置-->
</beans>
Springmvc 配置文件:springmvc.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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--SpringMVC的配置文件,用来声明controller和其他web相关的对象-->
<!--组件扫描器-->
<context:component-scan base-package="controller"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--注解驱动-->
<mvc:annotation-driven/>
<!--
1.响应ajax请求,返回json
2.静态资源响应问题
-->
</beans>
mybatis配置文件:mybatis.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>
<!--settings:控制mybatis全局行为-->
<settings>
<!--设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--定义别名-->
<typeAliases>
<!--包中的所有类,类名就是别名(类名不区分大小写)-->
<package name="domain"/>
</typeAliases>
<!--配置插件-->
<plugins>
<!--数据库分页插件-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
<!--sql mapper(sql映射文件)的位置-->
<mappers>
<!--一次指定一个目录下的所有文件求:
1.mapper文件名称需要和接口名称一样,区分大小写
2.mapper文件和dao接口需要在同一目录
-->
<package name="dao"/>
</mappers>
</configuration>
6.定义web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--注册中央调度器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--注册spring监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
<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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</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>
7.实体类Student
package domain;
public class Student {
private Integer SNO;
private String SNAME;
private String SSEX;
private String SDEPT;
public Student() {
}
public Student(Integer SNO, String SNAME, String SSEX, String SDEPT) {
this.SNO = SNO;
this.SNAME = SNAME;
this.SSEX = SSEX;
this.SDEPT = SDEPT;
}
public Integer getSNO() {
return SNO;
}
public void setSNO(Integer SNO) {
this.SNO = SNO;
}
public String getSNAME() {
return SNAME;
}
public void setSNAME(String SNAME) {
this.SNAME = SNAME;
}
public String getSSEX() {
return SSEX;
}
public void setSSEX(String SSEX) {
this.SSEX = SSEX;
}
public String getSDEPT() {
return SDEPT;
}
public void setSDEPT(String SDEPT) {
this.SDEPT = SDEPT;
}
@Override
public String toString() {
return "Student{" +
"SNO=" + SNO +
", SNAME='" + SNAME + '\'' +
", SSEX='" + SSEX + '\'' +
", SDEPT='" + SDEPT + '\'' +
'}';
}
}
8.Dao 接口和 sql 映射文件
package dao;
import domain.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectStudent();
}
<?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="dao.StudentDao">
<insert id="insertStudent">
insert into Student (SNO, SNAME, SSEX, SDEPT)
values (#{SNO}, #{SNAME}, #{SSEX}, #{SDEPT})
</insert>
<select id="selectStudent" resultType="domain.Student">
<include refid="studentSql"/>order by Sno
</select>
<!--<include refid="studentSql">-->
<sql id="studentSql">
select SNO,SNAME,SSEX,SDEPT from Student
</sql>
</mapper>
9.Service 接口和实现类
package service;
import domain.Student;
import java.util.List;
public interface StudentService {
int addStudent(Student student);
List<Student> listStudent();
}
package service.impl;
import dao.StudentDao;
import domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.StudentService;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
//引用类型自动注入@Autowired,@Resource
@Autowired
private StudentDao studentDao;
@Override
public int addStudent(Student student) {
return studentDao.insertStudent(student);
}
@Override
public List<Student> listStudent() {
return studentDao.selectStudent();
}
}
10.处理器定义
package controller;
import domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.StudentService;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService service;
//注册学生
@RequestMapping("/addStudent.do")
public ModelAndView addStudent(Student student){
ModelAndView mv = new ModelAndView();
String tips = "注册失败";
//调用service处理Student类
int num = service.addStudent(student);
if (num>0){
//注册成功
tips = "学生{" + student.getSNAME() + "}注册成功";
}
mv.addObject("tips",tips);
mv.setViewName("result");
return mv;
}
//处理查询,响应ajax
@RequestMapping("queryStudent.do")
@ResponseBody
public List<Student> queryStudent(){
//参数检查,简单数据处理
return service.listStudent();
}
}
11.定义视图-首页文件--- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!--指定路径:-->
<%
String basePath = request.getScheme()+"://"+
request.getServerName()+":"+request.getServerPort()+
request.getContextPath()+"/";
%>
<html>
<head>
<title>功能入口</title>
<!--指定 base 标签:-->
<base href="<%=basePath%>"/>
</head>
<body>
<div align="center">
<p>SSM整合的例子</p>
<img src="images/1.png" height="200" width="250" alt="显示错误"/>
<table>
<tr>
<td><a href="addStudent.jsp">注册学生</a></td>
</tr>
<tr>
<td><a href="listStudent.jsp">浏览学生</a></td>
</tr>
</table>
</div>
</body>
</html>
12.注册学生页面--addStudent.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme()+"://"+
request.getServerName()+":"+request.getServerPort()+
request.getContextPath()+"/";
%>
<html>
<head>
<title>学生注册</title>
<base href="<%=basePath%>"/>
</head>
<body>
<div align="center">
<form action="student/addStudent.do" method="post">
<table>
<tr>
<td>学号</td>
<td><input type="text" name="SNO"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="SNAME"></td>
</tr>
<tr>
<td>性别</td>
<td><input type="text" name="SSEX"></td>
</tr>
<tr>
<td>学院</td>
<td><input type="text" name="SDEPT"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
13.浏览学生界面--listStudent.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme()+"://"+
request.getServerName()+":"+request.getServerPort()+
request.getContextPath()+"/";
%>
<html>
<head>
<title>查询学生ajax</title>
<base href="<%=basePath%>"/>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script type="text/javascript">
$(function (){
//在当前页面dom对象加载后,执行loadStudentData()
loadStudentData();
$("#btn").click(function () {
loadStudentData();
})
})
function loadStudentData(){
$.ajax({
url:"student/queryStudent.do",
type:"get",
dataType:"json",
success:function (data) {
//清除旧数据
$("#info").html("");
//添加新数据
$.each(data,function (i,n) {
$("#info").append("<tr>")
.append("<td>"+n.sno+"</td>")
.append("<td>"+n.sname+"</td>")
.append("<td>"+n.ssex+"</td>")
.append("<td>"+n.sdept+"</td>")
.append("</tr>");
})
}
})
}
</script>
</head>
<body>
<div align="center">
<table>
<thead>
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>学院</td>
</tr>
</thead>
<tbody id="info">
</tbody>
</table>
<input type="button" id="btn" value="查询数据">
</div>
</body>
</html>
14.注册结果页面--result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
result.jsp 结果页面,注册结果:${tips}
</body>
</html>