(1) Books.java
package com.hello.demo.Untiy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookId;
private String bookName;
private int bookCounts;
private String detail;
}
(1)HelloController.java
package com.hello.demo.Mapper;
import com.hello.demo.Untiy.Books;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BooksMapper {
//增加一个Book
int addBook(Books book);
//根据id删除一个Book
int deleteBookById(int id);
//更新Book
int updateBook(Books books);
//根据id查询,返回一个Book
Books queryBookById(int id);
//查询全部Book,返回list集合
List<Books> queryAllBook();
}
(2)BooksMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hello.demo.Mapper.BooksMapper">
<insert id="addBook" parameterType="com.hello.demo.Untiy.Books">
insert into books(book_id,book_name,book_counts,detail)
values (#{bookId},#{bookName}, #{bookCounts}, #{detail})
insert>
<delete id="deleteBookById" parameterType="int">
delete from books where book_id=#{bookId}
delete>
<update id="updateBook" parameterType="com.hello.demo.Untiy.Books">
update books
set book_name = #{bookName},book_counts = #{bookCounts},detail = #{detail}
where book_id = #{bookId}
update>
<select id="queryBookById" resultType="com.hello.demo.Untiy.Books">
select * from books
where book_id = #{bookId}
select>
<select id="queryAllBook" resultType="com.hello.demo.Untiy.Books">
select * from books order by book_id
select>
mapper>
(1) 接口:BookService.java
package com.hello.demo.Service;
import com.hello.demo.Untiy.Books;
import java.util.List;
public interface BookService {
//增加一个Book
int addBook(Books book);
//根据id删除一个Book
int deleteBookById(int id);
//更新Book
int updateBook(Books books);
//根据id查询,返回一个Book
Books queryBookById(int id);
//查询全部Book,返回list集合
List<Books> queryAllBook();
}
(2)接口实现类:BookServiceImpl.java
package com.hello.demo.Service.ServiceImpl;
import com.hello.demo.Mapper.BooksMapper;
import com.hello.demo.Service.BookService;
import com.hello.demo.Untiy.Books;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
//调用dao层的操作,设置一个set接口,方便Spring管理
private final BooksMapper bookMapper;
public int addBook(Books book) {
return bookMapper.addBook(book);
}
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}
(1) HelloController.java
package com.hello.demo.Controller;
import com.hello.demo.Service.BookService;
import com.hello.demo.Untiy.Books;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequiredArgsConstructor
public class HelloController {
private final BookService bookService;
// @GetMapping("/hello")
// public String book() {
// return bookService.list();
// }
//查询全部Book,返回list集合
@GetMapping("/queryallbook")
public List<Books> queryAllBook() {
return bookService.queryAllBook();
}
//根据id查詢一个Book
@GetMapping("/queryonebook/{bookId}")
public Books queryOneBook(@PathVariable("bookId") int id) {
Books book = bookService.queryBookById(id);
return book;
}
//根据id删除一个Book
@DeleteMapping("/deletebook/{bookId}")
public int deleteBook(@PathVariable("bookId") int id) {
return bookService.deleteBookById(id);
}
//根据id更新一个Book
@PostMapping("/updateonebook")
public int updateBook(@RequestBody Books books){
return bookService.updateBook(books);
}
//新增一个Book
@PutMapping("/addonebook")
public int addBook(@RequestBody Books books){
return bookService.addBook(books);
}
}
plugins {
id 'org.springframework.boot' version '2.5.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'war'
}
ext {
mybatisplusVersion = '3.4.2'
swaggerFoxVersion = '2.9.2'
swaggerCoreVersion = '1.5.24'
postGisJdbcVersion = '2.5.0'
druidVersion = '1.1.9'
hutoolVersion = '5.3.8'
aspectVersion = '1.8.10'
easyExcelVersion = '2.2.6'
}
group = 'com.Hello'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
/* MyBatis-Plus */
implementation("com.baomidou:mybatis-plus-boot-starter:${mybatisplusVersion}")
/* Druid 连接池 */
implementation("com.alibaba:druid-spring-boot-starter:${druidVersion}")
/* PostgreSQL */
implementation 'org.postgresql:postgresql:9.4.1212.jre7'
/* PostGis-Jdbc */
implementation("net.postgis:postgis-jdbc:${postGisJdbcVersion}")
compile 'com.alibaba:druid:1.0.27'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2', version: '1.4.0.RELEASE'
implementation 'junit:junit:4.12'
// 排除冲突
configurations {
mybatisGenerator
compile.exclude module: 'spring-boot-starter-logging'
}
}
test {
useJUnitPlatform()
}
server:
port: 8080
spring:
datasource:
driver-class-name: org.postgis.DriverWrapper
url: jdbc:postgresql_postGIS://***/BOOK?useUnicode=true&characterEncoding=UTF-8
username: postgres
password: 123456
# ??druid???
type: com.alibaba.druid.pool.DruidDataSource
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
# MyBatis-Plus Config
mybatis-plus:
mapper-locations: classpath:/Mapper/*Mapper.xml
global-config:
banner: false
db-config:
id-type: auto
table-underline: true
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl