//设置主键自增
@TableId(type = IdType.AUTO)
private Long id;
@Test
void primary() {
User user = new User();
user.setName("Mary");
user.setAge(30);
user.setEmail("[email protected]");
userMapper.insert(user);
}
//手动设置主键值
@TableId(type = IdType.INPUT)
private Long id;
@Test
void primary() {
User user = new User();
user.setId(8L);
user.setName("Mary");
user.setAge(30);
user.setEmail("[email protected]");
userMapper.insert(user);
}
垂直拆分就是将一个大的表按照字段进行拆分
其实对于拆分后的数据,有三点需求,以水平拆分为例:
那就需要有一种算法,能够实现这三个需求,这个算法就是雪花算法。
雪花算法是由一个64位的二进制组成的,最终就是一个Long类型的数值。主要分为四部分存储
//使用雪花算法,默认为type = IdType.ASSIGN_ID,可省略
@TableId(type = IdType.ASSIGN_ID)
private Long id;
@Test
void primary() {
User user = new User();
user.setName("Mary");
user.setAge(30);
user.setEmail("[email protected]");
userMapper.insert(user);
}
//不指定主键生成策略
@TableId(type = IdType.NONE)
private Long id;
#主键生成策略全局配置(默认为assign_id)
mybatis-plus.global-config.db-config.id-type=assign_id
//主键采用ASSIGN_UUID策略
@TableId(type = IdType.ASSIGN_UUID)
private String id;
@Test
void primary() {
User user = new User();
user.setName("Mary");
user.setAge(30);
user.setEmail("[email protected]");
userMapper.insert(user);
}
1.通过配置类来指定一个具体的数据库的分页插件,自动生成分页语句
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
2.实现分页效果查询
@Test
void selectPage() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//指定分页对象,分页对象包含分页信息 IPage
Page<User> userPage = new Page<>(2, 3);
//执行查询
userMapper.selectPage(userPage, lambdaQueryWrapper);
//获取分页查询的信息
System.out.println("当前页:" + userPage.getCurrent());
System.out.println("每页显示条数:" + userPage.getSize());
System.out.println("总页数:" + userPage.getPages());
System.out.println("总条数:" + userPage.getTotal());
System.out.println("分页数据:" + userPage.getRecords());
}
1.在UserMapper.xml映射配置文件中提供查询语句
<mapper namespace="com.hhb.mp02.mapper.UserMapper">
<select id="selectByName" resultType="com.hhb.mp02.domain.User" parameterType="String">
select * from t_user where username=#{name}
select>
mapper>
2.在Mapper接口中提供对应的方法,方法中将Page对象作为参数传入
@Mapper
public interface UserMapper extends BaseMapper<User> {
Page<User> selectByName(Page<User> page, String name);
}
3.实现分页查询效果
@Test
void selectPage2() {
Page<User> userPage = new Page<>(1, 2);
userMapper.selectByName(userPage,"Mary");
System.out.println("当前页:" + userPage.getCurrent());
System.out.println("每页显示条数:" + userPage.getSize());
System.out.println("总页数:" + userPage.getPages());
System.out.println("总条数:" + userPage.getTotal());
System.out.println("分页数据:" + userPage.getRecords());
}
1.实体类继承Model
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User extends Model<User> {
private Long id;
@TableField("username")
private String name;
private Integer age;
private String email;
@TableField("`desc`")
private String desc;
}
2.测试ActiveRecord模式的增删改查
@SpringBootTest
public class ActiveRecordTest {
//添加操作
@Test
void activeRecordAdd() {
User user = new User();
user.setName("张三");
user.setEmail("[email protected]");
user.setAge(33);
user.insert();
}
//删除操作
@Test
void activeRecordDelete() {
User user = new User();
user.setId(9L);
user.deleteById();
}
//修改操作
@Test
void activeRecordUpdate() {
User user = new User();
user.setId(10L);
user.setName("李四");
user.setAge(26);
user.setEmail("[email protected]");
user.updateById();
}
//查询操作
@Test
void activeRecordSelect() {
User user = new User();
user.selectAll();
}
}
基于字段封装集合
@Test
void testList() {
List<Long> list = SimpleQuery.list(new LambdaQueryWrapper<User>().eq(User::getName, "Mary"), User::getId);
System.out.println(list);
}
对于封装后的字段进行lambda操作
@Test
void testList2() {
List<String> list = SimpleQuery.list(new LambdaQueryWrapper<User>().eq(User::getName, "Mary"), User::getName, new Consumer<User>() {
@Override
public void accept(User user) {
//map转化
Optional.of(user.getName()).map(String::toLowerCase).ifPresent(user::setName);
}
});
System.out.println(list);
}
将所有的对象以id=实体类的方式封装为Map集合
@Test
void testMap() {
Map<Long, User> map = SimpleQuery.keyMap(new LambdaQueryWrapper<User>(), User::getId);
System.out.println(map);
}
将单个对象以id=实体类的方式封装为Map集合
@Test
void testMap2() {
Map<Long, User> map = SimpleQuery.keyMap(new LambdaQueryWrapper<User>().eq(User::getId, 1L), User::getId);
System.out.println(map);
}
由id和name组成的map
@Test
void testMap3() {
Map<Long, String> map = SimpleQuery.map(new LambdaQueryWrapper<User>(), User::getId, User::getName);
System.out.println(map);
}
@Test
void testGroup() {
Map<String, List<User>> maps = SimpleQuery.group(new LambdaQueryWrapper<User>(), User::getName);
for (Map.Entry<String, List<User>> stringListEntry : maps.entrySet()) {
System.out.println(stringListEntry);
}
}