spring boot和mybatis整合

1.pom.xml中的依赖

1)mysql依赖

  
            mysql  
            mysql-connector-java  
     

2)mybatis依赖

     
            org.mybatis.spring.boot  
            mybatis-spring-boot-starter  
            1.3.0  
      

3)“刷新pom.xml”

2.yml配置

1)mysql配置

spring:
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/mybatistest?serverTimezone=UTC&characterEncoding=utf8
        username: root
        password: 88888888

2)mybatis配置

mybatis-plus:
  typeAliasesPackage: com.tony.luckymoney.system.model
  mapper-locations: classpath:com/tony/luckymoney/business/**/mapping/*.xml

3.整合简介

1)创建好数据库和表
2)在Entity包中创建实体类(数据库表的列名的别名最好和要关联的实体类的属性名一致)

package com.tony.luckymoney.mybatistest.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
/**
 * 对象红包信息表
 */
public class Luckymoney {

    private Integer id;
    private String producer;
    private BigDecimal money;
    private String consumer;
    private int typeId;

}

和这个实体类对应的数据表


luckymoney表

3)简单应用(部分代码)
1.controllor

package com.tony.luckymoney.mybatistest.controller;

import com.tony.luckymoney.mybatistest.entity.Luckymoney;
import com.tony.luckymoney.mybatistest.service.LuckyMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class LuckyMoneyController {
    @Autowired
    private LuckyMoneyService luckyMoneyService;

    /**
     * 增添红包信息
     * @param luckymoney luckymoney
     * @return luckymoney
     */
    @PostMapping("/createluckymoney")
    public int insertLuckyMoney(Luckymoney luckymoney) {
        return luckyMoneyService.insertLuckyMoneyInfo(luckymoney);
    }

2.service

package com.tony.luckymoney.mybatistest.service;

import com.tony.luckymoney.mybatistest.entity.Luckymoney;
import com.tony.luckymoney.mybatistest.entity.LuckymoneyStatement;

import java.util.List;

public interface LuckyMoneyService {
    /**
     * 添加红包信息接口
     * @param luckymoney luckymoney
     * @return int型的航记录
     */
    int insertLuckyMoneyInfo(Luckymoney luckymoney);

3.serviceImpl

package com.tony.luckymoney.mybatistest.service.serviceImpl;
@Component
@Slf4j
public class LuckyMoneyServiceImpl implements LuckyMoneyService {
    @Autowired
    private LuckyMoneyService luckyMoneyService;

    @Autowired
    private LuckyMoneyMapper luckyMoneyMapper;
    /**
     * 添加红包信息
     * @param luckymoney luckymoney
     * @return int型的insertRowCount
     */
    @Override
    public int insertLuckyMoneyInfo(Luckymoney luckymoney) {
        return luckyMoneyMapper.insertLuckyMoneyInfo(luckymoney);
    }

4.mapper(写sql语句)

package com.tony.luckymoney.mybatistest.mapper;
public interface LuckyMoneyMapper {
    /**
     * 创建红包信息
     * @param luckymoney
     * @return int
     */
    @Insert("INSERT INTO luckymoney (producer,money,typeId) VALUES (#{obj.producer},#{obj.money},#{obj.typeId})")
    //id自增
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int insertLuckyMoneyInfo(@Param("obj") Luckymoney luckymoney);
}

以上就是spring boot和mybatis整合的简单介绍

你可能感兴趣的:(spring boot和mybatis整合)