其他分享
首页 > 其他分享> > SpringBoot学习笔记(十一)——SpringBoot整合Mybatis

SpringBoot学习笔记(十一)——SpringBoot整合Mybatis

作者:互联网

这里就不介绍Mybatis如何使用了,在之前SSM整合的时候已经介绍过了,这里主要是整合。

在pom.xml中增加如下依赖:

<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> </dependency>

在application.properties中添加数据源配置

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/example
spring.datasource.username=root
spring.datasource.password=root

同时在application.properties中添加整合Mybaties相关属性

#给pojo实体类起别名,在mapper.xml文件中就不需要再写全类名了
mybatis.type-aliases-package=com.example.bean 
#接口的配置文件的位置 我这里接口配置文件是UserMapper.xml 如下图所示
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

然后我们在bean包下创建Books类,代码如下:

public class Books {

    private int bookId;
    private String bookName;
    private int bookCount;
    private String detail;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

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

    public int getBookCount() {
        return bookCount;
    }

    public void setBookCount(int bookCount) {
        this.bookCount = bookCount;
    }

    public String getDetail() {
        return detail;
    }

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

在Dao文件夹下创建BookMapper接口,代码如下:


@Mapper
@Component
public interface BookMapper {

    int addBook(Books book);

    List<Books> getAllBooks();

    Books getBookById(int bookId);

    int deleteBook(int bookId);

    int updateBook(Books book);

}

在我们之前配置的路径下Resources/mybatis/mapper/路径下创建BookMapper.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">

<!--  这里的namespace是BookMapper接口所在路径-->
<mapper namespace="com.example.dao.BookMapper">
    <!-- 这里Books不用写路径时因为设置了别名 -->
    <!-- 这个方法的返回值为int,是基本类型所以不用resultType参数,引用类型需要用-->
    <insert id="addBook" parameterType="Books">
        insert into example.books(bookName,bookCounts,detail)
            values (#{bookName},#{bookCounts},#{detai});
    </insert>

    <delete id="deleteBookById" parameterType="Integer">
        delete from example.books where bookId = #{id};
    </delete>

    <update id="updateBook" parameterType="Books">
        update example.books
           set
               <if test="bookName != null">
                   bookName = #{bookName}
               </if>
               <if test="bookName != null">
                   ,bookCounts = #{bookCounts}
               </if>
               <if test="bookName != null">
                   ,detail = #{detail}
               </if>
         where bookId = #{bookId};
    </update>

    <select id="queryBookById" parameterType="int" resultType="Books">
        select * from example.books where bookId = #{id};
    </select>

    <select id="queryAllBook" resultType="Books">
        select * from example.books;
    </select>

</mapper>

这里贴一下各个类的位置

 

 在Service中增加如下方法:

@Autowired
private BookMapper bookMapper;


public List<Books> getAllBooks() {
    return bookMapper.getAllBooks();
}

在controller中增加如下方法:

@RequestMapping("getAllBooks")
List<Books> getAllBooks() {
     return userService.getAllBooks();
}

在浏览器中访问,代码如下:

 

标签:SpringBoot,bookId,int,bookName,detail,笔记,example,Mybatis,public
来源: https://www.cnblogs.com/worthmove/p/16700673.html