mybatis(一):springboot集成mybatis-plus

之前一直用的jpa,现在项目上开始用mybaits,今天想搭一个自己玩玩但是过程有点艰辛,特地记录下来。

首先引入pom依赖:


        
            com.baomidou
            mybatis-plus
            2.3
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            2.3
        

        
            org.springframework.boot
            spring-boot-starter
            2.0.0.RELEASE
        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
            org.springframework.boot
            spring-boot-starter-web
            2.0.0.RELEASE
        
        
            com.alibaba
            fastjson
            1.2.34
        

    

开始由于没有引入 mybatis-plus-boot-starter依赖,一直无法注入要spring中。

接下来是写一个配置文件 application.yml:

server:
  port: 8083

mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml

spring:
  datasource:
    name: test
    url: jdbc:mysql://127.0.0.1:3306/test_mybatis?characterEncoding=utf8&allowMultiQueries=true
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

 

在写一个mapper:

import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import test.mybatis.plus.entity.Person;

import java.util.List;

@Mapper
public interface PersonMapper extends BaseMapper {

    List selectByAge(Integer age);
}

PersonMapper.xml文件:





    

 

这边由于把resultType和parameterType写成了resultMap和parameterMap,然后程序一直报错:

Result Maps collection does not contain value 

Parameter Maps collection does not contain value

其实这边只要改成resultType和parameterType就可以解决了。

 

到这边之后调用接口发现,又报错了,真是够心累的。。。

nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating interface test.mybatis.plus.mapper.PersonMapper with invalid types () or values (). Cause: java.lang.NoSuchMethodException: test.mybatis.plus.mapper.PersonMapper.()

看报错说是PersonMapper没有初始化,原来是我们在 resultType那里返回的结果写错了,我写成了mapper的,应该是model的才对,改成model之后成功返回了结果。

太不容易了。

 

 最后再附上Peron表的代码  以及 service和controller代码:

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;

import java.io.Serializable;

@TableName("person")
public class Person implements Serializable {

    @TableId(value = "id", type = IdType.UUID)
    private String id;

    private String name;

    private Integer age;

    @TableField(value = "address")
    private String address;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}



public interface IPersonService {
    Person getPersonById(String id);

    List getPersonByAge(Integer age);

    void insertPerson(Person person);
}


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.mybatis.plus.entity.Person;
import test.mybatis.plus.mapper.PersonMapper;
import test.mybatis.plus.service.IPersonService;

import java.util.List;
import java.util.UUID;

@Service
public class PersonServiceImpl implements IPersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public Person getPersonById(String id) {
        return personMapper.selectById(id);
    }

    @Override
    public List getPersonByAge(Integer age) {
        return personMapper.selectByAge(age);
    }

    @Override
    public void insertPerson(Person person) {
        person.setId(UUID.randomUUID().toString().replace("-",""));
        personMapper.insert(person);
    }
}


import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import test.mybatis.plus.entity.Person;
import test.mybatis.plus.service.IPersonService;

@RestController
@RequestMapping("person")
public class PersonContrller {

    @Autowired
    private IPersonService personService;

    @GetMapping("{id}")
    public String personById(@PathVariable String id){
        return JSONObject.toJSONString(personService.getPersonById(id));
    }

    @PostMapping("insert")
    public void insert(@RequestBody Person person){
        personService.insertPerson(person);
    }

    @GetMapping("/age")
    public String personByAge(@RequestParam Integer age){
        return JSONObject.toJSONString(personService.getPersonByAge(age));
    }
}

 

到这里一个简单的springboot集成mybaits就完成,下次再搭建一个mybaits集成分页的功能

你可能感兴趣的:(mybatis搭建,mybatis)