Springboot整合Mybatispuls Springboot整合Mybatispuls的实例详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Springboot整合Mybatispuls Springboot整合Mybatispuls的实例详解

康浩鹏   2021-03-15 我要评论
想了解Springboot整合Mybatispuls的实例详解的相关内容吗,康浩鹏在本文为您仔细讲解Springboot整合Mybatispuls的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Springboot整合Mybatispuls,Springboot,Mybatis,puls,下面大家一起来学习吧。

Springboot整合MybatisPuls

Maven导入依赖,主要只需导入MyBatisPuls

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

配置数据源

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
server.port=8082

编写实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("users")//连接的表名
public class Users implements Serializable {
  @TableId("id")标记该变量为主键
  private Integer id;
  private String Account;
  @TableField("passwraod" )//如果实体类变量和数据库不同使用
  private String password;
  private Integer Authority;
}

mapper接口编写
继承BaseMapper<这里为实体类>

@org.apache.ibatis.annotations.Mapper//让Spring容器扫描该类为Mapper
@Repository
public interface Mapper extends BaseMapper<Users> {
}

BaseMapper源码

在这里插入图片描述

实现接口方法

@RestController
public class Control {
  @Autowired
  Mapper mapper;
  @RequestMapping("/hello")
  public Users Select(){
    Users users = mapper.selectById(1);
    return users;
  }

}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们