Springboot使用Mybatis框架连接MySQL数据库

1、依赖注入
注入MySQL依赖(MySQL驱动使用Springboot版本对应的推荐版本,不需要手动指定版本)

 <dependency>
	<groupId>com.mysql</groupId>
	<artifactId>mysql-connector-j</artifactId>
	<scope>runtime</scope>
</dependency>

注入Mybatis依赖(使用Springboot版本的推荐版本)

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

注入Lombok依赖

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.10</version
	<scope>provided</scope>
</dependency>

2、配置数据源
在application.aproperties文件中,配置MySQL数据库的驱动、数据库url、用户名、密码。

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimeZone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

mybatis_mapperlocation.classpath=mapper/*.xml

3、编程
在Java目录下创建实体(entity)、映射(mapper)、服务(service)、服务实现(service/impl)、及控制器(controller)文件夹;
在resources目录下创建com.example.mapper文件夹
(1)、在entity中创建User实体

package com.example.entity;
import lombok.Data;
@Data
public class User {
    private Long id;
    private String name;
    private String mobile;
    private String email;
}

(2)、在mapper中创建UserMapper接口UserMapper.java

package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
    List<User> listAll();
}

(3)、在资源文件夹下创建UserMapper.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.mapper.UserMapper">
    <select id="listAll" resultType="com.example.entity.User">
        select * from user
    </select>
</mapper>

(4)、在服务中创建service接口UserService.java

package com.example.service;
import com.example.entity.User;
import java.util.List;
public interface UserService {
    List<User> listAll();
}

(5)、在服务实现中创建UserServiceImpl.java

package com.example.service.impl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> listAll() {
        return userMapper.listAll();
    }
}

(6)、在控制器中创建UserController.java

package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping
    public List<User> selectAll() {
        return userService.listAll();
    }
}

4、另外一种方法(不需要在资源种创建xml文件)
在mapper中创建UserMapper接口UserMapper.java,添加注解。

package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
    @Select("select * from user")
    List<User> listAll();
}

5、相关支持
springboot官网:https://spring.io/projects/spring-boot
Maven仓库:https://mvnrepository.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大浪淘沙胡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值