一、 SpringBoot整合MyBatisPlus
SpringBoot整合MyBatisPlus 入门案例:
步骤一:
创建新模块,选择Spring初始化,并配置模块相关基础信息。选择当前模块需要使用的技术集(仅保留JDBC)
手动添加MyBatisPlus起步依赖:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>X.X.X</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>X.X.X</version> </dependency>
步骤二:制作实体类与表结构
create database if not exists mybatisplus_db character set utf8; use mybatisplus_db; CREATE TABLE user ( id bigint(20) primary key auto_increment, name varchar(32) not null, password varchar(32) not null, age int(3) not null , tel varchar(32) not null ); insert into user values(null,'tom','123456',12,'12345678910'); insert into user values(null,'jack','123456',8,'12345678910'); insert into user values(null,'jerry','123456',15,'12345678910'); insert into user values(null,'tom','123456',9,'12345678910'); insert into user values(null,'snake','123456',28,'12345678910'); insert into user values(null,'张益达','123456',22,'12345678910'); insert into user values(null,'张大炮','123456',16,'12345678910');
public class User { private Long id; private String name; private String password; private Integer age; private String tel; //自行添加getter、setter、toString()等方法 }
步骤三:设置Jdbc参数(application.yml)
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC username: root password: 123456
步骤四:定义数据接口,继承BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.it.domain.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserDao extends BaseMapper<User> { }
步骤五:测试类中注入dao接口,测试功能
package com.it; import com.it.dao.UserDao; import com.it.domain.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest public class Mybatisplus01QuickstartApplicationTests { @Autowired private UserDao userDao; @Test void testGetAll() { List<User> userList = userDao.selectList(null); System.out.println(userList); } }
运行单元测试,查看结果。
标签:insert,123456,MybatisPlus,入门,案例,user,import,null,com From: https://www.cnblogs.com/kongsq/p/18504610