配置pom.xml环境
<dependencies>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--servlet jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<!--解决静态资源导出问题-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<!--设置默认编码-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
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>
<!--配置dispatcherServlet前端控制器-->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--绑定springmvc.xml配置文件,这里因为所有的配置文件都整合到了applicationContext.xml中,所以绑定applicationContext.xml-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--启动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!--处理所有的url:/
处理所有的url包括jsp:/*-->
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:springmvc.xml"/>
</beans>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--关联配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--配置动态实现dao注入到spring中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--要扫描dao的包-->
<property name="basePackage" value="com.xxx.dao"/>
</bean>
</beans>
spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将所有的业务类注入spring中或使用注解,这里使用注入-->
<bean id="studentServiceImpl" class="com.xxx.service.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"/>
</bean>
<!--声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--处理器映射器-->
<!--用注解,开启注解驱动-->
<mvc:annotation-driven/>
<!--静态资源过滤-->
<mvc:default-servlet-handler/>
<!--扫描包-->
<context:component-scan base-package="com.xxx.controller"/>
<!--处理器适配器-->
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
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>
<!--配置数据源 交给spring-->
<!--设置日志打印-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--起别名-->
<typeAliases>
<package name="com.xxx.pojo"/>
</typeAliases>
</configuration>
databases.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo01?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
insert.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加学生</title>
<%--引入Bootstrap--%>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form action="${pageContext.request.contextPath}/student/insertStudent" method="post">
<div class="form-group">
<label for="exampleInputEmail1">名称</label>
<input type="text" name="name" class="form-control" id="exampleInputEmail1" placeholder="请输入姓名">
</div>
<div class="form-group">
<label for="exampleInputPassword1">年龄</label>
<input type="text" name="age" class="form-control" id="exampleInputPassword1" placeholder="请输入年龄">
</div>
<button type="submit" class="btn btn-default">添加</button>
</form>
</body>
</html>
selectAll.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>
<%--引入Bootstrap--%>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>显示所有数据</small>
</h1>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<%--toolAdd--%>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/student/insert">添加学生</a>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/student/findAll">主页</a>
</div>
<form class="form-inline" action="${pageContext.request.contextPath}/student/selectName" method="post">
<div class="form-group">
<samp style="color: crimson">${er}</samp>
<input type="text" name="name" class="form-control" id="inputPassword2" placeholder="请输入要查询的名称">
</div>
<button type="submit" class="btn btn-primary">查询</button>
</form>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>学号</th>
<th>名称</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<%--从数据库中查询书来,从all中遍历出来--%>
<tbody>
<c:forEach var="student" items="${list}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>
<a href="${pageContext.request.contextPath}/student/updateStudent?id=${student.id}">修改</a>
|
<a href="${pageContext.request.contextPath}/student/deleteStudent?id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改学生信息</title>
<%--引入Bootstrap--%>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form action="${pageContext.request.contextPath}/student/update" method="post">
<input type="hidden" name="id" value="${student.id}">
<div class="form-group">
<label for="exampleInputEmail1">名称</label>
<input type="text" name="name" value="${student.name}" class="form-control" id="exampleInputEmail1" placeholder="请输入姓名">
</div>
<div class="form-group">
<label for="exampleInputPassword1">年龄</label>
<input type="text" name="age" value="${student.age}" class="form-control" id="exampleInputPassword1" placeholder="请输入年龄">
</div>
<button type="submit" class="btn btn-default">修改</button>
</form>
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/student/findAll">点击进入</a>
</body>
</html>
dao层
package com.xxx.dao;
import com.xxx.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentMapper {
//查询所有学生
List<Student> findAll();
//查询一个学生
Student findOne(@Param("id") int id);
//添加学生
int insert(Student student);
//修改学生信息
int update(Student student);
//删除学生
int delete(@Param("id") int id);
//根据学生姓名获取信息
Student select(@Param("name") String name);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.dao.StudentMapper">
<select id="findAll" resultType="student">
select * from student
</select>
<select id="findOne" resultType="student">
select * from student where id=#{id}
</select>
<insert id="insert" parameterType="student">
insert into student(name,age) value(#{name},#{age})
</insert>
<delete id="delete" parameterType="int">
delete from student where id=#{id}
</delete>
<update id="update" parameterType="student">
update student set name=#{name},age=#{age} where id=#{id}
</update>
<select id="select" resultType="student" parameterType="String">
select * from student where name=#{name}
</select>
</mapper>
service层
package com.xxx.service;
import com.xxx.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentService {
//查询所有学生
List<Student> findAll();
//查询一个学生
Student findOne(int id);
//添加学生
int insert(Student student);
//修改学生信息
int update(Student student);
//删除学生
int delete(int id);
//根据学生姓名获取信息
Student select(String name);
}
package com.xxx.service;
import com.xxx.dao.StudentMapper;
import com.xxx.pojo.Student;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
public List<Student> findAll() {
return studentMapper.findAll();
}
public Student findOne(int id) {
return studentMapper.findOne(id);
}
public int insert(Student student) {
return studentMapper.insert(student);
}
public int update(Student student) {
return studentMapper.update(student);
}
public int delete(int id) {
return studentMapper.delete(id);
}
public Student select(String name) {
return studentMapper.select(name);
}
}
controller层
package com.xxx.controller;
import com.xxx.pojo.Student;
import com.xxx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/student")
public class TestController {
@Autowired
@Qualifier("studentServiceImpl")
private StudentService studentService;
//查询所有学生
@RequestMapping("/findAll")
public String findAll(Model model){
List<Student> list = studentService.findAll();
model.addAttribute("list",list);
return "selectAll";
}
//跳转到添加页面
@RequestMapping("/insert")
public String insert(){
return "insert";
}
//添加学生,@RequestMapping("/insertStudent")不能出现相同
@RequestMapping("/insertStudent")
public String insert(Student student){
studentService.insert(student);
return "redirect:/student/findAll";
}
//删除学生
@RequestMapping("/deleteStudent")
public String delete(int id){
studentService.delete(id);
return "redirect:/student/findAll";
}
//跳转到修改页面
@RequestMapping("/updateStudent")
public String update(int id,Model model){
Student student = studentService.findOne(id);
model.addAttribute("student",student);
return "update";
}
//修改数据
@RequestMapping("/update")
public String update(Student student){
studentService.update(student);
return "redirect:/student/findAll";
}
//通过姓名获取数据
@RequestMapping("/selectName")
public String select(String name,Model model){
Student student = studentService.select(name);
List<Student> list=new ArrayList<Student>();
list.add(student);
if (student==null){
list=studentService.findAll();
model.addAttribute("er","未查到");
}
model.addAttribute("list",list);
return "selectAll";
}
}
pojo层
package com.xxx.pojo;
public class Student {
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}