项目整体目录结构
项目创建请查看https://blog.youkuaiyun.com/Tom_kobe/article/details/109741437
pom.xml配置
<?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.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.edu</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot</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-web</artifactId>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- druid数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- thymeleaf -->
<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.properties和application.yml,两者都属于配置文件根据的自己的喜好选择相应的格式
application.properties
#上下文配置
server.port=8080
server.servlet.context-path=/springboot
#MySQL Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/students?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#JPA Configuration:
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
#Mybatis
mybatis.mapper-locations= classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.edu.springboot.bean
mybatis.configuration.map-underscore-to-camel-case=true
application.yml
server:
port: 8080
servlet:
context-path: /springboot
---
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
jpa:
database: mysql
show-sql: true
#Hibernate ddl auto (validate|create|create-drop|update)
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
---
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.edu.springboot.bean
configuration.map-underscore-to-camel-case: true
数据库结构
创建Students实体bean
package com.edu.springboot.bean;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity //Entity注解表明该类为实体类
@Table(name = "students") //对应数据库的表名
public class Students {
@Id //数据库id
private int id;
private String name;
private String address;
private int age;
@DateTimeFormat(pattern = "yyyy-MM-dd") //转换时间格式
private String birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
创建StudentMapper接口
此项目中注解和映射都有用到
package com.edu.springboot.mapper;
import com.edu.springboot.bean.Students;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentMapper {
@Select("select * from students")
List<Students> getAll();
@Select("select * from students where id = #{id}")
Students getById(int id);
@Delete("delete from students where id = #{id}")
int delete(int id);
int update(Students students);
// @Insert("insert into students(name,address,age) value (#{name},#{address},#{age})")
int insert(Students students);
}
StudentMapper.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.edu.springboot.mapper.StudentMapper">
<update id="update" parameterType="com.edu.springboot.bean.Students">
update students
<set>
<if test="name != null">
name = #{name},
</if>
<if test="address != null">
address = #{address},
</if>
<if test="age != null">
age = #{age},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
</set>
where id = #{id}
</update>
<insert id="insert" parameterType="com.edu.springboot.bean.Students">
insert into students
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name,
</if>
<if test="address != null">
address,
</if>
<if test="age != null">
age,
</if>
<if test="birthday != null">
birthday,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name},
</if>
<if test="address != null">
#{address},
</if>
<if test="age != null">
#{age},
</if>
<if test="birthday != null">
#{birthday},
</if>
</trim>
</insert>
</mapper>
创建StudentService接口
package com.edu.springboot.service;
import com.edu.springboot.bean.Students;
import java.util.List;
public interface StudentService {
List<Students> getAll();
Students getById(int id);
int update(Students students);
int delete(int id);
int insert(Students students);
}
创建StudentServiceImpl实现类
package com.edu.springboot.service.impl;
import com.edu.springboot.bean.Students;
import com.edu.springboot.mapper.StudentMapper;
import com.edu.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Students> getAll() {
return studentMapper.getAll();
}
@Override
public Students getById(int id) {
return studentMapper.getById(id);
}
@Override
public int update(Students students) {
int i = studentMapper.update(students);
return i;
}
@Override
public int insert(Students students) {
int i = studentMapper.insert(students);
return i;
}
@Override
public int delete(int id) {
int i = studentMapper.delete(id);
return i;
}
}
创建StudentController控制器
package com.edu.springboot.controller;
import com.edu.springboot.bean.Students;
import com.edu.springboot.service.StudentService;
import com.edu.springboot.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController //RestController是@RequestBody和@Controller组合在一起的
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/list")
private List<Students> list(){
List<Students> students = studentService.getAll();
return students;
}
@RequestMapping("/edit")
private Students getById(int id){
return studentService.getById(id);
}
@RequestMapping(value = "update" ,method = RequestMethod.POST)
private Message update(Students students){
int i = studentService.update(students);
if (i == 1){
return Message.success();
}else {
return Message.fail();
}
}
@RequestMapping(value = "insert" ,method = RequestMethod.POST)
private Message insert(Students students){
int i = studentService.insert(students);
if (i == 1){
return Message.success();
}else {
return Message.fail();
}
}
@RequestMapping("/del")
private Message del(int id){
int i = studentService.delete(id);
if (i == 1){
return Message.success();
}else {
return Message.fail();
}
}
}
创建Message类
因为本项目没有写前端显示页面,所以就自己封装了一个Message类,根据返回的状态码观察操作是否成功。
package com.edu.springboot.util;
import java.util.HashMap;
import java.util.Map;
public class Message {
private Integer code;//响应状态码,约定200成功,失败500
private String msg;//代表响应信息
private Map<String, Object> info=new HashMap<>();
public static Message success() {
Message message=new Message();
message.setCode(200);
message.setMsg("成功");
return message;
}
public static Message fail() {
Message message=new Message();
message.setCode(500);
message.setMsg("失败");
return message;
}
public Message addInfo(String key,Object value) {
getInfo().put(key, value);
return this;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Map<String, Object> getInfo() {
return info;
}
public void setInfo(Map<String, Object> info) {
this.info = info;
}
}
查询所有的运行结果
条件查询(此项目根据ID查询)
删除的运行成功结果
删除的运行失败结果
新增运行结果
修改运行失败结果
修改运行成功结果