spring-boot整合Mybatis

一、引入依赖

在pom.xml中引入一下依赖,依赖看名字和注释应该能理解:

<!--        MySQL依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
<!--        jdbc依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.4.2</version>
        </dependency>
<!--        Mybatis依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

二、修改配置文件

在application.properties文件中写入数据库和mybatis相关信息(本文使用的是properties文件,你也可以选择yml等文件)

#数据源
spring.datasource.url=jdbc:mysql://localhost:3306/learn
spring.datasource.username=learn
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis
#定义Mapper的XML路径
mybatis.mapper-locations=classpath:/mapper/*Mapper.xml
#定义别名扫描的包
#mybatis.type-aliases-package=

三、创建数据表

创建如下数据表用于测试:

CREATE TABLE `user` (
  `id` INT(32) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(32) NOT NULL,
  `age` INT(2) NOT NULL,
  `work` VARCHAR(32),
  PRIMARY KEY (`id`)
)

image-20210124112900637

四、创建实体类、controller、service和dao

创建user实体类:

package cn.qzlin;

/**
 * @description: user实体类
 * @author: qzl
 * @created: 2021/01/24 11:30
 */

public class User {
    private int id;
    private String name;
    private int age;
    private String work;

    /*
    get、set省略
    */

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", work='" + work + '\'' +
                '}';
    }
}

创建UserDao:

package cn.qzlin.dao;

import cn.qzlin.enity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDao {
    public List<User> getUserList();
}

注:@Repository注解是spring的注解,主动标识当前类要交给spring容器管理(相当于@Component注解)。
一般添加了@Mapper注解或者@MapperScan注解,那么就不用添加@Repository。因为@Mapper注解(@MapperScan注解)已将改接口的代理类给了spring容器管理。
另外:如果不加@MapperScan注解,那么Mapper接口类上就要添加@Mapper注解。

创建UserService

package cn.qzlin.service;

import cn.qzlin.dao.UserDao;
import cn.qzlin.enity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: qzl
 * @created: 2021/01/24 11:36
 */
@Service
public class UserService {

    @Autowired
    UserDao userDao;

    public List<User> getAllUser(){
        return userDao.getUserList();
    }
}

创建controller:

package cn.qzlin.controller;

import cn.qzlin.enity.User;
import cn.qzlin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description:
 * @author: qzl
 * @created: 2021/01/24 11:37
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/all")
    @ResponseBody
    public List<User> getAll(){
        return userService.getAllUser();
    }
}

在UserMapper中编写SQL语句:

<?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="cn.qzlin.dao.UserDao">
    <select id="getUserList" resultType="cn.qzlin.enity.User">
        SELECT * FROM USER
    </select>
</mapper>

Spring Boot不建议使用XML文件配置,所以我们直接在Mapper类中采用注解的形式操作数据库,通过@MapperScan扫描制定的映射器存放路径,最终不需要加任何注解,也不需要对应的xml文件来配置sql语句。所以可以不需要UserMapper,采用全注解,将UserDao修改如下:

五、测试

最后项目包结构如下:

image-20210124114729514

在启动类添加注解@ComponentScan,以便包能够被扫描到,

使用@MapperScan注解,标识扫描dao接口的位置

package cn.qzlin.springbootmybatis;

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

@MapperScan("cn.qzlin.dao") //扫描的mapper
@SpringBootApplication
@ComponentScan(basePackages = {"cn.qzlin"})
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

在浏览器中输入http://localhost:8080/user/all测试:

image-20210124114838882

on {

public static void main(String[] args) {
    SpringApplication.run(SpringbootMybatisApplication.class, args);
}

}




在浏览器中输入http://localhost:8080/user/all测试:

[外链图片转存中...(img-HwXSZBEp-1611484583936)]

至此,spring-boot和Mybatis整合成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值