application.properties
spring.datasource.url=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=
spring.datasource.password=
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
mybatis.type-aliases-package=pojo //可寫全名
BootMybatisApplication
package cn.lnfvc.bootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.lnfvc.bootmybatis.mapper")
public class BootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(BootMybatisApplication.class, args);
}
}
pojo/Book.java
package cn.lnfvc.bootmybatis;
public class Book {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
mapping/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">
<mapper namespace="cn.lnfvc.bootmybatis.mapper.BookMapper">
<select id="loadBook" resultType="cn.lnfvc.bootmybatis.Book">
select * from books where id = #{id}
</select>
<insert id="addBook" parameterType="cn.lnfvc.bootmybatis.Book" useGeneratedKeys="true" keyProperty="id">
insert into books (name) values (#{name})
</insert>
<select id="findAllBooks" resultType="cn.lnfvc.bootmybatis.Book">
select * from books
</select>
<update id="updateBook" keyProperty="id">
update books set name=#{book.name} where id=#{id}
</update>
<delete id="deleteBook">
delete from books where id=#{id}
</delete>
</mapper>
BookMapper.java
package cn.lnfvc.bootmybatis.mapper;
import cn.lnfvc.bootmybatis.Book;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookMapper {
Book loadBook(int id);
void addBook(Book book);
List<Book> findAllBooks();
void updateBook(@Param("id") int id, @Param("book") Book book);
void deleteBook(int id);
}
BookController.java
package cn.lnfvc.bootmybatis.controller;
import cn.lnfvc.bootmybatis.Book;
import cn.lnfvc.bootmybatis.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class BookController {
@Autowired
private BookMapper bookMapper;
@RequestMapping(value = "/books",method = RequestMethod.GET)
public List<Book> findAllBooks(){
return bookMapper.findAllBooks();
}
@RequestMapping(value = "/books/{id}",method = RequestMethod.GET)
public Book loadBook(@PathVariable int id){
return bookMapper.loadBook(id);
}
//添加一個數(shù)據(jù)
@RequestMapping(value = "/books",method = RequestMethod.POST)
public Book addBook(@RequestBody Book book){
bookMapper.addBook(book);
return book;
}
//更新數(shù)據(jù)(替換)
@RequestMapping(value = "/books/{id}",method = RequestMethod.PUT)
public Book updateBook(@PathVariable int id,@RequestBody Book book){
bookMapper.updateBook(id,book);
book.setId(id);
return book;
}
//刪除數(shù)據(jù)
@RequestMapping(value = "/books/{id}",method = RequestMethod.DELETE)
public String deleteBook(@PathVariable int id){
bookMapper.deleteBook(id);
return "{}";
}
}
booktest.http(API)
POST http://localhost:8080/books
Content-Type: application/json
{"name": "li"}
###
GET http://localhost:8080/books
Accept: application/json
###
GET http://localhost:8080/books/5
Accept: application/json
###
PUT http://localhost:8080/books/5
Content-Type: application/json
{
"name":"MikeLiu1"
}
###
DELETE http://localhost:8080/books/6
Accept: application/json
###