Springboot 整合Mybatis

Posted 门虫不是虫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot 整合Mybatis相关的知识,希望对你有一定的参考价值。

1、依赖

  导入包

 <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

2、配置数据库连接、整合mybatis

  application.yml 中配置,application.properties中一样;

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8
    username: root
    password: 123456

#整合mybatis
mybatis:
  type-aliases-package: com.doubleh.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

3、pojo、mapper编写

pojo / Book.java  实体类

public class Book {
    private int bookID;

    private String bookName;

    private int bookCounts;

    private String detail;

    @Override
    public String toString() {
        return "Book{" +
                "bookID=" + bookID +
                ", bookName=‘" + bookName + ‘‘‘ +
                ", bookCounts=" + bookCounts +
                ", detail=‘" + detail + ‘‘‘ +
                ‘}‘;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }
}

mapper / BookMaper 接口  定义DAO操作

//这个注解表示是一个mybatis的mapper类
@Mapper
//注入到spring容器中
@Repository
public interface BookMapper {
    List<Book> queryAll();

    Book queryBookByID(@Param("bookID") int id);

    int insertBook(Book book);

    int deleteBook(@Param("bookID") int id);

    int updateBook(Book book);
}

resouces / mybatis / mapper / BookMapper.xml  编写sql语句

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.doubleh.mapper.BookMapper">
   <select id="queryAll" resultType="Book">
       select * from ssmbuild.books;
   </select>

    <select id="queryBookByID" parameterType="_int" resultType="book">
        select * from ssmbuild.books where bookID = #{bookID};
    </select>

    <insert id="insertBook" parameterType="book">
        insert into ssmbuild.books(bookName, bookCounts, detail) values(#{bookName},#{bookCounts},#{detail});
    </insert>

    <delete id="deleteBook" parameterType="_int">
        delete from ssmbuild.books where bookID = #{bookID};
    </delete>

    <update id="updateBook" parameterType="book">
        update ssmbuild.books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
        where bookID = #{bookID};
    </update>

</mapper>

4、测试

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookMapper bookMapper;

    @GetMapping("/all")
    public String queryAll(){
        List<Book> books = bookMapper.queryAll();
        return books.toString();
    }

    @GetMapping("/{id}")
    public String queryBookByID(@PathVariable("id") int id){
        System.out.println(id);
        Book book = bookMapper.queryBookByID(id);
        return book.toString();
    }

}

 

以上是关于Springboot 整合Mybatis的主要内容,如果未能解决你的问题,请参考以下文章

springboot整合mybatis,redis,代码

SpringBoot系列Mybatis整合

SpringBoot系列Mybatis整合

springboot整合mybatis开发

18.springboot整合mybatis

SpringBoot:4.SpringBoot整合Mybatis实现数据库访问