总目录
https://preparedata.blog.csdn.net/article/details/120062997
文章目录
- 总目录
- 一、引入依赖
- 二、配置mybatis-plus
- 1.配置数据源
- 2.配置mybatis-plus
- 三、服务启动类配置@MapperScan
- 四、新建实体
- 五、新建Mapper接口
- 六、新建Mapper接口的Mapper.xml
- 七、新建接口
- 八、新建接口的实现
- 九、测试
一、引入依赖
MyBatis-Plus对MyBatis做了功能封装,仅仅引入MyBatis-Plus即可
//mysql连接
implementation 'mysql:mysql-connector-java:8.0.23'
//mybatis-plus 地址:https://baomidou.com/
implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.2'
二、配置mybatis-plus
1.配置数据源
datasource:
url: jdbc:mysql://localhost:3306/shopping-order?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&rewriteBatchedStatements=true
username: root
password: 123456
# 使用com.mysql.cj.jdbc.Driver 或 com.mysql.jdbc.Driver 要依据MySQL的版本
driver-class-name: com.mysql.cj.jdbc.Driver
驱动使用com.mysql.cj.jdbc.Driver
或 com.mysql.jdbc.Driver
要依据MySQL的版本
2.配置mybatis-plus
mybatis-plus:
# sql xml映射文件目录,配置在resources/mybatis文件夹下, 自定义目录:mybatis/*Mapper.xml
mapper-locations: classpath:mybatis/*Mapper.xml
# 数据库表实体目录 与步骤(四)中相匹配实体包地址匹配
typeAliasesPackage: com.pd.shopping.order.model.entity
三、服务启动类配置@MapperScan
com.pd.shopping.order.mapper 为mapper文件的包地址, 也就是步骤(五)中Mapper接口的包地址
@MapperScan("com.pd.shopping.order.mapper")
四、新建实体
实体包地址 com.pd.shopping.order.model.entity 需要与步骤(二)中相匹配
package com.pd.shopping.order.model.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
@Data
@TableName("order_detail")
public class OrderDetail {
private Long id;
private String code;
}
五、新建Mapper接口
Mapper接口的包地址 com.pd.shopping.order.mapper 需要与步骤(三)中相匹配,这样才能被MapperScan扫描到
package com.pd.shopping.order.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pd.shopping.order.model.entity.OrderDetail;
public interface OrderDetailMapper extends BaseMapper<OrderDetail> {
//自定义操作表数据方法
}
六、新建Mapper接口的Mapper.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">
<mapper namespace="com.pd.shopping.order.mapper.OrderDetailMapper">
<!-- 自定义操作表数据方法 -->
</mapper>
七、新建接口
package com.pd.shopping.order.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pd.shopping.order.model.entity.OrderDetail;
public interface OrderDetailService extends IService<OrderDetail> {
}
八、新建接口的实现
package com.pd.shopping.order.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pd.shopping.order.mapper.OrderMapper;
import com.pd.shopping.order.model.entity.Order;
import com.pd.shopping.order.service.OrderService;
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
}
九、测试
package com.pd.shopping.order.controller;
import com.pd.shopping.order.model.entity.OrderDetail;
import com.pd.shopping.order.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private OrderDetailService orderDetailService;
@GetMapping("/getOrderList")
public void getOrderList() {
OrderDetail list = orderDetailService.getById(1L);
}
}