<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.1.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
dependency>
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mbplus?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
@MapperScan("psers.zhang.demo.mapper")
@Configuration
public class MyBatisPlusConfig {
//配置分页插件
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
//指定主键为自增
@TableId(type = IdType.AUTO)
private Long id;
//指定数据表中字段名
@TableField(value = "user_name")
private String userName;
private String password;
private String name;
private Integer age;
private String email;
//指定该字段在表中是不存在的
@TableField(exist = false)
private String address;
}
public interface UserMapper extends BaseMapper<User> {
}
insert:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
class CRUDTest {
@Autowired
UserMapper userMapper;
/*
测试插入操作:int insert(T t)
*/
@Test
public void testInsert(){
User user = new User();
user.setEmail("[email protected]");
user.setAge(30);
user.setName("xiaoqiang");
user.setPassword("123456");
user.setUserName("小强");
user.setAddress("北京");
//返回受影响的行数
int rows = userMapper.insert(user);
System.out.println(rows);
//获取自增长后的id值,MP会自动回填自增后的id
System.out.println(user.getId());
}
}
update:
/*
更新操作:
1. 根据id更新 int updateById(@Param(Constants.ENTITY) T entity);
2. 根据条件更新 int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);
*/
@Test
public void testUpdate(){
User user = new User();
user.setId(6l);
user.setEmail("[email protected]");
//修改年龄为99
user.setAge(99);
user.setName("xiaoqiang");
user.setPassword("123456");
user.setUserName("小强");
//返回受影响的行数
int rows = userMapper.updateById(user);
System.out.println(rows);
//第一种方式:
User user1 = new User();
user1.setName("条件更新");
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("age", "99");
//根据条件更新
int rows1 = userMapper.update(user1, wrapper);
System.out.println(rows1);
//第二种方式:
UpdateWrapper<User> wrapper1 = new UpdateWrapper<>();
//更新的字段,key为表中字段名
wrapper1.set("name", "条件更新2").set("password", "9999999");
//更新条件
wrapper1.eq("age", "99");
int rows2 = userMapper.update(null, wrapper1);
System.out.println(rows2);
}
delete:
/*
删除操作:
根据id删除:int deleteById(Serializable id);
*/
@Test
public void testDeleteById(){
int rows = userMapper.deleteById(6l);
System.out.println(rows);
}
/*
删除操作:
根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
*/
@Test
public void testDeleteByMap(){
Map<String, Object> map = new HashMap<>();
map.put("user_name", "xiaoqiang");
map.put("password", "123456");
//多条件之间是and关系
int rows = userMapper.deleteByMap(map);
System.out.println(rows);
}
/*
删除操作:
根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper wrapper);
*/
@Test
public void testDelete(){
//用法1
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("user_name", "wangwu").eq("password", "123456");
int rows1 = userMapper.delete(wrapper);
//用法2
User user = new User();
user.setPassword("123456");
user.setUserName("zhangsan");
QueryWrapper<User> wrapper1 = new QueryWrapper<>(user);
//根据包装条件删除
int rows2 = userMapper.delete(wrapper1);
System.out.println(rows2);
}
/*
删除操作:
删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
*/
@Test
public void testDeleteBatchIds(){
int rows = userMapper.deleteBatchIds(Arrays.asList(7l, 8l, 9l));
System.out.println(rows);
}
/*
查询操作:
根据id查询:T selectById(Serializable id);
*/
@Test
public void testSelectById(){
User user = userMapper.selectById(2l);
System.out.println(user);
}
select:
/*
查询操作:
根据id查询:T selectById(Serializable id);
*/
@Test
public void testSelectById(){
User user = userMapper.selectById(2l);
System.out.println(user);
}
/*
查询操作:
查询(根据ID 批量查询)
List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
*/
@Test
public void testSelectBatchIds(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(2l, 4l));
for (User u : users)
System.out.println(u);
}
/*
查询操作:
根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
public void testSelectOne(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
//查询条件
wrapper.eq("user_name", "lisi");
User user = userMapper.selectOne(wrapper);
//数据不存在返回null,如果数据有多条则报错
System.out.println(user);
}
/*
查询操作:
根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
public void testSelectCount(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age", "1");//年龄大于1岁
Integer count = userMapper.selectCount(wrapper);
System.out.println(count);
}
/*
查询操作:
根据 entity 条件,查询全部记录
List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
public void testSelectList(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age", "1");//查询年龄大于1岁
List<User> users = userMapper.selectList(wrapper);
for (User u : users)
System.out.println(u);
}
/*
查询操作:
根据 entity 条件,查询全部记录(并翻页)
IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
public void testSelectPage(){
//设置分页参数 查询第一页,每页显示1条数据
Page<User> page = new Page<>(1, 1);
//设置查询条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age", "1");
IPage<User> pages = userMapper.selectPage(page, wrapper);
List<User> users = pages.getRecords();
for (User u : users)
System.out.println(u);
}