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.csdn.net/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