Integrating MyBatis for SpringBoot data access

This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Mybatis annotated version:

Intimate link: Github

At the bottom of the page, find the Quick Start document


The above links are easy for readers to find.

Set up the environment with quick start documentation:

To connect to a database, use either navicate or Idea (if mysql version is older than 8, the time zone must be set for the Idea link).

jdbc:mysql://localhost:3306/vuesite? serverTimezone=GMTCopy the code

Create database:

Create four properties, default ID primary key, increment operation.

use  vuesite;
CREATE TABLE city
(
    id      INT PRIMARY KEY auto_increment,
    name    VARCHAR(255),
    state   VARCHAR(255),
    country VARCHAR(255));Copy the code

Create entity class:

package com.xbhog.pojo;

import lombok.Data;

@Data
public class City {
    private Long id;
    private String name;
    private String state;
    private String country;
}
Copy the code

Create Mapper:

Creating CityMapper and using annotations to implement SQL mapping

package com.xbhog.Mapper;

import com.xbhog.pojo.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface CityMapper {
    @Select("select * from user where id = #{id}")
    public City getCityId(Long id);
}
Copy the code

Create a Service:

package com.xbhog.service;

import com.xbhog.Mapper.CityMapper;
import com.xbhog.pojo.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CityService {
    @Autowired
    CityMapper cityMapper;

    public City getCityId(Long id){
        returncityMapper.getCityId(id); }}Copy the code

Using the Service annotation declaration and adding the class to the container for later invocation, the Mapper layer method is called in the Service layer, equivalent to the Service call Dao.

Create a Controller:

import com.xbhog.pojo.City;
import com.xbhog.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class Mycontro {
    @Autowired
    CityService cityService;


    @ResponseBody
    @GetMapping("/city")
    public City getCity(@RequestParam("id") Long id){
        returncityService.getCityId(id); }}Copy the code

Add database data:

Testing:

Since we wrote a method that requires a parameter, which is the id of the data, we need to take the ID parameter in the URL. Format:

    http://localhost:8080/city?id=
Copy the code

MyBatis

Let’s add a method to CItyMapper:

package com.xbhog.Mapper;

import com.xbhog.pojo.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CityMapper {
    @Select("select * from city where id = #{id}")
    public City getCityId(Long id);

    public void addCity(City city);
}
Copy the code

For this method we use a configuration file to bind.

Create citymapper.xml file:


      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xbhog.Mapper.CityMapper">
    
    <insert id="addCity" parameterType="com.xbhog.pojo.City">
        insert into city(`name`,`state`,`country`) values (#{name},#{state},#{country});
    </insert>
</mapper>
Copy the code

Where the namespace and CityMapper should correspond to the insert operation.

Add method to Service layer:

package com.xbhog.service;

import com.xbhog.Mapper.CityMapper;
import com.xbhog.pojo.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CityService {
    @Autowired
    CityMapper cityMapper;

    public void addCity(City city){ cityMapper.addCity(city); }}Copy the code

Add a method to Controller:

import com.xbhog.pojo.City;
import com.xbhog.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller
public class Mycontro {

    @Autowired
    CityService cityService;

    @ResponseBody
    @PostMapping("/add")
    public City addcity(City city){
        cityService.addCity(city);
        returncity; }}Copy the code

See if the information is correct by returning city.

Testing:

We tested this by sending requests through PostMan.

The request is going to be Post, and the URL is going to be the add request in our Controller. The parameters for post submission are name, state, and country.

It is automatically encapsulated as City.

Select * from Mybatis where id = Null; select * from Mybatis where id = useGeneratedKeys;

The added data will return the ID to the id in the incoming city.

<insert id="addCity" parameterType="com.xbhog.pojo.City" useGeneratedKeys="true" keyProperty="id">
    insert into city(`name`,`state`,`country`) values (#{name},#{state},#{country});
</insert>
Copy the code

This can also be done using annotations:

import com.xbhog.pojo.City;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CityMapper {
    @Select("select * from city where id = #{id}")
    public City getCityId(Long id);
    
    @Insert("insert into city(`name`,`state`,`country`) values (#{name},#{state},#{country})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    public void addCity(City city);
}

Copy the code
public City addcity(City city){  // id -->id -->id
    cityService.addCity(city);
    return city;  
}
Copy the code

Check the effect:

References:

Liverpoolfc.tv: making

Began to document

B station SpringBoot

Mybatis website

The end:

If you see here or just to help you, I hope you can point to follow or recommend, thank you;

If there are any errors, please point them out in the comments and the author sees them will be corrected.