无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作,BaseMapper
强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求,以后简单的CRUD操作,不用自己编写了 !
内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.11version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.0.5version>
dependency>
项目配置文件:application.properties
#数据库连接配置
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=root
#数据库链接
#useSSL、allowPublicKeyRetrieval要配置
spring.datasource.url=jdbc:mysql://localhost:3306/库名字?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
#驱动名
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
}
@Repository
注解BaseMapper<实体类>
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wsk.pojo.User;
import org.springframework.stereotype.Repository;
//在对应的接口上面继承一个基本的接口 BaseMapper
@Repository//代表持久层
public interface UserMapper extends BaseMapper<User> {
//所有CRUD操作都编写完成了,不用像以前一样配置一大堆文件
}
在主启动类添加@MapperScan注解
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//扫描mapper包下的所有接口,com.xxxx.mapper改成你的mapper目录
@MapperScan("com.xxxx.mapper")
@SpringBootApplication
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
通常编写@Service
注解的类,在类中进行操作
@Resource
userMapper userMapper;
User user = new User();
user.setName("live");
user.setAge(22);
Integer result = userMapper.insert(user); //会帮我们自动生成id
System.out.println(result); //受影响的行数
System.out.println(user); //通过日志发现id会自动回填
通常编写@Service
注解的类,在类中进行操作
@Resource
userMapper userMapper;
HashMap<String, Object> map = new HashMap<>();
map.put("age","18");
map.put("name","lol");
userMapper.deleteByMap(map);
通常编写@Service
注解的类,在类中进行操作
@Resource
userMapper userMapper;
User user = new User();
user.setId(2L);//怎么改id??
//通过条件自动拼接动态Sql
user.setName("root");
user.setAge(12);
int i = userMapper.updateById(user);//updateById,但是参数是个user
System.out.println(i);
@Resource
userMapper userMapper;
User user = userMapper.selectById(1L);
System.out.println(user);
@Resource
userMapper userMapper;
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));
users.forEach(System.out::println);
@Resource
userMapper userMapper;
//参数是一个wrapper ,条件构造器,和刚才的map对比学习!
//查询name不为空,age大于18的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.isNotNull("name")
.ge("age",18);
List<User> userList = userMapper.selectList(wrapper);
@Resource
userMapper userMapper;
//查询name=aaa的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","aaa");
//查询一个数据selectOne,若查询出多个会报错
//Expected one result (or null) to be returned by selectOne(), but found: *
//若出现多个结果使用list或map
User user = userMapper.selectOne(wrapper);//查询一个数据,若出现多个结果使用list或map
System.out.println(user);
@Resource
userMapper userMapper;
//查询age在10-20之间的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age", 10, 20);//区间
Integer count = userMapper.selectCount(wrapper);//输出查询的数量selectCount
System.out.println(count);
@Resource
userMapper userMapper;
QueryWrapper<User> wrapper = new QueryWrapper<>();
//通过id进行降序排序
wrapper.orderByDesc("id");
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
@Resource
userMapper userMapper;
//模糊查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.notLike("name","s")
.likeRight("email","t");//qq% 左和右?
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
maps.forEach(System.out::println);
@Resource
userMapper userMapper;
//模糊查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
//id 在子查询中查出来
wrapper.inSql("id","select id from user where id<5");
List<Object> objects = userMapper.selectObjs(wrapper);
objects.forEach(System.out::println);