目录
在之前使用了mybatis-plus做项目,感觉不是很方便,特别是涉及到多表查询,虽然MP不用写SQL语句,但是缺点就是不够灵活。做做一些小demo就差不多了。 真正项目里面使用最多的还是mybatis,根据自己的需要写SQL语句。中小型项目sql语句用注解可能会方便一点,大型的项目用xml会更好一些,所以我这里直接演示用的是xml形式
创建MySQL数据库和创建表的详细步骤(navicat)_mysql navicat新建数据库_云边的快乐猫的博客-CSDN博客
使用这个文章里面的方式二创建springboot项目
IDEA创建SpringBoot项目的两个方式详细步骤(2023)_云边的快乐猫的博客-CSDN博客
源码链接:https://pan.baidu.com/s/1TA9QvOG8rRzen6CROvpIeQ?pwd=jiu1
👆按照上面的方式创建springboot项目。这里面的东西都会有的

总体配置文件(仅供参考,可跳过)
- server:
- port: 80
- spring:
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/dndata?serverTimezone=GMT%2b8
- username: root
- password: 123456
-
- mybatis:
- mapper-locations: classpath:mapping/*.xml
- type-aliases-package: com.example.jiu.entity
pom.xml依赖文件(仅供参考,可跳过)
- "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.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.15version>
- <relativePath/>
- parent>
- <groupId>com.examplegroupId>
- <artifactId>JiuartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>Jiuname>
- <description>Jiudescription>
- <properties>
- <java.version>11java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>2.3.1version>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>com.mysqlgroupId>
- <artifactId>mysql-connector-jartifactId>
- <scope>runtimescope>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starter-testartifactId>
- <version>2.3.1version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.2.16version>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- exclude>
- excludes>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
1.新建一个entity包,里面再创建一个和数据库表名对应的类(驼峰命名)
类名:User
这个类是和数据库表里面的字段对应上的,用Date注解里面包含了实体属性常用的get、set构造函数这些,具体可以去自行了解一下。
- package com.example.jiu.entity;
- import lombok.Data;
- //使用@Data注解
- @Data
- public class User {
- private Integer id;
- private String username;
- private String password;
- }
2.新建一个mapper包,里面创建一个数据层的接口
接口名:UserMapper
这里面的方法都是自定义的,是最开始的数据层的方法语句,后续的命名调用都根据这里的来
- package com.example.jiu.mapper;
- import com.example.jiu.entity.User;
- import org.apache.ibatis.annotations.Mapper;
- import java.util.List;
-
- @Mapper
- public interface UserMapper {
- //自定义查询全部的方法
- List
findAll(); -
- //根据id查询
- User findById(Integer id);
-
- //自定义根据删除的方法
- int deleteUserById(Integer id);
-
- //自定义增加的方法
- int addUser(User user);
-
- //自定义修改的方法
- int updateUser(User user);
- }
3.在resources目录下新建一个mapping包,里面创建一个xml文件
这个xml映射文件:UserMapper.xml
mapper标签里面对应的路径是mapper包里面对应映射接口的位置
SQL语句标签里面的id对应mapper接口里面定义的方法名
如果是查询的语句用resultType,后面跟着实体属性类的位置
其他的增删改用parameterType,后面跟着的是代表要往这个SQL里面传入什么,一般只有删除是int类型,其他的也都是实体属性类位置
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="com.example.jiu.mapper.UserMapper">
-
-
- <select id="findAll" resultType="com.example.jiu.entity.User">
- select * from user;
- select>
-
- <select id="findById" resultType="com.example.jiu.entity.User">
- select * from user where id =#{id}
- select>
-
-
- <delete id="deleteUserById" parameterType="java.lang.Integer">
- delete from user where id = #{id}
- delete>
-
-
- <insert id="addUser" parameterType="com.example.jiu.entity.User">
- insert into user (username,password) values (#{username},#{password})
- insert>
-
-
- <update id="updateUser" parameterType="com.example.jiu.entity.User">
- update user set username = #{username},password = #{password} where id = #{id}
- update>
-
- mapper>
4.新建一个service包,这里是服务层,主要就是编写逻辑代码的,拿mapper层的数据来编写逻辑代码,然后再交给控制层去调用。
服务层接口:UserService
这个接口里面的方法和mapper层接口里面的方法一样,这个接口存在的意义是为了降低耦合度
- package com.example.jiu.service;
- import com.example.jiu.entity.User;
- import java.util.List;
-
- //这里写的和mapper接口里面的一样
- public interface UserService {
- List
findAll(); -
- User findById(Integer id);
- int deleteUserById(Integer id);
-
- int addUser(User user);
-
- int updateUser(User user);
- }
5.在service包下再建立一个Impl包(包名首字母大写的),里面写上服务类接口的实现类
服务层实现类名称:UserServiceImpl
这里是实现了服务层的接口,并把mapper数据层注入到里面,里面的方法都是用快捷键去实现的,return后面跟着的全局变量.方法
- package com.example.jiu.service.Impl;
-
- import com.example.jiu.entity.User;
- import com.example.jiu.mapper.UserMapper;
- import com.example.jiu.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.web.bind.annotation.RequestBody;
-
- import java.util.List;
-
- @Service
- public class UserServiceImpl implements UserService {
-
- //调用mapper的接口用做全局变量
- private final UserMapper userMapper;
-
- //代表当这个实现类被实例化就会自动找到mapper接口,并注入其中
- @Autowired
- public UserServiceImpl(UserMapper userMapper) {
- this.userMapper = userMapper;
- }
-
- //这里都是用快捷键去实现生成接口里面的方法的。不过return后面跟着的要自己写,全局变量.方法
- //查询全部
- @Override
- public List
findAll() { - return userMapper.findAll();
- }
-
- //根据id查询
- @Override
- public User findById(Integer id) {
- return userMapper.findById(id);
- }
-
- //根据id删除
- @Override
- public int deleteUserById(Integer id) {
- return userMapper.deleteUserById(id);
- }
- //增加
- @Override
- public int addUser(@RequestBody User user) {
- return userMapper.addUser(user);
- }
- //修改
- @Override
- public int updateUser(User user) {
- return userMapper.updateUser(user);
- }
- }
6.新建一个controller包,里面创建对应的控制类
控制层类名:UserController
这里面要自动注入服务层的实现类供下面的方法调用
方法名字可以自定义
- package com.example.jiu.controller;
- import com.example.jiu.entity.User;
- import com.example.jiu.service.Impl.UserServiceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
-
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @Autowired
- private UserServiceImpl userServiceImpl;
-
- //查询全部的方法,调用服务层
- @GetMapping
- public List
findAll(){ - return userServiceImpl.findAll();
- }
- //根据id查询
- @GetMapping("/{id}")
- public User findById(@PathVariable Integer id){
- return userServiceImpl.findById(id);
- }
- //根据id删除,调用服务层
- @DeleteMapping("/{id}")
- public int delete(@PathVariable Integer id){
- return userServiceImpl.deleteUserById(id);
- }
- //增加的方法,调用服务层
- @PostMapping
- public int add(@RequestBody User user){
- return userServiceImpl.addUser(user);
- }
-
- //修改的方法,调用服务层
- @PutMapping
- public int put(@RequestBody User user){
- return userServiceImpl.updateUser(user);
- }
-
- }
7.在yml配置文件里面加入这个,代表可以把xml的文件加载扫描到
第一个是代表扫描到xml文件的包路径里面
第二个是代表实体类的包路径
- mybatis:
- mapper-locations: classpath:mapping/*.xml
- type-aliases-package: com.example.jiu.entity
这里用的是postman测试
get请求。就可以直接网页上面运行
http://localhost:80/user
get请求。例如查询id为1的就这样可以查询
http://localhost:80/user/1
delete请求。例如删除id为8的那行数据,删除成功返回1
http://localhost:80/user/8

post请求。例如添加一个数据进去,由于数据库里面的id是设置为自增的,这里就不用添加id
了
http://localhost:80/user


put请求,往里面传入数据,用json格式,修改id为9的数据,修改成功返回1
http://localhost:80/user


链接:https://pan.baidu.com/s/18Cy1RluCx04_9TRi4nsiVg?pwd=jiux
提取码:jiux