springboot2整合mybatis

环境

window 10
Intellij IDEA:2021.1
spring-boot-starter-web:2.4.3
mysql-connector-java:5.1.49
mybatis-spring-boot-starter:2.1.4
druid:1.2.5
MySQL:5.7

整合步骤

引入依赖

假设是刚刚创建的springboot2项目,那么依然可能如下:

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

我们需要添加整合的依赖:

implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
implementation 'mysql:mysql-connector-java:5.1.49'
implementation 'com.alibaba:druid:1.2.5'

将上面的依赖添加进去。

全局配置

我们知道mybatis是和数据库打交道的,所以这里就涉及到数据库连接的问题。
这种配置信息属于全局配置,放到application.properties文件中。

该文件无论是eclipse还是Intellij IDEA都是放在resources目录下;
在这里插入图片描述
application.properties配置信息为:

# 数据库连接
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useSSL=false
spring.datasource.username=yutao
spring.datasource.password=yutaoyutao

# 指定数据源 连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 指定表模型类的包路径
mybatis.type-aliases-package=com.example.boot.pojo
# 指定mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml

表结构

在这里插入图片描述
数据库名:ssm,表名:users

映射

这里的映射是指数据库和Java模型或代码的映射。

在这里插入图片描述

首先,我们要基于users表结构,定义一个pojo类:
类名也叫:Users

package com.example.boot.pojo;
public class Users {
    private Integer id;

    private String name;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }
}

然后,我们再定义一个接口(相当于DAO),里面定义了数据库的操作;

创建一个mapper的包,代码如下:

package com.example.boot.mapper;

import com.example.boot.pojo.Users;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UsersMapper {
    void addUsers(Users users);
}

然后需要创建mybatisxml文件,mybatis会基于下面的配置,对UsersMapper生成动态代理,来操作数据库。

注意,该文件是在resources目录下;因为是mybatisxml文件,可以创建包名来做区别。

UsersMapper.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.example.boot.mapper.UsersMapper">
    <insert id="addUsers" parameterType="com.example.boot.pojo.Users">
        insert into users(name, age) values(#{name}, #{age})
    </insert>
</mapper>

注意:

  1. 如果是eclipse的话,UsersMapper接口类和UsersMapper.xml是可以放在一个包下。但是Intellij IDEA就不行,必须得放到resources目录下;
  2. 这里的UsersMapper.xml文件的路径,要mybatis能找到,就得在全局配置中配置:mybatis.mapper-locations=classpath:mapper/*.xml;其中mapper/*.xml根据实际项目的路径来配置。

service层

接口我们定义service层:

记得:创建service包

定义UserService接口:

package com.example.boot.service;

import com.example.boot.pojo.Users;

public interface UserService {
    void addUsers(Users users);
}

接着实现这个接口:

package com.example.boot.service;

import com.example.boot.mapper.UsersMapper;
import com.example.boot.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    //@Autowired(required = false)
    private UsersMapper usersMapper;//这里会提示红线

    @Override
    public void addUsers(Users users) {
        usersMapper.addUsers(users);
    }
}

注意:

  1. UsersMapper是个接口,在编译阶段,spring并没有这个bean,所以Intellij IDEA会提示红线。解决办法:①使用@Autowired(required = false),因为在运行时,spring会利用动态代理解决这个依赖问题。②如果运行时就报错,就在UsersMapper类上添加@Mapper注解;

controller层

记得:创建controller

package com.example.boot.controller;

import com.example.boot.service.UserService;
import com.example.boot.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/adduser")
    public void addUser(@RequestBody Users users) {
        userService.addUsers(users);
    }

    @RequestMapping("/all")
    public List<Users> getUserAll() {
        return userService.getAll();
    }
}

上面我使用的是@RestController注解,返回的数据都是json格式,因为我是使用postman进行测试,没有使用页面。

启动类

package com.example.boot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.boot.mapper")
public class BootApplication {
	public static void main(String[] args) {
		SpringApplication.run(BootApplication.class, args);
	}
}

启动类,一定要记得加上注解:

@MapperScan("com.example.boot.mapper")

测试

在这里插入图片描述

总结

最佳实战:

  • 引入mybatis-starter
  • 配置application.yaml中,指定mapper-location位置
  • 编写Mapper接口并标注@Mapper注解
  • 简单方法直接注解方式;即不用xml文件;如:@Select@Insert
  • 复杂方法编写mapper.xml进行绑定映射;(混合了注解和配置文件)
  • 在启动类上使用@MapperScan("包名"),这样其他接口可以不用标注@Mapper注解;如果启动类和mapper不在同一个目录或者启动类的子目录中,也会扫描不到mapper类。会报如下错误:
Field userMapper in com.ssm.boot.gateway.mapper.repository.UserImpl required a bean of type 'com.ssm.boot.mapper.UserMapper' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)

参考地址:

雷丰阳2021版SpringBoot2零基础入门springboot全套完整版
SpringBoot整合MyBatis_北京尚学堂_Java教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值