SpringBoot中如何使用注解方式整合Mybatis? | 带你读《SpringBoot实战教程》之二十一

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 本节介绍了SpringBoot中如何使用注解方式去整合Mybatis,并实现查找用户和添加用户。

上一篇:SpringBoot中如何使用xml方式整合Mybatis? | 带你读《SpringBoot实战教程》之二十
下一篇:如何区分多数据源? | 带你读《SpringBoot实战教程》之二十二

本文来自于千锋教育在阿里云开发者社区学习中心上线课程《SpringBoot实战教程》,主讲人杨红艳,点击查看视频内容

SpringBoot中整合Mybatis(注解方式)

添加依赖:

<!-- springboot整合mybatis,注解版 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
       
<!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

依然使用db1数据库中的users表。
创建全局配置文件:application.properties,添加连接数据库的信息:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/db1

逆向生成com.db1.pojo,创建实体类。

image.png

mapper:

public interface UsersMapper {

    @Select("select * from users where name=#{name}")
    Users findUserByName(@Param("name") String name);

    @Insert("insert into users(name, password) values(#{name}, #{password})")
    void addUser(@Param("name") String name, @Param("password")String password);
}

UsersService:

public interface UsersService {

    Users findUser(String name);

    void saveUser(Users user);
}

UsersServiceImpl:

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public Users findUser(String name) {
        return usersMapper.findUserByName(name);
    }

    @Override
    public void saveUser(Users user) {
        usersMapper.addUser(user.getName(), user.getPassword());
    }

}

UsersController:

@RestController
public class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping("/findUser")
    //@ResponseBody
    public Users findUser(String name) {
        return usersService.findUser(name);
    }

    @RequestMapping("/addUser")
    //@ResponseBody
    public String addUser() {
 
        Users user = new Users();
        user.setName("王小二");
        user.setPassword("9999");
        usersService.saveUser(user);
        return "ok";
    }
}

在启动类中添加所有需要扫描的包,mapper需要单独扫描

@SpringBootApplication(scanBasePackages="com.qianfeng")
@MapperScan("com.db1.mapper")

查找用户执行结果:

image.png

添加用户执行结果:

image.png
image.png

配套视频

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1天前
|
Java 关系型数据库 MySQL
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
9 4
|
20小时前
|
Java 数据库连接 数据库
Springboot整合mybatis注解版(202005)
Springboot整合mybatis注解版(202005)
9 3
|
1天前
|
SQL Java 数据库连接
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
8 2
|
1天前
|
安全 前端开发 Java
挑战5分钟内基于Springboot+SpringMVC+Mybatis-plus快速构建web后端三层架构
挑战5分钟内基于Springboot+SpringMVC+Mybatis-plus快速构建web后端三层架构
6 1
|
1天前
|
Java 程序员
浅浅纪念花一个月完成Springboot+Mybatis+Springmvc+Vue2+elementUI的前后端交互入门项目
浅浅纪念花一个月完成Springboot+Mybatis+Springmvc+Vue2+elementUI的前后端交互入门项目
11 1
|
15小时前
|
Java
springboot自定义log注解支持EL表达式
springboot自定义log注解支持EL表达式
5 0
|
16小时前
|
Java 关系型数据库 MySQL
3.MyBatis和SpringBoot整合及MyBatis-plus与SpringBoot整合
3.MyBatis和SpringBoot整合及MyBatis-plus与SpringBoot整合
|
16小时前
|
缓存 Java 编译器
1.SpringBoot01-自定义注解
1.SpringBoot01-自定义注解
|
20小时前
|
Java Spring
spring基于注解配置数据源
spring基于注解配置数据源
7 0
|
3天前
|
Java Maven Spring
Spring中AOP最简单实例-@注解形式
Spring中AOP最简单实例-@注解形式
15 0