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.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>boot</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</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>
controller.StudentController
package com.example.demo.controller;
import com.example.demo.pojo.Student;
import com.example.demo.pojo.Users;
import com.example.demo.service.StudentService;
import com.example.demo.service.UsersService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class StudentController {
@Resource
private StudentService studentService;
@Resource
private UsersService usersService;
@GetMapping("/students")
public ModelAndView getStudentList(Model model) {
List<Student> studentList = studentService.list();
model.addAttribute("list", studentList);//和代码中表名list对应,才能进行一系列操作
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("list");
modelAndView.addObject(studentList);
return modelAndView;
}
@GetMapping("/students/{id}")
@ResponseBody
public Student getStudentById(@PathVariable(value = "id") int id) {
return studentService.findById(id);
}
@GetMapping("/user/{id}")
@ResponseBody
public Users findUser(@PathVariable(value = "id") int id) {
return usersService.find(id);
}
}
mapper.StudentMapper
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
List<Student> getStudents();
Student getStudentById(int id);
}
mapper.UsersMapper
package com.example.demo.mapper;
import com.example.demo.pojo.Users;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author
* @description 针对表【users】的数据库操作Mapper
* @createDate
* @Entity com.example.demo.pojo.Users
*/
@Mapper
public interface UsersMapper extends BaseMapper<Users> {
Users find(Integer id);
}
pojo.Student
package com.example.demo.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
@TableName("users")
public class Student {
private Integer id;
private String name;
private String gender;
private Integer age;
}
pojo.Users
package com.example.demo.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
@TableName("users")
public class Student {
private Integer id;
private String name;
private String gender;
private Integer age;
}
service.impl.Studentimpl
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> findAll() {
return studentMapper.getStudents();
}
@Override
public Student findById(int id) {
return studentMapper.getStudentById(id);
}
}
service.impl.UsersServiceimpl
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.pojo.Users;
import com.example.demo.service.UsersService;
import com.example.demo.mapper.UsersMapper;
import org.springframework.stereotype.Service;
/**
* @author
* @description 针对表【users】的数据库操作Service实现
* @createDate 2
*/
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
implements UsersService {
@Override
public Users find(int id) {
return baseMapper.find(id);
}
}
service.StudentService
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.pojo.Student;
import java.util.List;
public interface StudentService extends IService<Student> {
List<Student> findAll();
Student findById(int id);
}
service.UsersService
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.pojo.Users;
/**
* @author
* @description 针对表【users】的数据库操作Service
* @createDate
*/
public interface UsersService extends IService<Users> {
Users find(int id);
}
DemoApplication
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
src/main/resources/mapper/StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.StudentMapper">
<select id="getStudents" resultType="com.example.demo.pojo.Student">
select * from users
</select>
<select id="getStudentById" resultType="com.example.demo.pojo.Student">
select * from users where id =#{id}
</select>
</mapper>
src/main/resources/mapper/UsersMapper.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.example.demo.mapper.UsersMapper">
<resultMap id="BaseResultMap" type="com.example.demo.pojo.Users">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="gender" column="gender" jdbcType="OTHER"/>
<result property="age" column="age" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id
,name,gender,
age
</sql>
<select id="find" resultType="com.example.demo.pojo.Users">
select *
from users
where id = #{id}
</select>
</mapper>
src/main/resources/templates/list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
<title>Title</title>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于SpringBoot的CRUD案例
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" th:href="@{/add}">新增</a>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<td>序号</td>
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>编辑</td>
<td>删除</td>
</thead>
<tr th:each="user,stat : ${list}">
<td th:text="${stat.count}">序号</td>
<td th:text="${user.id}">ID</td>
<td th:text="${user.name}">姓名</td>
<td th:text="${user.gender}">性别</td>
<td th:text="${user.age}">年龄</td>
<td><a th:href="@{/update/}+${user.id}">编辑</a></td>
<td><a th:href="@{/delete/}+${user.id}" onclick="return confirm('确定要删除该记录吗?')">删除</a></td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
src/main/resources/application.yaml 在idea中springboot项目连接数据库相关文件方法 打开数据连接目标数据库,右键数据库,点击第一项(需要下载插件mybatis=x)填入信息后点击next即可生成相关文件,具体信息可参考student的相关代码文件(上面已经列出来了)
相关代码结果
经验分享:
1、application.yaml文件里面url可能书写错误
2、pom文件里面版本冲突 springboot版本改为2.3.12.RELEASE 以及mysql-connector-java
<artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version>后,bug消失
3、mapper文件里的xml中对于resultType书写错误,应为“com.example.demo.pojo.Users”
在一般条件下,pojo里面的名字与mysql的名字有默认的对应关系,必要时需要书写相关的对应关系,例如以下关系
<resultMap id="BaseResultMap" type="com.example.demo.pojo.Users">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="gender" column="gender" jdbcType="OTHER"/>
<result property="age" column="age" jdbcType="INTEGER"/>
</resultMap>
4、书写spingboot的默认文件格式可能错误,与默认关系不服时需要添加必要的代码,详细的具体关系如下图所示
5、自动生成相关依赖和注解,在setting里输入auto import后点击,对java内的四个选项点击后对号及可完成
标签:实战,springboot,demo,id,org,import,com,example From: https://blog.csdn.net/m0_75182143/article/details/140932698