spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴

项目启动截图

spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第1张图片

查询:http://localhost:9000/song/list

spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第2张图片

添加:http://localhost:9000/song/myAdd

spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第3张图片

修改:http://localhost:9000/song/update/%201

spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第4张图片

搜索:http://localhost:9000/song/search?username=李天

spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第5张图片
页面简单没有美化
spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第6张图片

删除:http://localhost:9000/song/delete/*

spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第7张图片

数据库

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(45) NOT NULL,
  `password` varchar(45) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

spring boot项目实战开始POM文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.song</groupId>
    <artifactId>demo-jpa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-jpa</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml配置

server:
  port: 9000
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root用户名
    password: *****密码
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    show-sql: true
    properties:
      hibernate:
        format_sql: true

UserDto

package com.song.demojpa.dto;

import lombok.Data;

/**
 * @author SongXianYang
 * @date 2020-08-03 17:47:13
 **/

@Data
public class UserDto {
    private Integer id;
    public String username;
}

User实体类

package com.song.demojpa.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @author SongXianYang
 * @date 2020-07-30 17:38:42
 **/
@Data
@Entity
@Table(name = "user")
public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String username;
    private String password;
}

UserMapper接口 extends JpaRepository

package com.song.demojpa.mapper;

import com.song.demojpa.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;


/**
 * @author SongXianYang
 * @date 2020-07-30 17:44:46
 **/
public interface UserMapper extends JpaRepository<Users, Integer> {
	//查找一个
    @Query(value = "select * from user where username=?",nativeQuery = true)
    Users search(String username);
}

UserService层

package com.song.demojpa.service;

import com.song.demojpa.entity.Users;

import java.util.List;

/**
 * @author SongXianYang
 * @date 2020-07-30 17:50:39
 **/
public interface UserService {

    //查询
    public List<Users> findAll();
    //查询一个
    public Users findId(Integer id);
    //删除
    void deleteId(Integer id);
    //添加
    void save(Users users);
    //更新
    void update(Users users);

	//搜索用户
    Users search(String username);
}

UserServiceImpl实现类

package com.song.demojpa.service.impl;

import com.song.demojpa.entity.Users;
import com.song.demojpa.mapper.UserMapper;
import com.song.demojpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author SongXianYang
 * @date 2020-07-30 17:57:42
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<Users> findAll() {
        return userMapper.findAll();
    }

    @Override
    public Users findId(Integer id) {
        Users users = userMapper.findById(id).get();
        return users;
    }

    @Override
    public void deleteId(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void save(Users users) {
        userMapper.save(users);
    }

    @Override
    public void update(Users users) {
        userMapper.save(users);
    }


	@Override
    public Users search(String username) {
        Users search = userMapper.search(username);
        return search;
    }
}

Controller层也是web层

package com.song.demojpa.web;

import com.song.demojpa.entity.Users;
import com.song.demojpa.mapper.UserMapper;
import com.song.demojpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

/**
 * @author SongXianYang
 * @date 2020-07-30 18:01:17
 **/
@Controller
@RequestMapping("song")

public class UserWEb {

    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;

    @RequestMapping("list")
    public String list(ModelMap modelMap) {
        List<Users> users = userService.findAll();
        modelMap.put("user", users);
        return "user/list";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        userService.deleteId(id);
        return "redirect:/song/list";
    }

    @RequestMapping("/myAdd")
    public String myAdd() {
        return "user/add";
    }

    @PostMapping("/add")
    public String add(Users users) {
        userService.save(users);
        return "redirect:/song/list";
    }

    @RequestMapping("update/{id}")
    public String update(@PathVariable("id") Integer id, ModelMap map) {
        Users user = userService.findId(id);
        map.put("user", user);
        return "user/update";
    }

    @PostMapping("search")
    public String search() {
        return null;
    }
    
    @GetMapping("search")
    public String search(UserDto userDto,ModelMap map) {
        String username;
        username = userDto.getUsername();
        Users search = userService.search(username);
        map.put("user", search);
        return "user/search";
    }
}

spring boot启动类

package com.song.demojpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoJpaApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoJpaApplication.class, args);
    }

}

static文件都是bootstrap文件,下载地址:

https://v3.bootcss.com/
看项目 截图

添加add页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add</title>
    <link rel="stylesheet" href="/css/bootstrap.css">
</head>
<body class="container">
<br>
<h1>添加用户</h1>
<br>
<form action="/song/add" class="form-horizontal" method="post">
    <div class="form-group">
        <label for="username" class="col-sm-1 control-label">用户名</label>
        <div class="col-sm-5">
            <input class="form-control" type="text" id="username" name="username">
        </div>
    </div>
    <div class="form-group">
        <label for="password" class="col-sm-1 control-label">密码</label>
        <div class="col-sm-5">
            <input class="form-control" type="password" id="password" name="password">
        </div>
    </div>
    <div class="form-group">
        <input class="btn btn-info" value="提交" type="submit">
        <a class="btn btn-success" href="/song/list">主页</a>
    </div>
</form>
</body>
</html>

查询List页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>查询所有</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}">
</head>
<body>
<H2 class="panel-heading">User列表</H2>
<H3><a href="/song/myAdd">添加用户</a></H3>
<div>
    <form action="/song/search" method="post">
        输入用户名:<input class="form-control" type="text" name="username"/>
        <input class="btn btn-info" type="submit" value="搜索"/>
    </form>
</div>
<div>
    <table class="table table-striped">
        <tr>
            <th>User_Id</th>
            <th>User_Name</th>
            <th>User_Password</th>
            <th>操作</th>
            <th>操作</th>

        </tr>
        <tr th:each="user:${user}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.username}"></td>
            <td th:text="${user.password}"></td>
            <td>
                <a th:href="${'/song/update/ '+user.id}"><button class="btn btn-info" >修改</button></a>
            </td>
            <td>
                <!--<a class="a" th:href="${'/song/delete/ '+user.id}" style="background-color: #d43f3a">删除</a>-->
                <BUTTON class="btn btn-danger"><a class="a" th:href="${'/song/delete/ '+user.id}">删除</a></BUTTON>
            </td>

        </tr>
    </table>
</div>
</body>
</html>

更新update页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>更新用户</title>
    <link rel="stylesheet" href="/css/bootstrap.css">
</head>
<body>
<H1>更新用户</H1>
<form action="/song/add" method="post" class="form-horizontal">
    <div class="form-group">
        <label for="id" class="col-sm-1 control-label">ID</label>
        <div class="col-sm-5">
            <input class="form-control" th:value="${user.id}" name="id" readonly="readonly" id="id"/>
        </div>
    </div>

    <div class="form-group">
        <label for="username" class="col-sm-1 control-label">用户名</label>
        <div class="col-sm-5">
            <input class="form-control" th:value="${user.username}" name="username" id="username"/>
        </div>
    </div>

    <div class="form-group">
        <label for="password" class="col-sm-1 control-label">密码</label>
        <div class="col-sm-5">
            <input class="form-control" th:value="${user.password}" name="password" id="password"/>
        </div>
    </div>

    <div class="form-group">
        <input class="btn btn-info" value="提交" type="submit">
        <a class="btn btn-success" href="/song/list">主页</a>
    </div>
</form>
</body>
</html>

搜索:search.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>搜索</title>
</head>
<body>
<div>
    <h1>您搜索的用户为:</h1>
</div>
<div>
    <table class="table table-striped">
        <tr>
            <th>User_Id</th>
            <th>User_Name</th>
            <th>User_Password</th>
        </tr>
        <tr th:each="user:${user}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.username}"></td>
            <td th:text="${user.password}"></td>
        </tr>
    </table>
</div>
</body>
</html>

在写后端搜索时候 可以选择模糊搜索Like 修改为:List泛型Users。我写了一个dto,欢迎借鉴。也可以不用dto
截图:
spring boot+JPA+bootstrap+thymeleaf+MySQL整合集成,数据的增删改查+搜索。适合初学者借鉴_第8张图片

具体功能就:增删改查+搜索。小伙伴们可以添加其他功能哦
不合适的地方,望大牛指正。欢迎交流学习
加油找Java工作

你可能感兴趣的:(小项目,spring,boot,jpa,mysql,bootstrap,html5)