【SpringBoot整合Mybatis】简单代码示例

测试SpringBoot整合Mybatis

首先创建SpringBoot项目

引入mybatis启动器


<dependencies>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

数据库表结构
在这里插入图片描述
根据表结构创建对应的实体类


@Data
public class User {
    private Integer user_id;
    private String name;
    private String sex;
    private String age;
}

创建UserController


import com.example.springboot_mybatis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("getUser/{user_id}")
    public String GetUser(@PathVariable("user_id") int user_id){
        String userToString = userService.Sel(user_id).toString();
        log.info(userToString);
        return userToString;
    }
}

创建UserService


import com.example.springboot_mybatis.bean.User;
import com.example.springboot_mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    public UserMapper userMapper;

    public User Sel(int user_id){
        return userMapper.Sel(user_id);
    }
}

创建UserMapper


import com.example.springboot_mybatis.bean.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {

    public User Sel(int user_id);
}

创建UserMapper.xml文件,配置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="com.example.springboot_mybatis.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>

    <select id="Sel" resultType="User">
        select * from user where user_id = #{user_id}
    </select>
</mapper>

在启动类上添加扫描路径


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

修改application.yaml全局配置文件


server:
  port: 8080

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/mybatis_test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.example.springboot_mybatis.bean
#showSql
logging:
  level:
    com:
      example:
        mapper : info

至此,一个简单的SpringBoot框架整合MyBatis框架完成,项目结构如下
在这里插入图片描述
运行启动类,在浏览器输入http://localhost:8080/getUser/1,浏览器显示如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值