SpringBoot配置mybatis-mysql数据源

前言

学习SpringBoot整合mybatis + mysql配置,首先需要了解什么是ORM(对象映射关系)框架,ORM(Object Relational Mapping)对象关系映射,是 一种为了解决面向对象与关系型数据库不匹配而出现的技术,使开发者能够用面向对象的方式使用关系型数据库。

学习配置

建表

CREATE TABLE `sys_user` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(100) DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb3;

创建一个简单的用户信息表,来作为数据库操作基础。

pom配置

<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-jdbcartifactId>
    dependency>

    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
        <version>2.2.0version>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-devtoolsartifactId>
        <scope>runtimescope>
        <optional>trueoptional>
    dependency>

    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <scope>runtimescope>
    dependency>

    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <optional>trueoptional>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
dependencies>

CRUD

简单的编写一下java代码来完成数据库的操作方法

domain(java-数据库映射实体)

import lombok.Data;
import java.io.Serializable;

/**
 * @author yanzy
 * @date 2021/6/15 21:52
 * @since v1.0
 */
@Data
public class SysUser implements Serializable {

    private Long id;

    private String name;

    private String password;
}

mapper(数据库操作接口)

import com.zy.study.domain.SysUser;

/**
 * @author Yanzy
 */
public interface SysUserMapper {

    int deleteByPrimaryKey(Long id);

    int insert(SysUser record);

    int insertSelective(SysUser record);

    SysUser selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(SysUser record);

    int updateByPrimaryKey(SysUser record);
}

service (业务接口 和 业务接口实现)

import com.zy.study.domain.SysUser;

/**
 * @author yanzy
 * @date 2021/6/15 21:53
 * @since v1.0
 */
public interface SysUserService {

    int deleteByPrimaryKey(Long id);

    int insert(SysUser record);

    int insertSelective(SysUser record);

    SysUser selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(SysUser record);

    int updateByPrimaryKey(SysUser record);
}
import com.zy.study.domain.SysUser;
import com.zy.study.mapper.SysUserMapper;
import com.zy.study.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author yanzy
 * @date 2021/6/15 21:54
 * @since v1.0
 */
@Service
@AllArgsConstructor
public class SysUserServiceImpl implements SysUserService {

    private final SysUserMapper sysUserMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SysUser record) {
        return sysUserMapper.insert(record);
    }

    @Override
    public int insertSelective(SysUser record) {
        return sysUserMapper.insertSelective(record);
    }

    @Override
    public SysUser selectByPrimaryKey(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SysUser record) {
        return sysUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SysUser record) {
        return sysUserMapper.updateByPrimaryKey(record);
    }
}

springboot配置

yml配置文件

spring:
  # 数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.31.10:3308/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    username: root
    password: root
# mybatis配置
mybatis:
  # mapper.xml映射路径
  mapper-locations: classpath:mapper/*.xml
  # 数据库表映射实体所在包路径
  type-aliases-package: com.zy.study.domain

启动类

// 启动类需增加扫描mapper接口的注解, 不然会报错
@MapperScan("com.zy.study.mapper")
@SpringBootApplication
public class StudyProject02Application {

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

测试

编写一个简单的测试类,这里使用springboot-test的方式测试,不需要web启动服务编写controller就可以完成测试。

@SpringBootTest
class StudyProject02ApplicationTests {
    // 注入实例
    @Autowired
    private SysUserService sysUserService;

    @Test
    void contextLoads() {
	// 调用查询方法, 验证数据源是否配置成功.
        SysUser sysUser = sysUserService.selectByPrimaryKey(1L);
        System.out.println(sysUser.toString());
    }
}

测试结果如下,可以正常获取到数据库数据,这里其他方法先不做验证,通过查询方法得知数据源已配置成功即可。
image.png

End

拜~

你可能感兴趣的:(Spring,数据库,java,数据库,mybatis,spring,mysql)