Spring Boot练习

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 负责页面访问控制

  1. 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;
    }
}


  1. 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值