SpringBoot 整合mybatis-plus
1、导入Maven依赖
<dependencies>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!--数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、application.yml配置
spring:
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/learn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
mybatis-plus:
mapper-locations: classpath*:mapper/**/*Mapper.xml # 存放sql语句的xml文件目录
type-aliases-package: com.hu.entity # 实体类包路径
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 放开则开启SQL打印
map-underscore-to-camel-case: true # 开启驼峰法则
global-config:
db-config:
table-prefix: tbl_ # 数据库表前缀
3、项目结构
4、entity
import lombok.Data;
import java.io.Serializable;
@Data
public class Book implements Serializable {
private Integer id;
private String type;
private String name;
private String description;
}
5、mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hu.entity.Book;
import java.util.List;
public interface BookMapper extends BaseMapper<Book> {
List<Book> getBookAll();//查询全部
}
6、resource下的mapper
<?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="com.hu.mapper.BookMapper">
<select id="getBookAll" resultType="Book">
select * from tbl_Book;
</select>
</mapper>
7、启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.hu.mapper") // 扫描mapper接口包,不用在接口上加@Mapper注解
@SpringBootApplication
public class MyBatisApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisApplication.class, args);
}
}
8、service
import com.baomidou.mybatisplus.extension.service.IService;
import com.hu.entity.Book;
import java.util.List;
public interface BookService extends IService<Book> {
List<Book> getBookAll();
}
9、impl
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.mapper.BookMapper;
import com.hu.entity.Book;
import com.hu.service.BookService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
@Resource
private BookMapper bookMapper;
@Override
public List<Book> getBookAll() {
return bookMapper.getBookAll();
}
}
10、controller
@RestController
@ResponseBody
@RequestMapping("/book")
public class BookController {
@Resource
private BookService bookService;
@GetMapping
public List<Book> getAll(){
return bookService.getBookAll();
}
}
标签:mapper,SpringBoot,boot,plus,org,mybatis,import,com,public From: https://www.cnblogs.com/huxiaoan1/p/16877527.html时间:2022-11-10 下午