只是简单的写了一下后段的一些接口;
项目架构:
架构介绍:
<?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.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.DemoCrud</groupId>
<artifactId>democurd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>democurd</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</dependency>
<!-- mybatis整合Springboot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<!-- maven 添加json-->
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>
<!--jquery-webjar-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.0</version>
</dependency>
<!--bootstrap-webjar-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0</version>
</dependency>
<!--配置文件注入时使用后会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
/*
Navicat Premium Data Transfer
Source Server : deptuser
Source Server Type : MySQL
Source Server Version : 80020
Source Host : localhost:3306
Source Schema : cruddemo
Target Server Type : MySQL
Target Server Version : 80020
File Encoding : 65001
Date: 08/07/2022 23:11:12
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(0) NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`age` int(0) NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`newdate` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (11, '44', 77, '44', '2022-07-08 01:55:42');
INSERT INTO `user` VALUES (16, '88', 88, '8888', '2022-07-08 00:00:00');
INSERT INTO `user` VALUES (18, '44', 77, '44', '2022-07-08 02:12:43');
INSERT INTO `user` VALUES (21, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (22, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (23, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (24, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (36, 'tt', 44, '12345456', '2022-07-03 00:08:25');
SET FOREIGN_KEY_CHECKS = 1;
我这边暂时么写前段这个不写也行,我习惯的贴下
# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
spring:
datasource:
name:
url: jdbc:mysql://127.0.0.1:3306/cruddemo?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
username: root
password: root
messages:
##message.propertiesi是默认的国际化配置文件,不需要特别指定路径
basename: i18n.login
##禁用thymeleaf的缓存
thymeleaf:
cache: false
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.democru.pojo
server:
port: 6688
servlet:
context-path: /
package com.example.democrud.democurd;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(scanBasePackages = "com.example") //指定项目扫描不加也可以
@MapperScan(basePackages = "com.example.democrud.democurd.usermapper") //mapper 经常不自动加载故加了下
//@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //排除掉数据库自动加载
public class DemocurdApplication {
public static void main(String[] args) {
SpringApplication.run(DemocurdApplication.class, args);
}
}
package com.example.democrud.democurd.controller;
import com.example.democrud.democurd.pojo.userdemo;
import com.example.democrud.democurd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//@RestController
@Controller
@RequestMapping("/user/")
public class CrudController {
@Autowired
public UserService userService;
@RequestMapping("abc")
@ResponseBody
public String hello() {
return "hello 你好";
}
/**
* 查询 :模糊查询 单点查询 查询全部
* 删除
* 增加 数据
* <p>
*
*
* @param
*/
// public static void main(String[] args) {
//
// }
/**
* 查询全部
*http://localhost:6688/user/findall
* @return
*/
@ResponseBody
@GetMapping("findall")
public List<userdemo> findAll() {
System.out.println("执行controller");
List<userdemo> findall = userService.findall();
System.out.println(findall);
return findall;
}
/**
* 查询id
*http://localhost:6688/user/find/id
* @param id
* @return
*/
@ResponseBody
@GetMapping("findid/{id}")
public List<userdemo> findId(@PathVariable(value = "id") int id) {
System.out.println("id" + id);
List<userdemo> le = userService.findid(id);
return le;
}
/**
* http://localhost:6688/user/find/{like=》模糊查询的数据}
* 模糊查询 相关数据
* @param like
* @return
*/
@ResponseBody
@GetMapping("find/{like}")
public List<userdemo> findLike(@PathVariable String like) {
System.out.println("like" + like);
List<userdemo> likes = userService.findlike(like);
return likes;
}
/**
* http://localhost:6688/user/delectall
* 删除全部
* @return
*/
@ResponseBody
@GetMapping("delectall")
public String delectAll() {
System.out.println("删除全部");
int delect = userService.delectAll();
// 返回删除的条数 String.valueOf(delect)
if (delect > 0) {
String le = "删除成功,删除了" + delect + "条数据";
return le;
} else {
return "删除失败,或者无可删除数据";
}
}
/**
* http://localhost:6688/user/delect/id(写删除的id)
* 根据id删除
* @param id
* @return
*/
@ResponseBody
@GetMapping("delect/{id}")
public String delectId(@PathVariable int id) {
int le = userService.delectId(id);
if (le > 0) {
String lee = "删除成功删除了一条数据 id为" + id;
return lee;
} else {
return "删除失败";
}
}
/**
* http://localhost:6688/user/delectBatches
* 刪除多個 傳入多個id即可
* @return
*/
@ResponseBody
@GetMapping("delectBatches")
public String delectBatches() {
int le = 0;
int[] nee = {34, 35};
for (int i = 0; i < nee.length; i++) {
le = userService.delectId(nee[i]);
}
if (le > 0) {
String lee = "删除成功";
return lee;
} else {
return "删除失败";
}
}
/**
* http://localhost:6688/user/addOnce
*新增一个 或者批量新增
* @param userdemo
* @return
*/
@ResponseBody
@GetMapping("addOnce") // 一般采用put 请求但是这个demo不涉及前后交互
public int addOnce(userdemo userdemo) {
List<userdemo> userdemos = new ArrayList<>();
Date lee = new Date();
//新增一个或者多个
userdemos.add(new userdemo("21", "44", "77", "44", lee));
userdemos.add(new userdemo("22", "44", "77", "44", lee));
userdemos.add(new userdemo("23", "44", "77", "44", lee));
userdemos.add(new userdemo("24", "44", "77", "44", lee));
int lo = userService.addOnce(userdemos);
if (lo > 0) {
System.out.println("您新增了" + lo + "条数据");
} else {
System.out.println("无新增");
}
return lo;
}
/**
* http://localhost:6688/user/update 修改
* @param userdemo
* @return
*/
@ResponseBody
@GetMapping("update")
public int update(userdemo userdemo) {
//后端直接伪数据 进行赋值 发送新的数据
userdemo.setId("16");
userdemo.setName("88");
userdemo.setAge("88");
userdemo.setPassword("8888");
userdemo.setNewdate(new Date());
System.out.println(userdemo.toString());
int le=userService.updateid(userdemo);
if (le>0){
System.out.println("修改完成");
}
return le;
}
}
package com.example.democrud.democurd.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data //get/set方法
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
public class userdemo {
private String id;
private String name;
private String age;
private String password;
private Date newdate;
}
service:
package com.example.democrud.democurd.service;
import com.example.democrud.democurd.pojo.userdemo;
import java.util.List;
public interface UserService {
List<userdemo> findall();
List<userdemo> findid(int id);
List<userdemo> findlike(String like);
int delectAll();
int delectId(int id);
int addOnce(List<userdemo> userdemos);
int updateid(userdemo userdemo);
}
serviceimpl:
package com.example.democrud.democurd.service;
import com.example.democrud.democurd.pojo.userdemo;
import com.example.democrud.democurd.usermapper.DaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
//@Autowired 使用aut ...装备坏报错故推荐这个
// @Autowired
@Resource
private DaoMapper daoMappers;
/**
* @Autowired是一种注解,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,
* @Autowired标注可以放在成员变量上,也可以放在成员变量的set方法上,也可以放在任意方法上表示,
* 自动执行当前方法,如果方法有参数,会在IOC容器中自动寻找同类型参数为其传值。
*/
/**
* @Override这个标签有什么用呢。。。。。。
* 其实我也是比较迷茫,查了一下,三个作用吧
* 1. 可以给你当作注释用,感觉这个也不能说明什么,注释也没什么用。
* 2. 可以告诉读你代码的人,这是对它父类方法的重写,其实很多代码规范没有为什么,规范就是规范,
* 代码的可读性还是很重要的。
* 3. 编译器可以给你验证@Override下面的方法名称是否是你父类中所有的,如果没有就会报错。
*
* 比如当你想要在子类中重写父类的一个方法,但是你把名字打错了,当你写了@Override编译器会提示你,
* 你写的这个方法父类中没有;但是如果你没有写@Override编译器就会觉得这个是你子类中写的新的方法,
* 并不会报错,到时候你debug还是很麻烦的一件事。
*
* @return
*/
@Override
public List<userdemo> findall() {
return daoMappers.findall();
}
@Override
public List<userdemo> findid(int id) {
return daoMappers.findid(id);
}
@Override
public List<userdemo> findlike(String like) {
return daoMappers.findlike(like);
}
@Override
public int delectAll() {
return daoMappers.delectAll();
}
@Override
public int delectId(int id) {
return daoMappers.delectId(id);
}
@Override
public int addOnce(List<userdemo> userdemos) {
return daoMappers.addOnce(userdemos);
}
@Override
public int updateid(userdemo userdemo) {
return daoMappers.updateid(userdemo);
}
}
package com.example.democrud.democurd.usermapper;
import com.example.democrud.democurd.pojo.userdemo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface DaoMapper {
List<userdemo> findall();
List<userdemo> findid(int id);
List<userdemo> findlike(String like);
int delectAll();
int delectId(int id);
int addOnce(List<userdemo> userdemos);
int updateid(userdemo userdemo);
}
<?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">
<!--数据源文件(告知程序着是数据源从这里拿数据Repository)-->
<mapper namespace="com.example.democrud.democurd.usermapper.DaoMapper">
<resultMap id="userMap" type="com.example.democrud.democurd.pojo.userdemo">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="password" property="password"/>
<result column="newdate" property="newdate"/>
</resultMap>
<sql id="lee">
id,name,age,password,newdate
</sql>
<select id="findall" parameterType="com.example.democrud.democurd.pojo.userdemo" resultMap="userMap">
select
<include refid="lee"/> from user
</select>
<!-- parameterType 传入值的类型 决定-->
<select id="findid" parameterType="integer" resultMap="userMap">
select <include refid="lee"></include>
from user where id=#{id}
</select>
<select id="findlike" resultType="com.example.democrud.democurd.pojo.userdemo" parameterType="com.example.democrud.democurd.pojo.userdemo">
select <include refid="lee"></include>
from USER WHERE id LIKE CONCAT('%',#{LIKE},'%')
OR name LIKE CONCAT('%',#{LIKE},'%') OR age LIKE CONCAT('%',#{LIKE},'%')
</select>
<delete id="delectAll" parameterType="int">
delete from user
</delete>
<delete id="delectId" parameterType="int">
delete from user where id=#{id}
</delete>
<!--<insert id="addOnce" parameterType="com.example.democrud.democurd.pojo.userdemo">
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="age != null" >
age,
</if>
<if test="password != null" >
password,
</if>
<if test="newdate != null" >
newdate,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=VARCHAR},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
<if test="newdate != null" >
#{newdate,jdbcType=DATE},
</if>
</trim>
</insert>-->
<insert id="addOnce" parameterType="int">
insert into user (id,name,age,password,newdate)
values
<foreach collection="list" item="item" separator=",">
(
#{item.id}, #{item.name},#{item.age},#{item.password},#{item.newdate}
)
</foreach>
</insert>
<!-- <update id="updateid" parameterType="int">-->
<!-- update user set-->
<!-- name=#{name},age=#{age},password=#{password},newdate={newdate}-->
<!-- where id=#{id}-->
<!-- </update>-->
<update id="updateid" parameterType="int">
update user
<set>
<if test="name !=null">
name =#{name,jdbcType=VARCHAR},
</if>
<if test="age !=null">
age=#{age,jdbcType=VARCHAR},
</if>
<if test="password !=null">
password=#{password,jdbcType=VARCHAR},
</if>
<if test="newdate !=null">
newdate=#{newdate,jdbcType=DATE},
</if>
</set>
where id=#{id,jdbcType=VARCHAR}
</update>
</mapper>
gitee的地址: https://gitee.com/yan_wen_chao/springboot-daily-crud-record 的源码 有兴趣可自行查看;