Springboot 使用 Mybatis

Springboot 使用 Mybatis

依赖 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.draymonder</groupId>
	<artifactId>book</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>book</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.16</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.15</version>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

配置文件 application.yml


server:
  port: 80

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: xxxx

mybatis:
#  type-aliases-package: com.draymonder.book.mybatis
  mapper-locations: classpath:mappers/*.xml # 映射文件的路径扫描

book实体

package com.draymonder.book.mybatis;

/**
 * @auther draymonder
 */
public class Book {
  private Integer id;
  private String author;
  private String name;

  @Override
  public String toString() {
    return "Book{" +
        "id=" + id +
        ", author='" + author + '\'' +
        ", name='" + name + '\'' +
        '}';
  }

  public Integer getId() {
    return id;
  }

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

  public String getAuthor() {
    return author;
  }

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

  public String getName() {
    return name;
  }

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

bookMapper

package com.draymonder.book.mybatis;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @auther draymonder
 */
@Mapper
public interface BookMapper {

  int addBook(Book book);

  int deleteBookById(Integer id);

  int updateBookById(Book book);

  Book getBookById(Integer id);

  List<Book> getAllBooks();
}

bookMapper.xml

<?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.draymonder.book.mybatis.BookMapper">
    <insert id="addBook" parameterType="com.draymonder.book.mybatis.Book">
        insert into book(name, author) values(#{name}, #{author})
    </insert>
    <delete id="deleteBookById" parameterType="int">
        delete from book where id = #{id}
    </delete>
    <update id="updateBookById" parameterType="com.draymonder.book.mybatis.Book">
        update book set name=#{name}, author=#{author} where id=#{id}
    </update>
    <select id="getBookById" parameterType="int" resultType="com.draymonder.book.mybatis.Book">
        select * from book where id=#{id}
    </select>

    <select id="getAllBooks"  resultType="com.draymonder.book.mybatis.Book">
        select * from book
    </select>
</mapper>

bookService

package com.draymonder.book.mybatis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther draymonder
 */
@Service
public class BookService {
  @Autowired
  private BookMapper bookMapper;

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

  public int deleteBookById(Integer id) {
    return bookMapper.deleteBookById(id);
  }

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

  public Book getBookById(Integer id) {
    return bookMapper.getBookById(id);
  }

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

bookController

package com.draymonder.book.mybatis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther draymonder
 */
@RestController
public class BookController {
  @Autowired
  BookService bookService;

  @GetMapping("/bookOps")
  public void bookOps() {
    Book b1 = new Book();
    b1.setName("西厢记");
    b1.setAuthor("王实甫");
    int i = bookService.addBook(b1);
    System.out.println("add book >>> " + i);

    Book b2 = new Book();
    b2.setId(1);
    b2.setName("朝花夕拾");
    b2.setAuthor("鲁迅");
    int updateBook = bookService.updateBookById(b2);
    System.out.println("update book >>> " + updateBook);

    Book b3 = bookService.getBookById(2);
    System.out.println("get book >>> " + b3);

    // int delete = bookService.deleteBookById(1);
    // System.out.println("delete book >>> " + delete);


    List<Book> allBooks = bookService.getAllBooks();
    allBooks.forEach(System.out::println);
  }
}

过程中踩过的坑

  1. mapper注入不到, 记得配置application.xml中的mapper-locations: classpath:mappers/*.xml
  2. mysql相关
    • You must configure either the server or JDBC driver (via the serverTimezone configuration property)
    • 插入的数据是中文的, 但是插入结果是???
    • 解决方案: jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 即url上添加参数
posted @ 2019-11-10 00:43  Draymonder  阅读(225)  评论(0编辑  收藏  举报