springboot考试

文章目录


springboot考试_第1张图片
springboot考试_第2张图片
springboot考试_第3张图片
springboot考试_第4张图片

package net.shuai.boot.bean;

public class Book {
    private int id;
    private String bname;
    private float rmb;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getRmb() {
        return rmb;
    }

    public void setRmb(float rmb) {
        this.rmb = rmb;
    }
}

springboot考试_第5张图片

package net.shuai.boot.mapper;

import net.shuai.boot.bean.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {

    //查询
    @Select("select * from book")
    List<Book> findAll();

    //添加
    @Insert("insert into book (id, bname, rmb) values (#{id}, #{bname}, #{rmb})")
    int addBook(Book book);

    //删除
    @Delete("delete from book where id = #{id}")
    int deleteBook(int id);

    //修改
    @Update("update book set bname = #{bname}, rmb = #{rmb} where id = #{id}")
    int updateBook(Book book);

    @Select("select * from book where id = #{id}")
    Book findId(int id);
}

springboot考试_第6张图片

package net.shuai.boot.service;

import net.shuai.boot.bean.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface BookService {
    //查询
    List<Book> findAll();

    //添加
    int addBook(Book book);

    //删除
    int deleteBook(int id);

    //修改
    int updateBook(Book book);
}

springboot考试_第7张图片

package net.shuai.boot.service.impl;

import net.shuai.boot.bean.Book;
import net.shuai.boot.mapper.BookMapper;
import net.shuai.boot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@EnableTransactionManagement
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;


    @Override
    public List<Book> findAll() {
        return bookMapper.findAll();
    }

    @Override
    public int addBook(Book book) {
        return bookMapper.addBook(book);
    }

    @Override
    public int deleteBook(int id) {
        return bookMapper.deleteBook(id);
    }

    @Override
    public int updateBook(Book book) {
        return bookMapper.updateBook(book);
    }
}

springboot考试_第8张图片

package net.shuai.boot.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import net.shuai.boot.bean.Book;
import net.shuai.boot.mapper.BookMapper;
import net.shuai.boot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
//@RestController
public class WebController {
    @Autowired
    BookService bookService;
    @Autowired
    BookMapper bookMapper;

    @ResponseBody
    @GetMapping("/book")
    public List<Book> book() {
        return bookMapper.findAll();
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/register")
    public String register(@RequestParam("username") String username,
                           @RequestParam("password") String password, Model model) {
        model.addAttribute("username", username);
        model.addAttribute("password", password);
        Book book  = bookMapper.findId(4);
        model.addAttribute("bname", book.getBname());
        return "register";
    }
}

springboot考试_第9张图片

DOCTYPE html>
<html>
<head>
	<title>用户登录title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="/css/login.css">
head>
<body>
	<h1>请输入用户名和密码h1>
	<form method="post" action="/register">
		<label for="username">用户名:label>
		<input type="text" id="username" name="username" required>
		<label for="password">密 码:label>
		<input type="password" id="password" name="password" required>
		<button type="submit">登 录button>
	form>
body>
html>

springboot考试_第10张图片

body {
    background-color: #f2f2f2;
    font-family: Arial, sans-serif;
}

h1 {
    text-align: center;
    color: #333;
    margin-top: 50px;
}

form {
    padding: 20px;
    margin-top: 30px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0px 0px 10px #999;
    max-width: 500px;
    margin-left: auto;
    margin-right: auto;
}

label {
    display: block;
    font-weight: bold;
    margin-bottom: 10px;
}

input[type="text"],
input[type="password"] {
    width: 100%;
    padding: 10px;
    margin-bottom: 20px;
    border-radius: 5px;
    border: none;
    box-shadow: 0px 0px 5px #999;
    box-sizing: border-box;
    font-size: 16px;
}

button[type="submit"] {
    background-color: #333;
    color: #fff;
    padding: 10px;
    border-radius: 5px;
    border: none;
    font-size: 16px;
    cursor: pointer;
    width: 100%;
    margin-top: 20px;
    box-shadow: 0px 0px 5px #999;
    transition: background-color 0.3s ease-in-out;
}

button[type="submit"]:hover {
    background-color: #444;
}

springboot考试_第11张图片

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录成功title>
head>
<body>
<h1 th:text="'用户名:' + ${username}">h1>
<h1 th:text="'密 码:' + ${password}">h1>
<h1 th:text="${bname}">h1>
body>
html>

springboot考试_第12张图片

# ???
server.port=8080


# ?????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/data1
spring.datasource.username=root
spring.datasource.password=1234
#
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html

mybatis.type-aliases-package=net.shuai.boot.bean

spring.thymeleaf.cache = true
spring.thymeleaf.encoding = UTF-8
spring.thymeleaf.mode = HTML5
spring.thymeleaf.prefix = classpath:/templates/
spring.thymeleaf.suffix = .html

你可能感兴趣的:(spring,boot,java,mybatis)