SpringBoot整合Mybatis

一、导入依赖

导入整合的依赖:mybatis-spring-boot-starter
https://github.com/mybatis

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

二、创建mapper.xml文件

将mapper.xml文件建在resources下的mapper目录下
在这里插入图片描述
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">

三、在application.yml中配置mybatis的相关配置

mybatis:
  mapper-locations: classpath:mapper/*.xml	# mapper.xml的路径
  configuration:
    map-underscore-to-camel-case: true	# 开启驼峰功能

四、测试

controller层

@RestController
public class lndexController {

    private final MyDao myDao;

    public lndexController(MyDao myDao) {
        this.myDao = myDao;
    }

    @GetMapping("/{id}")
    public String One(@PathVariable String id){

        Blog blog = myDao.selectById(id);
        return blog.toString();
    }

    @GetMapping("/insert/{title}/{author}/{views}")
    public String Two(@PathVariable String title,@PathVariable String author,@PathVariable int views){

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        Blog blog = new Blog(title,author,format,views);
        myDao.insert(blog);
        return blog.toString();

    }
}

dao层

@Mapper
@Component
public interface MyDao {

	//使用mapper.xml文件方式
    Blog selectById(String id);

	//使用注解方式
    @Insert("insert into blog(title,author,create_time,views) values(#{title},#{author},#{createTime},#{views})")
    @Options(useGeneratedKeys = true,keyProperty = "id") //返回数据库自动生成的主键
    int insert(Blog blog);

}

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.springboot.thymeleaf.dao.MyDao">

    <select id="selectById" resultType="com.springboot.thymeleaf.pojo.Blog">
        select * from blog where id=#{id}
    </select>

</mapper>

映射类

@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Blog {

    private int id;
    private String title;
    private String author;
    private String createTime;
    private int views;

    public Blog(String title, String author, String createTime, int views) {
        this.title = title;
        this.author = author;
        this.createTime = createTime;
        this.views = views;
    }
}

效果展示

第二个方法:插入数据
在这里插入图片描述
第一个方法:通过id查询数据
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值