网站修改了关键词被降权,个人网站 摄影展示,济南j建设网,网站规划与设计就业目录
一、前言
二、创建项目
创建MySQL数据库和表
创建springboot项目
本文总体代码结构图预览
三、编写代码 #xff08;一#xff09;新建实体层属性类
#xff08;二#xff09;新建数据层mapper接口
#xff08;三#xff09;新建mapper的映射SQL#xff08…目录
一、前言
二、创建项目
创建MySQL数据库和表
创建springboot项目
本文总体代码结构图预览
三、编写代码 一新建实体层属性类
二新建数据层mapper接口
三新建mapper的映射SQLxml
四新建服务层接口
五新建服务层的实现类 六控制层类
七配置文件加入扫描xml文件
四、运行代码 查询全部
根据ID查询
根据ID删除 新增
修改
五、代码获取 一、前言
在之前使用了mybatis-plus做项目感觉不是很方便特别是涉及到多表查询虽然MP不用写SQL语句但是缺点就是不够灵活。做做一些小demo就差不多了。 真正项目里面使用最多的还是mybatis根据自己的需要写SQL语句。中小型项目sql语句用注解可能会方便一点大型的项目用xml会更好一些所以我这里直接演示用的是xml形式
二、创建项目
创建MySQL数据库和表 创建MySQL数据库和创建表的详细步骤navicat_mysql navicat新建数据库_云边的快乐猫的博客-CSDN博客 创建springboot项目 使用这个文章里面的方式二创建springboot项目 IDEA创建SpringBoot项目的两个方式详细步骤2023_云边的快乐猫的博客-CSDN博客 源码链接https://pan.baidu.com/s/1TA9QvOG8rRzen6CROvpIeQ?pwdjiu1 本文总体代码结构图预览
按照上面的方式创建springboot项目。这里面的东西都会有的 总体配置文件仅供参考可跳过
server:port: 80
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/dndata?serverTimezoneGMT%2b8username: rootpassword: 123456mybatis:mapper-locations: classpath:mapping/*.xmltype-aliases-package: com.example.jiu.entity
pom.xml依赖文件仅供参考可跳过
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.15/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdJiu/artifactIdversion0.0.1-SNAPSHOT/versionnameJiu/namedescriptionJiu/descriptionpropertiesjava.version11/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.3.1/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter-test/artifactIdversion2.3.1/versionscopetest/scope/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.2.16/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/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;
}
二新建数据层mapper接口
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 {//自定义查询全部的方法ListUser findAll();//根据id查询User findById(Integer id);//自定义根据删除的方法int deleteUserById(Integer id);//自定义增加的方法int addUser(User user);//自定义修改的方法int updateUser(User user);
}三新建mapper的映射SQLxml
3.在resources目录下新建一个mapping包里面创建一个xml文件 这个xml映射文件UserMapper.xml mapper标签里面对应的路径是mapper包里面对应映射接口的位置 SQL语句标签里面的id对应mapper接口里面定义的方法名 如果是查询的语句用resultType后面跟着实体属性类的位置 其他的增删改用parameterType后面跟着的是代表要往这个SQL里面传入什么一般只有删除是int类型其他的也都是实体属性类位置 ?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttps://mybatis.org/dtd/mybatis-3-mapper.dtd!--这里面是映射mapper接口里面的在这里编写sql语句--
mapper namespacecom.example.jiu.mapper.UserMapper!--namespace对应mapper接口的全部路径--!--映射查询方法--select idfindAll resultTypecom.example.jiu.entity.User!--参数一接口对应的方法名 参数二封装类的详细地址--select * from user;!--要执行的sql语句--/selectselect idfindById resultTypecom.example.jiu.entity.Userselect * from user where id #{id}/select!--映射删除的方法--delete iddeleteUserById parameterTypejava.lang.Integerdelete from user where id #{id}/delete!-- 这里映射增加的方法 --insert idaddUser parameterTypecom.example.jiu.entity.Userinsert into user (username,password) values (#{username},#{password})/insert!-- 这里定义修改的方法 --update idupdateUser parameterTypecom.example.jiu.entity.Userupdate 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 {ListUser 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接口并注入其中Autowiredpublic UserServiceImpl(UserMapper userMapper) {this.userMapper userMapper;}//这里都是用快捷键去实现生成接口里面的方法的。不过return后面跟着的要自己写全局变量.方法//查询全部Overridepublic ListUser findAll() {return userMapper.findAll();}//根据id查询Overridepublic User findById(Integer id) {return userMapper.findById(id);}//根据id删除Overridepublic int deleteUserById(Integer id) {return userMapper.deleteUserById(id);}//增加Overridepublic int addUser(RequestBody User user) {return userMapper.addUser(user);}//修改Overridepublic 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 {Autowiredprivate UserServiceImpl userServiceImpl;//查询全部的方法调用服务层GetMappingpublic ListUser 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);}//增加的方法调用服务层PostMappingpublic int add(RequestBody User user){return userServiceImpl.addUser(user);}//修改的方法调用服务层PutMappingpublic int put(RequestBody User user){return userServiceImpl.updateUser(user);}}七配置文件加入扫描xml文件
7.在yml配置文件里面加入这个代表可以把xml的文件加载扫描到 第一个是代表扫描到xml文件的包路径里面 第二个是代表实体类的包路径 mybatis:mapper-locations: classpath:mapping/*.xmltype-aliases-package: com.example.jiu.entity
四、运行代码
这里用的是postman测试 postman测试后端增删改查_云边的快乐猫的博客-CSDN博客 查询全部
get请求。就可以直接网页上面运行
http://localhost:80/user
根据ID查询
get请求。例如查询id为1的就这样可以查询
http://localhost:80/user/1
根据ID删除
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?pwdjiux 提取码jiux