0
点赞
收藏
分享

微信扫一扫

【Spring Boot】Mybatis 开发方式



文章目录

  • ​​Mybatis 开发方式​​
  • ​​方式一:注解​​
  • ​​1.创建数据库信息​​
  • ​​2.封装bean​​
  • ​​3.创建CityMapper​​
  • ​​4.创建CityService​​
  • ​​5.测试​​
  • ​​方式二:xml 配置​​
  • ​​1.修改CityMapper​​
  • ​​2.创建CityMapper.xml​​
  • ​​3.修改CityService​​
  • ​​4.测试​​
  • ​​方式三:混合注解和xml​​

Mybatis 开发方式

方式一:注解

1.创建数据库信息

  • 准备一张city表

【Spring Boot】Mybatis 开发方式_spring boot

/*
Navicat MySQL Data Transfer

Source Server : test
Source Server Type : MySQL
Source Server Version : 80017
Source Host : localhost:3306
Source Schema : dormitory

Target Server Type : MySQL
Target Server Version : 80017
File Encoding : 65001

Date: 27/07/2021 20:41:19
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for city
-- ----------------------------
DROP TABLE IF EXISTS `city`;
CREATE TABLE `city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`state` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`country` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of city
-- ----------------------------
INSERT INTO `city` VALUES (1, '北京', '1', '中国');
INSERT INTO `city` VALUES (2, '南京', '1', '中国');
INSERT INTO `city` VALUES (3, '江苏', '2', '中国');
INSERT INTO `city` VALUES (4, '山东', '2', '中国');
INSERT INTO `city` VALUES (5, '纽约', '3', '美国');
INSERT INTO `city` VALUES (6, '洛杉矶', '3', '美国');

SET FOREIGN_KEY_CHECKS = 1;

​​返回顶部​​

2.封装bean

package com.zyx.core.admin.bean;

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

/**
* @author 35192
* @date 2021-07-27 20:21
*/
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class City {
private Integer id;
private String name;
private String state;
private String country;
}

​​返回顶部​​

3.创建CityMapper

  • 我们创建一个查询的方法,可以发现报错:没有对应的mapper.xml匹配,这里我们将采用注解的形式进行。
  • 【Spring Boot】Mybatis 开发方式_spring boot_02

我们直接使用@Select注解进行查询:@Select("select * from city where id=#{id}")其中的id就会从方法的参数进行匹配。

package com.zyx.core.admin.mapper;

import com.zyx.core.admin.bean.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
* @author 35192
* @date 2021-07-27 20:24
*/
@Mapper
public interface CityMapper {

@Select("select * from city where id=#{id}")
public City getById(Integer id);
}

​​返回顶部​​

4.创建CityService

package com.zyx.core.admin.service;

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

/**
* @author 35192
* @date 2021-07-27 20:31
*/
@Service
public class CityService {

@Autowired
CityMapper cityMapper;

public City getById(Integer id){
City city = cityMapper.getById(id);
return city;
}
}

​​返回顶部​​

5.测试

package com.zyx.core.admin.test;

import com.zyx.core.admin.bean.City;
import com.zyx.core.admin.service.AdminService;
import com.zyx.core.admin.service.CityService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;

/**
* @author 35192
* @date 2021-07-23 16:44
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MybatisTest {

@Autowired
CityService cityService;

@Test
public void getById() {
City city = cityService.getById(1);
System.out.println(city);
}
}

【Spring Boot】Mybatis 开发方式_返回顶部_03

​​返回顶部​​

方式二:xml 配置

1.修改CityMapper

package com.zyx.core.admin.mapper;

import com.zyx.core.admin.bean.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
* @author 35192
* @date 2021-07-27 20:24
*/
@Mapper
public interface CityMapper {

@Select("select * from city where id=#{id}")
public City getById(Integer id);

// 添加操作
public void insert(City city);
}

​​返回顶部​​

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

<!-- public void insert(City city); -->
<insert id="insert" parameterType="com.zyx.core.admin.bean.City" useGeneratedKeys="true" keyColumn="id">
INSERT INTO city(name, state, country) VALUES(#{name},#{state},#{country})
</insert>

</mapper>

该方式等同于:

@Insert(" INSERT INTO city(name, state, country) VALUES(#{name},#{state},#{country})")
@Options(useGeneratedKeys = true,keyProperty = "id")
public void insert(City city);

  • useGeneratedKeys 和 keyProperty 搭配使用,这样可以解决在主键自增的情况下获取主键

​​返回顶部​​

3.修改CityService

package com.zyx.core.admin.service;

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

/**
* @author 35192
* @date 2021-07-27 20:31
*/
@Service
public class CityService {

@Autowired
CityMapper cityMapper;

public City getById(Integer id){
City city = cityMapper.getById(id);
return city;
}

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

​​返回顶部​​

4.测试

package com.zyx.core.admin.test;

import com.zyx.core.admin.bean.City;
import com.zyx.core.admin.service.AdminService;
import com.zyx.core.admin.service.CityService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;

/**
* @author 35192
* @date 2021-07-23 16:44
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MybatisTest {

@Autowired
CityService cityService;

@Test
public void getById() {
City city = cityService.getById(1);
System.out.println(city);
}

@Test
public void insert() {
City city = new City();
city.setName("成都");
city.setState("2");
city.setCountry("中国");
cityService.insert(city);
System.out.println(city);
}
}

【Spring Boot】Mybatis 开发方式_spring boot_04

【Spring Boot】Mybatis 开发方式_mybatis_05

​​返回顶部​​

方式三:混合注解和xml

  • 混合使用方式一、方式二即可!

最佳实战:

  • 引入mybatis-starter
  • 配置application.yaml中,指定mapper-location位置即可
  • 编写Mapper接口并标注@Mapper注解
  • 简单方法直接注解方式
  • 复杂方法编写mapper.xml进行绑定映射
  • @MapperScan(“com.atguigu.admin.mapper”) 简化,其他的接口就可以不用标注@Mapper注解

​​返回顶部​​


举报

相关推荐

0 条评论