一、环境搭建
1.创建一个springboot项目(勾选web)
2.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
3.创建数据库表并插入数据
二、代码编写
1.全局配置文件编写,application.yml配置信息
#配数据源信息
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring-mybatis?serverTimezone=Asia/Shanghai
username: root
password: 625814
mvc: #配置jsp映射路径 return "index" /index.jsp
view:
prefix: /
suffix: .jsp
main:
allow-circular-references: true
mybatis:
type-aliases-package: com.zyl.pojo #别名
mapper-locations: classpath:/mapper/*Mapper.xml
2.全查功能实现
(1). 封装实体类
package com.zyl.pojo;
public class Student {
private Integer id;
private String name;
private Integer age;
private String gender;
public Student() {
}
public Student(Integer id, String name, Integer age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
(2). 书写dao层
package com.zyl.dao;
import com.zyl.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentDao {
//全部查询
public List<Student> queryAll();
}
(3). mapper映射文件
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zyl.dao.StudentDao">
<select id="queryAll" resultType="student">
select * from student
</select>
</mapper>
(4). service接口
package com.zyl.service;
import com.zyl.pojo.Student;
import java.util.List;
public interface StudentService {
//全部查询
public List<Student> queryAll();
}
(5). service接口实现
package com.zyl.service;
import com.zyl.dao.StudentDao;
import com.zyl.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentDao studentDao;
@Override
public List<Student> queryAll() {
return studentDao.queryAll();
}
}
(6). controller
package com.zyl.controller;
import com.zyl.pojo.Student;
import com.zyl.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/queryAll")
public String queryAll(Model model){
List<Student> list = studentService.queryAll();
model.addAttribute("list",list);
return "showAll";
}
}
(7). 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>
<body>
<center>
<h3>学生信息展示页面</h3>
<table border="1" width="600px">
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>学生性别</td>
<td>学生年龄</td>
<td>学生操作</td>
</tr>
<c:forEach items="${list}" var="a">
<tr>
<td>${a.id}</td>
<td>${a.name}</td>
<td>${a.gender}</td>
<td>${a.age}</td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
3. 根据ID删除
(1). dao接口
//根据id删除学生信息
public void deleteById(Integer id);
(2). mapper映射文件
<delete id="deleteById" parameterType="Integer">
delete from student where id=#{id}
</delete>
(3). service接口
//根据id删除学生信息
public void deleteById(Integer id);
(4). service接口实现
@Override
public void deleteById(Integer id) {
try{
studentDao.deleteById(id);
}catch (Exception e){
throw new RuntimeException("根据id删除有异常");
}
}
(5). controller
@RequestMapping("/deleteById")
public String deleteById(Integer id){
try{
studentService.deleteById(id);
return "redirect:/student/queryAll";
}catch (Exception e){
return "error";
}
}
(6). jsp代码
<td>
<a href="${pageContext.request.contextPath}/student/deleteById?id=${a.id}">删除</a>
</td>
4、添加学生信息
(1). dao层
//添加学生信息
public void insertStudent(Student student);
(2). mapper映射文件
<insert id="insertStudent" parameterType="student">
INSERT INTO student(name,age,gender) values(#{name},#{age},#{gender})
</insert>
(3). service接口
//添加学生信息
public void insertStudent(Student student);
(4).service接口实现
@Override
public void insertStudent(Student student) {
try{
studentDao.insertStudent(student);
}catch (Exception e){
throw new RuntimeException("添加有异常");
}
}
(5). controller
@RequestMapping("/add")
public String add(){
return "add";
}
@RequestMapping("/addStudent")
public String addStudent(Student student){
try{
studentService.insertStudent(student);
return "redirect:/student/queryAll";
}catch (Exception e){
return "error";
}
}
(6). jsp代码
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2022/5/15
Time: 23:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加</title>
</head>
<body>
<h3>学生信息添加</h3>
<form action="${pageContext.request.contextPath}/student/addStudent">
学生姓名:<input type="text" name="name">
学生年龄:<input type="text" name="age">
学生性别:<input type="radio" name="gender" value="男" checked="checked">男<br>
<input type="radio" name="gender" value="女">女
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
在showAll.jsp加上(其实此处不要写成这样 controller中add可以去掉 这里直接href跳转到add页面)
<a href="${pageContext.request.contextPath}/student/add">添加</a>
5、修改学生信息
(1). 思路
(2). 数据回显
A. dao接口
//根据ID查询学生信息
public Student selectById(Integer id);
B.mapper映射文件
<!--根据ID查询-->
<select id="selectById" parameterType="Integer" resultType="student">
select * from student where id=#{id}
</select>
C.service接口
//根据ID查询学生信息
public Student queryById(Integer id);
D.service接口实现
@Override
public Student queryById(Integer id) {
return studentDao.selectById(id);
}
E.controller
//根据ID查询学生信息
@RequestMapping("/queryById")
public String queryById(Integer id, ModelMap mm){
Student student = studentService.queryById(id);
mm.addAttribute("student",student);
//跳转到修改页面,进行数据回显
return "update";
}
F.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>
</head>
<body>
<center>
<h3>学生信息修改页面</h3>
<form action="${pageContext.request.contextPath}/student/changeStudent">
<input type="hidden" name="id" value="${student.id}">
学生姓名:<input type="text" name="name" value="${student.name}"><br>
学生年龄:<input type="text" name="age" value="${student.age}"><br>
学生性别:<input type="radio" name="gender" value="男"
<c:if test="${student.gender=='男'}">checked="checked"</c:if>
>男
<input type="radio" name="gender" value="女"
<c:if test="${student.gender=='女'}">checked="checked"
</c:if>
>女
<br>
<input type="submit" value="修改">
</form>
</center>
</body>
</html>
(3).数据修改
A. dao层
//修改学生
public void updateStudent(Student student);
B. mapper映射文件
<update id="updateStudent" parameterType="student">
update student set name=#{name},age=#{age},gender=#{gender} where id=#{id}
</update>
C. service接口
//修改学生
public void updateStudent(Student student);
D. service接口实现
@Override
public void updateStudent(Student student) {
try{
studentDao.updateStudent(student);
}catch (Exception e){
throw new RuntimeException("修改数据异常");
}
}
E. controller
//修改数据
@RequestMapping("/changeStudent")
public String changeStudent(Student student){
try{
studentService.updateStudent(student);
return "redirect:/student/queryAll";
}catch (Exception e){
return "error";
}
}
F. jsp页面同回显页面
注意事项:
在多模块开发下可能会碰到页面404的情况 打开 Edit Configrations 修改下面箭头标记处 这样表示当前目录
文章存在一些不足之处,望包含!有更多建议可评论私信。
版权归原作者 yulick 所有, 如有侵权,请联系我们删除。