Springboot整合Mybatis+Mybatis Plus

一、創(chuàng)建springboot項目

二、配置數(shù)據(jù)庫連接

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/order?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

新建數(shù)據(jù)庫表:

DROP TABLE IF EXISTS `order_db`;
CREATE TABLE `order_db` (
  `order_id` bigint(20) NOT NULL,
  `price` decimal(10,0) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `status` varchar(50) NOT NULL,
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `order_db` VALUES ('1', '222', '2', 'over');

三、配置Mybatis+Mybatis Plus

  • 添加mybatis-plus-boot-starter
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>2.3</version>
        </dependency>
  • yml配置Mybatis Plus
mybatis-plus:
  global-config:
    db-config:
      id-type: auto
      field-strategy: not_empty
      #駝峰下劃線轉(zhuǎn)換
      column-underline: true
      #邏輯刪除配置
      logic-delete-value: 0
      logic-not-delete-value: 1
      db-type: mysql
    refresh: false
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.springbootshardingjdbc.entity
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
  • 創(chuàng)建實體類和映射文件


實體類:

public class OrderDb {
    public Long orderId;
    public BigDecimal price;
    public Integer userId;
    public String status;

    get\set....

OrderDbMapper :繼承BaseMapper,泛型為所要操作實體類

public interface OrderDbMapper extends BaseMapper<OrderDb> {
}

OrderDbMapper.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.example.shardingsphere.dao.OrderDbMapper">

</mapper>

ShardingsphereApplication 中開啟Mapper文件掃描 :@MapperScan

@SpringBootApplication
@MapperScan(value = "com.example.shardingsphere.dao")
public class ShardingsphereApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShardingsphereApplication.class, args);
    }

}

四、添加Service

OrderDbService

public interface OrderDbService extends  IService<OrderDb> {
}

OrderDbServiceImpl :需要繼承ServiceImpl類,泛型為Mapper類和實體類,需要添加@Service注解

@Service
public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> implements OrderDbService {
    
}

五、編寫測試類進行測試

@SpringBootTest
class ShardingsphereApplicationTests {

  @Autowired
    OrderDbMapper orderDbMapper;
    @Autowired
    OrderDbService orderDbService;

    @Test
    void contextLoads() {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "over");
        List<OrderDb> orderDbs1 = orderDbMapper.selectByMap(map);
        List<OrderDb> orderDbs2 = orderDbService.selectByMap(map);
        System.out.println(JSON.toJSONString(orderDbs1));
        System.out.println(JSON.toJSONString(orderDbs2));
    }

}

輸出:
[{"orderId":1,"price":222,"status":"over","userId":2}]
[{"orderId":1,"price":222,"status":"over","userId":2}]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容