12-SpringBoot之数据库(三)——Mybatis

1. 添加pom依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

2. Mybatis配置

在application.yml文件中添加Mybatis配置,如下:

 #mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.springboot.web.model

项目启动类添加@MapperScan

@MapperScan("com.springboot.web.dao")

3. 案例开发

3.1 创建实体类

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String id;
    private String name;
    private Integer age;

    @JSONField(format="yyyy-MM-dd")
    private Date birthday;

    public User(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

3.2 DAO

DAO接口:

import com.springboot.web.model.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMybatisDao {
    Integer insert(User user);
    List<User> getAllUserList();
}

mapper文件:在resources\mapper文件下创建UserMapper.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.springboot.web.dao.UserMybatisDao">
    <sql id="BASE_TABLE">
      user
    </sql>

    <sql id="BASE_COLUMN">
      id,name,age,birthday
    </sql>

    <insert id="insert" parameterType="com.springboot.web.model.User">
        INSERT INTO
        <include refid="BASE_TABLE"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="BASE_COLUMN"/>
        </trim>
        <trim prefix="VALUES(" suffix=")" suffixOverrides=",">
            #{id, jdbcType=VARCHAR},#{name, jdbcType=VARCHAR},
            #{age, jdbcType=INTEGER}, #{birthday, jdbcType=DATE},

        </trim>
    </insert>

    <select id="getAllUserList" resultType="com.springboot.web.model.User">
        SELECT
        <include refid="BASE_COLUMN"/>
        FROM
        <include refid="BASE_TABLE"/>
    </select>
    
</mapper>

3.3 Service

service接口:

import com.springboot.web.model.User;

import java.util.List;

public interface UserMybatisService {
    Integer insert(User user);
    List<User> getAllUserList();
}

service实现:

import com.springboot.web.dao.UserMybatisDao;
import com.springboot.web.model.User;
import com.springboot.web.service.UserMybatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserMybatisServiceImpl implements UserMybatisService {


    @Autowired
    private UserMybatisDao userMybatisDao;

    @Override
    public Integer insert(User user) {
        return userMybatisDao.insert(user);
    }

    @Override
    public List<User> getAllUserList() {
        return userMybatisDao.getAllUserList();
    }
}

3.4 Controller

import com.springboot.web.model.User;
import com.springboot.web.service.UserMybatisService;
import com.springboot.web.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/mybatis")
public class UserMybatisController {

    @Autowired
    private UserMybatisService userMybatisService;

    @GetMapping("/getAllUserList")
    public Object getAllUserList() {
        return userMybatisService.getAllUserList();
    }

    @PostMapping("/createUser")
    public void createUser(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        User user = new User(UUIDUtil.getUUID(), name, age, new Date());
        userMybatisService.insert(user);
    }
}

3.5 测试

使用postman进行测试

测试createUser:在这里插入图片描述
在这里插入图片描述

测试getAllUserList:在这里插入图片描述

4. 源码下载

源码下载地址:https://download.youkuaiyun.com/download/huangjun0210/10791197

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥俊子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值