SpringBoot整合Mybatis增删改查
时间:2022-07-22
本文章向大家介绍SpringBoot整合Mybatis增删改查,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
本文仅仅展示简单的整合和使用,存在不规范等诸多问题。 真正开发中也离不开动态SQL Mybatis 动态 SQL
1.使用IDEA新建Spring Boot项目
勾选Lombok,Web,Mybatis,Mysql依赖.(这一步也可以先不勾选,到项目创建完成后前往pom.xml中添加依赖.参考步骤二
2.没有勾选的话就在pom.xml中加入依赖.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
3.创建数据库和数据表
CREATE TABLE student(
id INT PRIMARY KEY auto_increment,
name VARCHAR(10),
sex int,
birthday date
);
INSERT INTO student(name,sex,birthday) values
("小明",1,"2020-1-1 00:00:00"),
("小红",0,"2020-2-8 00:00:00"),
("小白",0,"2020-3-4 00:00:00");
4.entity层
在entity中,写实体类
@Data
public class Student {
private int id;
private String name;
private int sex;
private Date birthday;
}
5.mapper层
在mapper层中写StudentMapper接口
@mapper
public interface StudentMapper {
/**
* 增加一个新学生
* @param student
*/
void save(Student student);
/**
* 通过id删除一个学生
* @param id
*/
void delete(int id);
/**
* 根据id修改学生
* @param student
*/
void update(Student student);
/**
* 查询所有学生
* @return 学生
*/
List<Student> findAll();
/**
* 通过id查询学生
* @param id
* @return 学生
*/
Student findById(int id);
}
6.Mapper.xml
在resources/mapping中创建StudentMapper接口的Mapper.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.mapper.StudentMapper">
<select id="findAll" resultType="student">
select * from student
</select>
<select id="findById" resultType="student" >
select * from student where id = #{id}
</select>
<insert id="save" parameterType="student">
insert into student(name,sex,birthday) values (#{name},#{sex},#{birthday})
</insert>
<update id="update" parameterType="student">
update student set name=#{name},sex=#{sex},birthday=#{birthday} where id = #{id}
</update>
<delete id="delete">
delete from student where id = #{id}
</delete>
</mapper>
7.controller层
在controller层新建StudentHandler类
注意: 这里注入时bean会提示不能注入,可以在StudentMapper中添加@Mapper注解,或者在启动类中添加扫描包
eg: @MapperScan("com.example.mapper")
@RestController
public class StudentHandler {
@Autowired
private StudentMapper studentMapper;
@GetMapping("/findAll")
public List<Student> findAll(){
return studentMapper.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") int id){
return studentMapper.findById(id);
}
@PutMapping("/save")
public void save(@RequestBody Student student){
studentMapper.save(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentMapper.update(student);
}
@DeleteMapping("/delete/{id}")
public void delete(@PathVariable("id") int id){
studentMapper.delete(id);
}
}
8.application.yml
在resources下新建application.yml
填写数据库信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
mybatis:
type-aliases-package: com.example.entity
mapper-locations: classpath:/mapping/*.xml
9.在启动类中添加扫描mapper
//这里你接口中都有mapper注解的话,可以不写,两者功能相同。
@MapperScan("com.example.mapper") //mapper包的路径
10.运行并测试
这里我使用的是ApiPost, 百度就可以下载得到。
- Web Spider实战1——简单的爬虫实战(爬取"豆瓣读书评分9分以上榜单")
- 如何用R语言从网上读取多样格式数据
- C/C++——生成随机数
- PHP基础——PHP数组
- 使用shell抽取html数据之二(r2笔记75天)
- Python爬取链家网数据:新房楼盘价格分析
- 【编程基础】Java里面如何对字符串排序?
- 计算广告——广告定向实践
- 通过shell抓取html数据(r2笔记74天)
- 通过shell脚本分析足彩(r2笔记74天)
- 通过shell脚本得到数据字典的信息 (r2笔记72天)
- 机器学习算法实践——K-Means算法与图像分割
- 利用 Python、SciKit 和文本分类来构建客户行为描述模型
- 使用Python爬取社交网络数据分析
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释