SpringBoot 整合 MyBatis 实现简单注解开发

本文详细介绍如何在Spring Boot中集成MyBatis框架,并通过具体示例演示如何使用MyBatis进行数据库操作,包括增删改查等基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、引入依赖
<!--mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>RELEASE</version>
</dependency>
2、实体类
public class Animal {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;

    //省略 get set
3、创建 Animal 映射的操作 AnimalMapper
package com.zzq.mapper;

import com.zzq.entity.Animal;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author zzq
 * @createTime 2018/4/1
 */
@Mapper
@Repository
public interface AnimalMapper {
    @Select("select * from animal where id = #{id}")
    Animal findAnimalById(@Param("id") Integer id);

    @Insert("insert into animal(name, sex, age) values(#{name}, #{sex}, #{age})")
    int insert(Animal animal);

    @Update("update animal set name=#{name}, sex=#{sex}, age=#{age} where id=#{id}")
    void update(Animal animal);

    @Delete("delete from animal where id=#{id}")
    void delete(Integer id);

    /**
     * @Result中的property属性对应Animal对象中的成员名,column对应SELECT出的字段名
     * 如果两个的名称一致,则不用另外指定
     */
/*    @Results({
            @Result(property = "name", column="name"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "age", column = "age")
    })*/
    @Select("select name, sex, age from animal")
    List<Animal> findAll();

    @Insert("insert into animal(name, sex, age) values(" +
            "#{name, jdbcType=VARCHAR}, #{sex, jdbcType=VARCHAR}, #{age, jdbcType=INTEGER})")
    int insertByMap(Map<String, Object> map);

}

4、ServiceImpl(对这里的 @Transaction 注解有不了解的可以看

    https://blog.youkuaiyun.com/qq_39267171/article/details/79746262 这篇整合有简单介绍)


package com.zzq.service.impl;

import com.zzq.entity.Animal;
import com.zzq.mapper.AnimalMapper;
import com.zzq.service.AnimalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zzq
 * @createTime 2018/4/1
 */
@Service
public class AnimalServiceImpl implements AnimalService {
    @Autowired
    private AnimalMapper animalMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Animal findAnimalById(Integer id) {
        return animalMapper.findAnimalById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insert(Animal animal) {
        return animalMapper.insert(animal);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(Animal animal) {
        animalMapper.update(animal);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(Integer id) {
        animalMapper.delete(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Animal> findAll() {
        return animalMapper.findAll();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertByMap(Map<String, Object> map) {
        return animalMapper.insertByMap(map);
    }


}
5、Controller
package com.zzq.controller;

import com.zzq.entity.Animal;
import com.zzq.entity.JsonResult;
import com.zzq.service.AnimalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zzq
 * @createTime 2018/4/1
 */
@RestController
@RequestMapping("animal")
public class AnimalController {
    @Autowired
    private AnimalService animalService;

    @GetMapping("one/{id}")
    public Animal list(@PathVariable("id") Integer id){
        return animalService.findAnimalById(id);
    }

    @PostMapping("insert")
    public int insert(Animal animal){
        return animalService.insert(animal);
    }

    @PostMapping("update")
    public void update(@ModelAttribute("Animal") Animal animal){
        animalService.update(animal);
    }

    @GetMapping("delete/{id}")
    public void delete(@PathVariable("id") Integer id){
        animalService.delete(id);
    }

    @GetMapping("findAll")
    public JsonResult findAll(){
        return JsonResult.ok(animalService.findAll());
    }

    @PostMapping("insertByMap")
    public void insertByMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "admin");
        map.put("sex", "m");
        map.put("age", 14);
        animalService.insertByMap(map);
    }

}

源码地址:https://github.com/EERINESS/springboot-integration

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值