标签:driver ali cte sel mysq 导入 定义 ide style
导入包
<!-- 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>
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
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>
@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(); } }
标签:driver ali cte sel mysq 导入 定义 ide style
原文地址:https://www.cnblogs.com/xp2h/p/12396187.html