SpringBoot数据访问之整合mybatis

475 阅读3分钟

SpringBoot数据访问之整合mybatis

这是我参与8月更文挑战的第9天,活动详情查看:8月更文挑战

mybatis注解版:

贴心链接:Github

在网页下方,找到快速开始文档


上述链接方便读者查找。

通过快速开始文档,搭建环境:

连接数据库,可以采用navicate或者使用Idea中的连接(如果mysql版本是在8以上,idea中的链接需要设置时区)。

    jdbc:mysql://localhost:3306/vuesite?serverTimezone=GMT

创建数据库:

创建四个属性,默认id为主键,自增操作。

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

image-20210808164510638

创建实体类:

package com.xbhog.pojo;

import lombok.Data;

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

创建Mapper:

创建CityMapper并采用注解的方式实现sql映射的问题:

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);
}

创建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){
        return cityMapper.getCityId(id);
    }
}

使用Service注解声明,并将该类加入到容器中,方便后面调用,在service层调用Mapper层的方法, 相当于service调用Dao.

创建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){
        return cityService.getCityId(id);
    }
}

增加数据库数据:

image-20210808171903055

测试:

因为我们编写的方法是需要传参的,也就是数据的id,所以在url设置的需要携带id参数。 格式:

    http://localhost:8080/city?id=

image-20210808172036566

mybatis混合版:

我们在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);
}

这个方法我们采用配置文件来绑定。

创建CityMapper.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.xbhog.Mapper.CityMapper">
    
    <insert id="addCity" parameterType="com.xbhog.pojo.City">
        insert into city(`name`,`state`,`country`) values (#{name},#{state},#{country});
    </insert>
</mapper>

其中命名空间与CityMapper要对应,进行插入操作。

在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 void addCity(City city){
        cityMapper.addCity(city);
    }
}

在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);
        return city;  
    }
}

通过返回的city来查看信息是否正确。

测试:

我们通过PostMan来发送请求信息进行测试。

请求的方式为Post,url就是我们Controller中的add请求。post提交的参数:name、state、country。

会自动封装为City。

image-20210808185953623

从上图可以发现,我们的id是Null,怎么样让添加后的数据返回id呢,在Mybatis中有useGeneratedKeys自增主键,自增主键的名字叫id。

这样添加进去的数据就会将id返回给传入的city中的id。

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

也可以采用注解的方式实现:

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);
}

public City addcity(City city){  //city -->id有值了
    cityService.addCity(city);
    return city;  
}

查看效果:

image-20210808190845548

参考文献:

官网:Github

开始文档

B站SpringBoot

Mybatis官网

结束:

如果你看到这里或者正好对你有所帮助,希望能点个关注或者推荐,感谢;

有错误的地方,欢迎在评论指出,作者看到会进行修改。