SpringBoot:整合Mybatis

1、导入 MyBatis 所需要的依赖

<!-- mybatis和SpringBoot的整合包:mybatis-spring-boot-starter -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.2.0</version>
		</dependency>

2、配置数据库连接信息

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/study?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #SpringBoot默认是不注入这些属性的,需要自己绑定
    #初始化连接数
    initial-size: 5
    #最小连接池数量
    minIdle: 5
    #最大连接池数量
    maxActive: 20
    #获取连接时最大等待时间,单位毫秒
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 30000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: false


    #配置监控统计拦截的filters,stat: 监控统计 log4j: 日志记录、wall:防御sql注入
    #如果允许时报错,java.lang.ClassNotFoundException:org.apache.log4j.Priority
    #则导入log4j依赖即可
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

3、测试数据库是否连接成功!

4、创建实体类

package com.zjb.po;


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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserPO {
    private int id;
    private String name;
    private String pwd;
}

5、创建mapper目录以及对应的 Mapper 接口

package com.zjb.mapper;

import com.zjb.po.UserPO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper //@Mapper : 表示本类是一个 MyBatis 的 Mapper
@Repository
public interface UserMapper {
    List<UserPO> getUserList();
}

6、在resources目录下创建对应的Mapper映射文件: 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.zjb.mapper.UserMapper">

    <select id="getUserList" resultType="UserPO">
        select * from user;
    </select>

</mapper>

7、yml文件整合mybatis

#整合mybatis
mybatis:
  type-aliases-package: com.zjb.po
  mapper-locations: classpath:mapper/*.xml

8、编写 UserController 进行测试

package com.zjb.controller;

import com.zjb.mapper.UserMapper;
import com.zjb.po.UserPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/getUsers")
    public List<UserPO> getUsers(){
        return userMapper.getUserList();
    }

}

9.启动项目访问进行测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值