springboot整合mybatis

  1. 导依赖
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

  1. 修改配置文件
#properties.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/teaching?serverTimezone=UTC
    username: root
    password: "001013"  #0开头的密码需要加双引号
    driver-class-name: com.mysql.cj.jdbc.Driver

#.xml的全局配置,注解开放不用
#mybatis:
 # mapper-locations: classpath:mapper/*.xml   #配置Mybatis的配置文件路径
 # type-aliases-package: com.example.demo2022418.test425   #配置xml映射文件中指定的实体类别名路径

  1. 实体类
package com.example.demo2022418;

public class Article {
    public Integer id;
    public String title;
    public String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


}

  1. mapper映射类
package com.example.demo2022418;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface ArticleMapper {

    //查询数据库操作
    @Select("SELECT * FROM article WHERE id =#{id}")
    public Article findById(Integer id);

    //插入
    @Insert("INSERT INTO article(id,title,content) values (#{id},#{title},#{content})")
    public int insertArticle(Article article);

    //更新
    @Update("UPDATE article SET content=#{content} WHERE id =#{id}")
    public int updateArticle(Article article);

    //删除
    @Delete("DELETE FROM article WHERE id=#{id}")
    public int deleteArticle(Integer id);
}

  1. 测试
package com.example.demo2022418;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Demo2022418ApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    ArticleMapper articleMapper;

    @Test
    public void Mytest(){

        //查询
        Article article = articleMapper.findById(1);
        System.out.println(article.getTitle());

        //增加
        Article article1 = new Article();
        article1.setId(2);
        article1.setTitle("2");
        article1.setContent("2");
        int i = articleMapper.insertArticle(article1);

        //更改
        Article article2 = new Article();
        article2.setId(1);
        article2.setContent("hello");
        int i1 = articleMapper.updateArticle(article2);

        //删除
        int i2 = articleMapper.deleteArticle(2);
    }

}

小节:

  1. 导依赖
  2. 修改配置文件
  3. 实体类
  4. 映射类
  5. 测试
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值