欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot整合Mybatispuls的实例详解

程序员文章站 2022-03-08 21:40:46
springboot整合mybatispulsmaven导入依赖,主要只需导入mybatispuls org.springf...

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源码

Springboot整合Mybatispuls的实例详解

实现接口方法

@restcontroller
public class control {
  @autowired
  mapper mapper;
  @requestmapping("/hello")
  public users select(){
    users users = mapper.selectbyid(1);
    return users;
  }

}

到此这篇关于springboot整合mybatispuls的文章就介绍到这了,更多相关springboot整合mybatispuls内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!