SpringBoot 整合Mybatis (混合版 | 注解版)

数据库
在这里插入图片描述
表单

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" th:action="@{/city}">
    NAME:<input type="text" name="name"><br/>W
    stata:<input type="text" name="stata"><br/>
    country:<input type="text" name="country"><br/>
    <input type="submit" value="提交">
</form>
</body>
</html>



一、注解版

1)pom.xml引入mybatis-starter

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

2)创建实体类

package com.atguigu.admin.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author LunarYouI
 * @create 2021-04-14 19:12
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class City {
    private Long id;
    private String name;
    private String stata;
    private String country;

}

3)yaml配置文件链接数据库

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_test1
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    #查询超时(3秒内没有查询出来就被认为查询超时)  单位:秒
  jdbc:
    template:
      query-timeout: 3

4)mapper

package com.atguigu.admin.mapper;

import com.atguigu.admin.bean.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;

/**
 * @author LunarYouI
 * @create 2021-04-14 19:14
 */

@Mapper
public interface CityMapper {
    //纯注解版
    @Select("select * from city where id=#{id}")
    public City getById(Long id);

    @Insert("insert into city(name,stata,country) value(#{name},#{stata},#{country})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    public void insert(City city);
}

5)service

package com.atguigu.admin.service;

import com.atguigu.admin.mapper.CityMapper;
import com.atguigu.admin.bean.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author LunarYouI
 * @create 2021-04-14 19:20
 */

@Service
public class CityService {

    @Autowired
    CityMapper cityMapper;

    public City getById(Long id){
        City byId = cityMapper.getById(id);
        return byId;
    }

    public void saveCity(City city) {
        cityMapper.insert(city);
    }
}

6)controller

package com.atguigu.admin.controller;

import com.atguigu.admin.bean.City;
import com.atguigu.admin.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;

/**
 * @author LunarYouI
 * @create 2021-04-14 19:17
 */

@Controller
public class CityByOperation {

    @Autowired
    CityService cityService;

    @GetMapping("biaodan")
    public String biaodan(){
        return "biaodan";
    }

    @ResponseBody
    @GetMapping("/city")
    public City getCityById(@RequestParam("id") Long id){
        City byId = cityService.getById(id);
        return byId;
    }

    @ResponseBody
    @PostMapping("/city")
    public City saveCity(City city){
        cityService.saveCity(city);
        return city;
    }
}

7)运行结果

查询
在这里插入图片描述
插入
在这里插入图片描述
在这里插入图片描述







二、混合版

与注解版相差不大,唯一改动的地方接就是mapper

1)mapper

package com.atguigu.admin.mapper;

import com.atguigu.admin.bean.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;

/**
 * @author LunarYouI
 * @create 2021-04-14 19:14
 */

@Mapper
public interface CityMapper {
    //纯注解版
    @Select("select * from city where id=#{id}")
    public City getById(Long id);

//    @Insert("insert into city(name,stata,country) value(#{name},#{stata},#{country})")
//    @Options(useGeneratedKeys = true,keyProperty = "id")
//    public void insert(City city);

    //混合版
    public void insert(City city);
}

2)创建mapper.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.atguigu.admin.mapper.CityMapper">

    <!--只要插入数据,那么会默认的把自增主键的id返回——————使用自增的主键useGeneratedKeys="true" 自增主键的属性是什么keyProperty-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into city(name,stata,country) value(#{name},#{stata},#{country})
    </insert>

</mapper>

3)配置文件

#JavaBean位置
mybatis.type-aliases-package=com.atguigu.admin.bean
#mapper.xml位置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

在这里插入图片描述
如果不愿意每次都往mapper下的**mapper接口中添加@mapper注解 那么可以在启动类中添加@MapperScan(“com.atguigu.admin.mapper”)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值