测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestMyBatisSpringBoot {
@Autowired
private UserMapper userMapper;
}
插入操作
@Test
public void testInsert(){
User user = new User();
user.setAge(30);
user.setMail("@qq.com");
user.setUserName("ssc");
user.setName("曹操");
user.setPassword("123456");
int count = this.userMapper.insert(user);
System.out.println(count);
}
注解
TableId 主键id的属性
@TableId(type = IdType.AUTO)
private Long id
public enum IdType {
AUTO(0),
NONE(1),
INPUT(2),
ID_WORKER(3),
UUID(4),
ID_WORKER_STR(5);
private final int key;
private IdType(int key) {
this.key = key;
}
public int getKey() {
return this.key;
}
}
TableField
指定数据库字段名
- 指定数据库中的字段名,mail在数据库中没有对应的字段,可以用
TableField(value = "email") 指定
@TableField(value = "email")
private String mail;
表中不存在的字段
@TableField(exist = false)
private String address;
不查询某个字段
@TableField(select = false)
private String password;
更新操作
根据id更新
@Test
public void testUpdateById(){
User user = new User();
user.setId(1L);
user.setAge(19);
int i = this.userMapper.updateById(user);
System.out.println("受影响条数:"+ i);
}
根据条件更新
@Test
public void testUpdateByCondition(){
User user = new User();
user.setAge(20);
user.setPassword("888888");
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("user_name","zhangsan");
int i = this.userMapper.update(user, wrapper);
System.out.println("受影响条数:"+ i);
}
@Test
public void testUpdateByCondition1(){
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.set("age", 21).set("password","999999")
.eq("user_name","zhangsan");
int i = this.userMapper.update(null, wrapper);
System.out.println("受影响条数:"+ i);
}
删除操作
根据id删除
@Test
public void testDeleteById(){
int i = this.userMapper.deleteById(8);
System.out.println("受影响条数:"+ i);
}
根据columnMap条件,删除
@Test
public void testDeleteByMap(){
Map<String ,Object> map = new HashMap<>();
map.put("user_name","ssc");
map.put("password","123456");
int i = this.userMapper.deleteByMap(map);
System.out.println("受影响条数:"+ i);
}
根据entity条件,删除
@Test
public void testDeleteByWrapper(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("user_name","ssc").eq("password","123456");
int i = this.userMapper.delete(wrapper);
System.out.println("受影响条数:"+ i);
}
@Test
public void testDeleteByWrapper(){
User user = new User();
user.setUserName("ssc1");
user.setPassword("123456");
QueryWrapper<User> wrapper = new QueryWrapper<>(user);
int i = this.userMapper.delete(wrapper);
System.out.println("受影响条数:"+ i);
}
根据id,批量删除
@Test
public void testDeleteBatchId(){
int i = this.userMapper.deleteBatchIds(Arrays.asList(8L,9L));
System.out.println("受影响条数:"+ i);
}
DELETE FROM tb_user WHERE id IN ( ? , ? )
查询操作
id查询
@Test
public void testSelectById(){
User user = this.userMapper.selectById(1);
System.out.println(user);
}
查询全部
@Test
public void testFindAll(){
List<User> users = this.userMapper.selectList(null);
users.forEach(user -> System.out.println(user));
}
id批量查询
@Test
public void testSelectBatchId(){
List<User> users = this.userMapper.selectBatchIds(Arrays.asList(1L, 2L));
users.forEach(user -> System.out.println(user));
}
entity查询一条记录
@Test
public void testSelectByWrapper(){
User user = new User();
user.setUserName("zhangsan");
user.setPassword("999999");
QueryWrapper<User> wrapper = new QueryWrapper<>(user);
User user1 = this.userMapper.selectOne(wrapper);
System.out.println(user1);
}
@Test
public void testSelectByWrapper(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("user_name","zhangsan").eq("password","999999");
User user1 = this.userMapper.selectOne(wrapper);
System.out.println(user1);
}
SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE user_name = ? AND password = ?
根据wrapper条件,查询总记录条数
@Test
public void testSelectCount(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("password","123456").gt("age",20);
Integer integer = this.userMapper.selectCount(wrapper);
System.out.println(integer);
}
SELECT COUNT( 1 ) FROM tb_user WHERE password = ? AND age > ?
entity查询多条列表
@Test
public void testSelectAll(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("email",".cn");
List<User> users = this.userMapper.selectList(wrapper);
users.forEach(user -> System.out.println(user));
}
SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE email LIKE ?
entity查询全部记录(并翻页)
- 配置插件
- 测试类
@Test
public void testSelectPage(){
Page<User> page = new Page<>(1,2);
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("email",".cn");
IPage<User> userIPage = this.userMapper.selectPage(page, wrapper);
System.out.println(userIPage.getTotal());
System.out.println(userIPage.getPages());
System.out.println(userIPage.getCurrent());
List<User> records = userIPage.getRecords();
records.forEach(item -> System.out.println(item));
}
|