IDEA:IntelliJ IDEA 2021.3.3
JDK:14.0.2
MySql:
SpringBoot:2.6.7
MyBatis_Plus:3.5.1
创建表和插入数据的sql语句
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE `mybatis_plus`;
CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL COMMENT '主键ID',
`name` VARCHAR(30) DEFAULT NULL COMMENT '姓名',
`age` INT(11) DEFAULT NULL COMMENT '年龄',
`email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
创建实体类:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private long id;
private String name;
private Integer age;
private String email;
}
(3)创建SpringBoot工程
导入依赖
com.baomidou
mybatis-plus-boot-starter
3.5.1
org.projectlombok
lombok
true
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
配置SpringBoot的application.yml
spring:
#配置数据源信息
datasource:
#配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
#配置链接数据库的各个信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false
username: root
password: root
写mapper接口继承BaseMapper,并且启动类扫描注意:
1.驱动类driver-class-name:
spring boot 2.0(内置jdbc5驱动),驱动类使用:
driver-class-name: com.mysql.jdbc.Driver
spring boot 2.1及以上(内置jdbc8驱动),驱动类使用:driver-class-name: com.mysql.cj.jdbc.Driver
否则运行测试用例的时候会有 WARN 信息
2.连接地址url MySQL5.7版本的url:
jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
MySQL8.0版本的url:jdbc:mysql://localhost:3306/mybatis_plus?
serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
否则运行测试用例报告如下错误:
java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more
@Component
public interface UserMapper extends BaseMapper {
}
@SpringBootApplication
@MapperScan("com.example.mybatisplus_demo.mapper")//扫描mapper接口所在的包
public class MyBatisPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisPlusDemoApplication.class, args);
}
}
(4)测试
@SpringBootTest
class MyBatisPlusDemoApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
List users = userMapper.selectList(null);
System.out.println(users);
}
}
(5)[拓展]添加日志功能
其实只需要在配置文件中加上配置信息就行
2.增删改查功能的实现 (1)Dao层基础的单表CRUD:mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Autowired
UserMapper userMapper;
@Test//查询记录
void test4() {
/*共10种,先测试3种,带Wrapper条件的先不测*/
User user = userMapper.selectById(1L);//第一种
System.out.println(user);
List ids = Arrays.asList(3L, 4L);//第二种,SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
List users = userMapper.selectBatchIds(ids);
System.out.println(users);
Map map=new HashMap<>();map.put("name","萧萧");map.put("age",2000);
userMapper.selectByMap(map);//第三种
}
@Test//修改记录
void test3() {
/*共两种,先测试一种,带Wrapper条件的先不测*/
User user = new User(1L,"萧萧", 2000, "xiaoxiao@123.com");
userMapper.updateById(user);
}
@Test//删除记录
void test2() {
/*共五种,先测试四种,带Wrapper条件的先不测*/
userMapper.deleteById(1);//第一种
userMapper.deleteById(new User(2L));//第二种
Map map=new HashMap<>();map.put("name","萧萧");map.put("age",2000);
userMapper.deleteByMap(map);//第三种
List ids = Arrays.asList(3L, 4L);
userMapper.deleteBatchIds(ids);//第四种:DELETE FROM user WHERE id IN ( ? , ? )
}
@Test//添加记录
void test1() {
User user = new User("萧萧", 2000, "xiaoxiao@123.com");
int result = userMapper.insert(user);//会增加一条记录,并且自动生成id,将id赋值给该对象的id属性
System.out.println(result);
System.out.println("id"+user.getId());//id是雪花算法自动生成的,
}
(2)自定义功能
这里的步骤就跟Mybatis中一样了,但是有几个点要注意。首先mapper映射文件有默认的路径(也就是在resources/mapper下写映射文件),可以在yml配置文件中进行自己配置。
@Test//自定义
void test5() {
Map stringObjectMap = userMapper.selectMapById(1L);
System.out.println(stringObjectMap);
}
(3)Service层封装的单表CRUD:
继承和实现体系
在IService
如果我们想要使用这个,我们需要新建一个接口UserService去继承IService
//继承接口
public interface UserService extends IService {
}
//实现自定义接口
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
}
简单的测试
@Autowired
UserServiceImpl userService;
@Test//简简单单,只测试两个方法
void test() {
//查询总记录数
long count = userService.count();
System.out.println(count);
//批量添加
List list =new ArrayList<>();
for(int i=1;i<=5;i++){
User user = new User();
user.setName("xiaoxiao"+i);
user.setAge(20+i);
list.add(user);
}
boolean b = userService.saveBatch(list);
System.out.println(b);
}
3.常用注解
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)