学无先后,达者为师

网站首页 编程语言 正文

Mybatis-plus的快速使用

作者:宣布无人罪 更新时间: 2023-12-19 编程语言

01 7项前期准备

  • 引入依赖

  • 配置yml文件

  • 配置启动类

  • 创建实体类

  • 创建mapper接口继承BaseMapper类

  • 创建service接口继承IService类

  • 创建ServiceImpl实现类继承ServiceImp类和实现service接口

02 引入依赖

  • 在pom.xml中配置依赖

    <dependencies>
<!--其他要用的依赖-->
        <!--mybatis-plus需要的依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
    </dependencies>

03 配置yml文件

# 配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/需要连接的数据库?userSSL=false;serverTimezone=Asia/Shanghai
    #你的账号
    username: root
    #你的密码
    password: 123456
mybatis-plus:
  # mapper文件夹配置文件,在resource资源文件夹内建一个mapper的文件夹
  mapper-locations: classpath:mapper/*.xml
  # resultType别名,没有这个配置resultType包名要写全,配置后只要写类名
  type-aliases-package: com.example.springboot01.entity
  configuration:
    # 日志的配置,直接添加即可,内部有自带配置项
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #数据库下划线自动转驼峰配置
    map-underscore-to-camel-case: true

04 配置启动类

  • springboot自带一个启动类,在启动类配置注解进行扫描@MapperScan("com.mashang.demo.mapper")

  • 括号内为扫描路径,填入java文件夹中数据操作层的路径(就接下去要继承BaseMapper类的类所在的文件夹)

@SpringBootApplication
@MapperScan("com.mashang.demo.mapper")
public class DemoApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
​
}

05 创建实体类

  • 创建与数据库内表对应的实体类,这里我配置了在yml文件中配置了数据库下划线自动转驼峰,所以数据库中的下划线自动转成下一个单词首字母大写

public class Category {
    //@TableId用于标记数据库中的主键
    @TableId
    private Long categoryId;
    private String categoryName;
    private String categoryPicture1;
    private String categoryPicture2;
    //get和set方法,toString方法,空参构造器和全参数构造器记得重写,我使用了额外的插件,这里就不重写了
    
}

06 创建mapper接口继承BaseMapper类

  • 创建一个接口继承mybatis的BaseMapper<>类

  • 括号内的泛型要填需要操作的数据库对应的实体类也就是第5步创建实体类

@Repository//记得注入sprig
public interface CategoryMapper extends BaseMapper<Category> {
​
}

07 创建service接口继承IService类

  • 创建一个接口继承mybatis的IService<Category>

  • 括号内的泛型要填需要操作的数据库对应的实体类也就是第5步创建实体类

public interface ICategoryService extends IService<Category> {
}

08 创建ServiceImpl实现类继承ServiceImp类和实现service接口

  • 创建一个实体继承mybatis的ServiceImpl<Category>类

  • 括号内的泛型要填是第5步创建实体类和第6步创建的mapper接口

  • 还要实现(implements)第7部创建的service接口

@Service//同样的,要注入spring
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category>
    implements ICategoryService {
}

09 测试

  • 完成了7项前期准备后就可以开始使用了

  • 这里使用springboot自带的测试类test文件夹内DemoApplicationTests类进行测试

@SpringBootTest
class DemoApplicationTests {
    //首先注入第7步service接口
    @Autowired
    private ICategoryService iCategoryService;
​
    @Test
    void contextLoads() {
        //这时候mybatis-plus自动实现接口中的方法,可以直接调用
        List<Category> categories=iCategoryService.list();
    }
​
}
  • 将鼠标移到contextLoads()方法内, 右键启动Run'contextLoads()'

  • 当操作台打印出数据库对应的实体类数据时,说明成功

JDBC Connection [HikariProxyConnection@1958242673 wrapping com.mysql.cj.jdbc.ConnectionImpl@1f9d4b0e] will not be managed by Spring
==>  Preparing: SELECT category_id,category_name,category_picture1,category_picture2 FROM category
==> Parameters: 
<==    Columns: category_id, category_name, category_picture1, category_picture2
<==        Row: 1, 手机, null, null
<==        Row: 2, 电视机, null, null
<==        Row: 3, 空调, null, null
<==        Row: 4, 洗衣机, null, null
<==        Row: 5, 保护套, null, null
<==        Row: 6, 保护膜, null, null
<==        Row: 7, 充电器, null, null
<==        Row: 8, 充电宝, null, null
<==        Row: 11, 手办, null, null
<==      Total: 9
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@58c1da09]

原文链接:https://blog.csdn.net/2302_77182979/article/details/134405306

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新