一.快速创建
1.mybatis-plus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
2.mapper的编写(user是实体类对象)
public interface UserMapper extends BaseMapper<User> {
}
3.实体类 3.1 @TableName(“tb_user”) // 指定表名,进行数据库表的映射,如果前缀统一可以直接在全局中进行配置,这个注解可以不写 3.2 @TableField(“user_name”) 指定映射关系 实体类的属性名和数据库的字段名自动映射: * 名称一样 * 数据库字段使用_分割,实体类属性名使用驼峰名称 否则需要使用 @TableField(“user_name”) 指定映射关系 3.3 忽略某个字段的查询和 插入 @TableField(exist = false) 3.4 设置id生成策略:AUTO 数据库自增 @TableId(type = IdType.AUTO) 3.5 yam中的全局配置
mybatis-plus:
global-config:
db-config:
# 表名前缀
table-prefix: tb_
# id生成策略 数据库自增
id-type: auto
@TableName("tb_user")
@Data
public class User {
private Long id;
@@TableField("user_name")
private String userName;
private String password;
4.启动类
@MapperScan("com.xxxx.xx.mapper")
@SpringBootApplication
public class MybatisPlusSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusSpringbootApplication.class, args);
}
}
5.测试类
@SpringBootTest
@RunWith(SpringRunner.class)
class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectById() {
User user = userMapper.selectById(1L);
System.out.println(user);
}
}
CRUD
@SpringBootTest
@RunWith(SpringRunner.class)
class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectById() {
User user = userMapper.selectById(1L);
System.out.println(user);
}
@Test
public void testInsert() {
User user = new User();
user.setUserName("1111");
user.setPassword("1111");
int i= userMapper.insert(user);
System.out.println(i);
}
@Test
public void testDelete() {
Map<String, Object> map = new HashMap<>();
map.put("user_name","zhangsan");
map.put("age","18");
userMapper.deleteByMap(map);
}
@Test
public void testUpdateById() {
User user = new User();
user.setId(2L);
user.setPassword("1111111");
int count = userMapper.updateById(user);
}
@Test
public void testSelectPage() {
int current = 1;
int size = 2;
IPage<User> page = new Page(current,size);
userMapper.selectPage(page,null);
List<User> records = page.getRecords();
long pages = page.getPages();
long total = page.getTotal();
System.out.println(records);
System.out.println(pages);
System.out.println(total);
}
}
查询
1.分页查询的拦截器,,,,因为我们都使用的mybatis-puls是3.4.0的所以得使用下面的拦截器,上面的会显示过时
@Configuration
public class PageConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
2.Wrapper的讲解
2.1因为Wrapper是一个抽象类,所以在使用的使用查询和更新用属于自己的专用的对象 2.2 Wrapper的作用就是在书写mysql,通过点对象的形式在进行的书写,关于点对象,我将图贴在下面
查询: QueryWrapper、LambdaQueryWrapper
QueryWrapper<User> wrapper = new QueryWrapper<>();
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
更新: UpdateWrapper、LambdaUpdateWrapper
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
模糊查询 select这个就是,你可以定义你自己查询的字段,提高数据库的查询效率
wrapper.eq("user_name","lisi")
.or()
.lt("age",23)
.in("name","李四","王五")
.orderByDesc("age")
.select("id","user_name");
分页查询
@Test
public void testWrapper6(){
int current = 1;
int size = 2;
Page<User> page = new Page<>(current,size);
QueryWrapper<User> wrapper = new QueryWrapper();
wrapper.lt("age",23);
userMapper.selectPage(page,wrapper);
List<User> records = page.getRecords();
long total = page.getTotal();
long pages = page.getPages();
System.out.println(records);
System.out.println(total);
System.out.println(pages);
}
== LambdaQueryWrapper:消除代码中的硬编码 ==
@Test
public void testWrapper7(){
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getUserName,"zhangsan");
userMapper.selectOne(wrapper);
}
service 封装
逆向工程
这个看官网就行了
|