SpringBoot3-集成mybatis

1、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu</groupId>
    <artifactId>pro29-springboot-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <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>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

2、 application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///springboot
    username: root
    password: 123456
mybatis:
  mapper-locations: classpath:/mapper/*.xml
  type-aliases-package: com.atguigu.pojo

3、 创建数据库和表

create database springboot;
use springboot;
create table user (
 id int primary key,
 username varchar(100),
 address varchar(100)
);

insert into user values(11,'lucy','China');

4、User.java 

package com.atguigu.pojo;
import lombok.Data;
@Data
public class User {
    private Integer id;
    private String username;
    private String address;
}

5、UserMapper.java 

package com.atguigu.mapper;
import com.atguigu.pojo.User;
public interface UserMapper {
    User getUser(Integer id);
}

6、 mapper/UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace等于mapper接口类的全限定名,这样实现对应 -->
<mapper namespace="com.atguigu.mapper.UserMapper">

    <!-- 查询使用 select标签
            id = 方法名
            resultType = 返回值类型
            标签内编写SQL语句
     -->
    <select id="getUser" resultType="User">
        select * from user
    </select>
</mapper>

7、 UserService.java

package com.atguigu.service;
import com.atguigu.pojo.User;
public interface UserService {
    User getUser(Integer id);
}

8、UserServiceImpl.java 

package com.atguigu.service.impl;
import com.atguigu.mapper.UserMapper;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUser(Integer id) {
        return userMapper.getUser(id);
    }
}

 9、UserController.java

package com.atguigu.controller;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User h01(@PathVariable Integer id) {
        return userService.getUser(id);
    }
}

10、MyApplication.java

package com.atguigu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.atguigu.mapper")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class);
    }
}

 

MyBatis-Spring Boot Starter 是一个 MyBatis 和 Spring Boot 的集成库,它可以帮助开发者更方便地在 Spring Boot 应用程序中使用 MyBatis。这个 Starter 提供了对 MyBatis 的基本配置和整合,包括对 MyBatis 的映射器、SqlSessionFactory、SqlSessionTemplate 等的自动配置,同时还提供了对 MyBatis 的事务管理的支持。

使用 MyBatis-Spring Boot Starter,你可以在 Spring Boot 项目中轻松地使用 MyBatis 进行 ORM 映射,而无需手动配置许多基本的设置。这可以帮助你节省时间,同时避免许多常见的配置错误。

在项目中如何使用它主要取决于你的项目类型和你想要达到的效果。一般情况下,你可以通过在项目的 Maven 或 Gradle 配置文件中添加相应的依赖,然后在你的 Spring Boot 项目中引入这个依赖,MyBatis-Spring Boot Starter 就会自动配置你的 MyBatis 和 Spring Boot 项目。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值