请查看官网开发文档:mybatis-plus官网
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mybatisdemo` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE mybatisdemo;
DROP TABLE IF EXISTS USER;
CREATE TABLE USER
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
DELETE FROM USER;
INSERT INTO USER (id, NAME, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
mysql
mysql-connector-java
org.projectlombok
lombok
1.18.20
com.baomidou
mybatis-plus-boot-starter
3.5.1
说明:使用mybatis-plus可以节省大量的代码,尽量不要同时导入mybatis和mybatis-plus
spring.datasource.driverClassName: com.mysql.cj.jdbc.Driver
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/mybatisdemo?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username: root
spring.datasource.password: 123456
pojo:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
mapper接口:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mybatis.mybatisdemo.pojo.User;
import org.springframework.stereotype.Repository;
// 在对应的mapper上面实现基本接口BaseMapper
@Repository //代表持久层
public interface UserMapper extends BaseMapper {
//所有的CRUD已经编写完成
//不需要写一大堆配置文件
}
以下是baseMapper底层代码,T即为传输进来的类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.baomidou.mybatisplus.core.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface BaseMapper extends Mapper {
int insert(T var1);
int deleteById(Serializable var1);
int deleteById(T var1);
int deleteByMap(@Param("cm") Map var1);
int delete(@Param("ew") Wrapper var1);
int deleteBatchIds(@Param("coll") Collection> var1);
int updateById(@Param("et") T var1);
int update(@Param("et") T var1, @Param("ew") Wrapper var2);
T selectById(Serializable var1);
List selectBatchIds(@Param("coll") Collection extends Serializable> var1);
List selectByMap(@Param("cm") Map var1);
default T selectOne(@Param("ew") Wrapper queryWrapper) {
List ts = this.selectList(queryWrapper);
if (CollectionUtils.isNotEmpty(ts)) {
if (ts.size() != 1) {
throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records", new Object[0]);
} else {
return ts.get(0);
}
} else {
return null;
}
}
default boolean exists(Wrapper queryWrapper) {
Long count = this.selectCount(queryWrapper);
return null != count && count > 0L;
}
Long selectCount(@Param("ew") Wrapper var1);
List selectList(@Param("ew") Wrapper var1);
List
使用:
import com.mybatis.mybatisdemo.mapper.UserMapper;
import com.mybatis.mybatisdemo.pojo.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
class MybatisdemoApplicationTests {
//继承了BaseMapper,所有的方法都来自自己的父类
//我们也可以编写自己的扩展方法
//如果自己有需要实现的方法,就写在Mapper接口里面
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
List users = userMapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
}
所有数据查出,搭建完成!