前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot整合Mybatis增删改查

SpringBoot整合Mybatis增删改查

作者头像
乐心湖
发布2020-07-31 16:58:05
1.6K0
发布2020-07-31 16:58:05
举报
文章被收录于专栏:MyTechnologyMyTechnology

本文仅仅展示简单的整合和使用,存在不规范等诸多问题。 真正开发中也离不开动态SQL Mybatis 动态 SQL

1.使用IDEA新建Spring Boot项目

勾选Lombok,Web,Mybatis,Mysql依赖.(这一步也可以先不勾选,到项目创建完成后前往pom.xml中添加依赖.参考步骤二

2.没有勾选的话就在pom.xml中加入依赖.

代码语言:javascript
复制
    <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.创建数据库和数据表

代码语言:javascript
复制
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中,写实体类

代码语言:javascript
复制
@Data
public class Student {
    private int id;
    private String name;
    private int sex;
    private Date birthday;
}

5.mapper层

在mapper层中写StudentMapper接口

代码语言:javascript
复制
@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

代码语言:javascript
复制
<?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")

代码语言:javascript
复制
@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

填写数据库信息

代码语言:javascript
复制
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

代码语言:javascript
复制
//这里你接口中都有mapper注解的话,可以不写,两者功能相同。
@MapperScan("com.example.mapper")  //mapper包的路径

10.运行并测试

这里我使用的是ApiPost, 百度就可以下载得到。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.使用IDEA新建Spring Boot项目
  • 2.没有勾选的话就在pom.xml中加入依赖.
  • 3.创建数据库和数据表
  • 4.entity层
  • 5.mapper层
  • 6.Mapper.xml
  • 7.controller层
  • 8.application.yml
  • 9.在启动类中添加扫描mapper
  • 10.运行并测试
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档