Spring Boot 整合 MyBatis

一、创建数据和表

 

二、添加依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.9</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

三、数据库配置

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username  = root
spring.datasource.password  = kangxg198811

四、创建实体类

public class Book {
    private  Integer id;
    private String name;
    private String author;

    protected   Float price;

    private Date publicationDate;

    public Integer getId() {
        return id;
    }

    public Date getPublicationDate() {
        return publicationDate;
    }

    public Float getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAutor(String autor) {
        this.author = autor;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public void setPublicationDate(Date publicationDate) {
        this.publicationDate = publicationDate;
    }

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

五、创建数据库访问层

@Mapper
public interface BookMapper {
    int addBook(Book book);
    int deleteBookById(Integer id);
    int updateBookById(Book book);
    Book getBookById(Integer id);
    List<Book> getAllBooks();
}

@Mapper 去掉可在入口设置这样可以指定包下所有接口作为Mapper

@SpringBootApplication
@MapperScan("com.kxg.springboot.mapper")
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

}

指定多个@MapperScan({"com.kxg.demo","com.kxg.user"})  

六、创建映射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 表示命名空间  -->
<mapper namespace="com.kxg.springboot.mapper.BookMapper">
    <insert id="addBook" parameterType="com.kxg.springboot.po.Book">
        INSERT INTO t_book(name,author) VALUES(#{name},#{author})
    </insert>
    <delete id="deleteBookById" parameterType="Integer">
        DELETE from t_book where id=#{id}
    </delete>
    <update id="updateBookById" parameterType="com.kxg.springboot.po.Book">
        UPDATE t_book set name=#{name},author=#{author} where id=#{id}
    </update>
    <select id="getBookById" parameterType = "Integer" resultType="com.kxg.springboot.po.Book">
        select * from t_book where id =#{id}
    </select>
    <select id="getAllBooks" resultType="com.kxg.springboot.po.Book">
        select * from t_book
    </select>

</mapper>

七、创建Service和Controller

1.Service

@Service
public class BookService {
    @Autowired
    BookMapper bookMapper;

    public int addBook(Book book)
    {
        return bookMapper.addBook(book);
    }

    public int updateBook(Book book)
    {
        return bookMapper.updateBookById(book);
    }
    public int deleteBookById(Integer id)
    {
        return bookMapper.deleteBookById(id);
    }
    public Book getBookById(Integer id)
    {
        return bookMapper.getBookById(id);
    }

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

}

2.Controller

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping("/addBook")
    public int addBook(){
        Book book= new Book();
        book.setName("西厢记");
        book.setAutor("王实甫");
        return bookService.addBook(book);
    }

    @GetMapping("/updateBook")
    public  int updateBook(Integer id)
    {
        Book book= new Book();
        book.setName("朝花夕拾");
        book.setAutor("鲁迅");
        book.setId(2);
        return bookService.updateBook(book);
    }

    @GetMapping("/getBookById")
    public  Book getBookByid(Integer id)
    {
        return bookService.getBookById(id);
    }
    @GetMapping("/getAllBooks")
    public List<Book> getAllBooks()
    {
        return bookService.getAllBooks();
    }

    @GetMapping("/deleteBookById")
    public int deleteBookById(Integer id)
    {
        return bookService.deleteBookById(id);
    }

}

八、配置pom.xml 指定资源文件的位置

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resource</directory>
        </resource>
    </resources>
</build>

九、测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值