Spring Boot + Mybatis + JSP案例
Spring Boot的基础结构共三个文件:
- src/main/java 程序开发以及主程序入口
- src/main/resources 配置文件
- src/test/java 测试程序
springboot建议目录结构
root package结构:com.example.myproject
com
± example
± myproject
± Application.java
|
± domain
| ± Customer.java
| ± CustomerRepository.java
|
± service
| ± CustomerService.java
|
± controller
| ± CustomerController.java
|
Application.java 建议放到跟目录下面,主要用于做一些框架配置
domain目录主要用于实体(Entity)与数据访问层(Repository)
service 层主要是业务类代码
controller 负责页面访问控制
- pojo层
数据库实体层,封装实体类,一般数据库一张表对应一个实体类,类属性同表字段一一对应。
package com.test.studentmanager.pojo;
public class Student {
private Integer id ;
private Integer age;
private String name;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = 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;
}
}
- Dao层
访问实体于数据库,想数据库发送sql
写mapper.xml
<?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="com.test.studentmanager.dao.StudentDao"> //names = 接口的全名称,mybatis通过namespace将xml文件与mapper接口关联
<select id="selectAll" resultType="student"> //id:通过id查找 tesultType返回值类型,将方法添加到studentDao
Select * FROM student
</select>
//删除
<delete id="deleteById" parameterType="Integer">
DELETE FROM student WHERE id=#{id}
</delete>
//添加
<insert id="insertStudent" parameterType="student">
INSERT INTO student(name,age,gender) values(#{name},#{age},#{gender})
</insert>
</mapper>
StudentDao
package com.test.studentmanager.dao;
import com.test.studentmanager.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
//会根据Mapper中的代理,会根据接口生成接口的实现类
public interface StudentDao {
//全查 List集合返回Student
public List<Student> selectAll();
//删除根据id
public void deleteById(Integer id);
//添加
public void insertStudent(Student student);
}
Service
- StudentService
package com.test.studentmanager.service;
import com.test.studentmanager.pojo.Student;
import org.springframework.stereotype.Service;
import java.util.List;
public interface StudentService {
//全查学生信息
public List<Student> queryAll();
//删除
public void removeById(Integer id);
//添加
public void addStudent(Student student);
}
- StudentServiceImpl
package com.test.studentmanager.service;
import com.test.studentmanager.dao.StudentDao;
import com.test.studentmanager.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import java.util.List;
@Service //创建Studengservice的实现类,纳入工厂S
@Transactional //受控制注解
public class StudentServiceImpl implements StudentService{
@Autowired //studentservice 依赖dao
//以来类型注入
private StudentDao sd;
@Override
//全查学生信息
public List<Student> queryAll() {
return sd.selectAll();
}
@Override
public void removeById(Integer id){
try {
sd.deleteById(id);
}catch (Exception E){
throw new RuntimeException("删除异常");
}
}
@Override
public void addStudent(Student student){
try{
sd.insertStudent(student);
}catch(Exception e){
throw new RuntimeException("添加异常");
}
}
Controller
package com.test.studentmanager.controller;
import com.test.studentmanager.pojo.Student;
import com.test.studentmanager.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 java.util.List;
@Controller //controller里面的方法都以json格式输出
@RequestMapping("student")
public class StudentController {
//全查
@Autowired //依赖于 service
private StudentService ss;
//全查
@RequestMapping("queryAll")//请求路径queryAll
//Model和ModelMap 的实例都是spirng mvc框架来自动创建并作为控制器方法参数传入,无需自己创建。需要return 返回指定的页面路径.
public String queryAll(Model model){
List<Student> list =ss.queryAll();//studentservice的实现类去调用存入List集合
model.addAttribute("list",list) ; //封装查询的数据
//控制跳转
return "showAll";
}
//删除
@RequestMapping("removeById")
public String removeById(Integer id){
try {
ss.removeById(id);
return "redirect:/student/queryAll"; //重定向
}catch(Exception e){
return "error";
}
}
//删除
@RequestMapping("addStudent")
public String addStudent(Student student){
try{
ss.addStudent(student);
return "redirect:/student/queryAll";
}catch(Exception e){
e.printStackTrace(); //?
return "error";
}
}
}
showAll.jsp
<%--
Created by IntelliJ IDEA.
User: T
Date: 2021/9/2
Time: 14:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%--daoru 核心表达式--%>
<html>
<head>
<title>Title</title>
</head>
<body>
<center>
<h3>学生信息展示页面</h3>
<table border="1" width="600px">
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>学生性别</td>
<td>学生年龄</td>
</tr>
<%--获取值的时候,对应的是addAttribute的第一个参数!取了个别名为a-->
<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>
<a href="/add.jsp">添加</a>
</center>
</body>
</html>
add.jsp
<%--
Created by IntelliJ IDEA.
User: T
Date: 2021/9/3
Time: 19:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>信息添加</h3>
<form action="${pageContext.request.contextPath}/student/addStudent">
学生姓名:<input type="text" name="name"><br>
学生年龄:<input type="text" name="age"><br>
学生性别:<input type="radio" name="gender" value="男" checked="checked">男
<input type="radio" name="gender" value="女" >女
<br>
<input type="submit" value="提交">
</form>
</body>
</html>