一. 项目创建及配置
1.项目创建
点击finish完成创建
- pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
<scope>runtime</scope>
</dependency>
- applicatio.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/shiro
username: root
password: 123
logging:
level:
com.mapper: trace
二、增删改查实现
- 实体类 entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tb_user")
public class User {
@TableId(value="id",type = IdType.AUTO)
private Integer id;
@TableField("username")
private String username;
@TableField(value = "password",select = false) //查询密码不显示
private String password;
private String gender;
private String addr;
}
- UserMapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
- test
//insert
@Test
void insert(){
User user =new User(null,"武松","123","男","梁山");
int insert = userMapper.insert(user);
System.out.println(insert);
}
//update
@Test
void updata(){
User user = userMapper.selectById(1);
user.setPassword("111");
int update = userMapper.updateById(user);
System.out.println(update);
}
//delete
@Test
void delete(){
int delete = userMapper.deleteById(7);
System.out.println(delete);
}
//selectOne
@Test
void selectOne1(){
User user = userMapper.selectById(1);
System.out.println(user);
}
//selectAll
@Test
void selectAll(){
Wrapper<User> wrapper = new QueryWrapper<>();
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
//分页
@Test
void page(){
int pageNo=1;
int pageSize=3;
IPage<User> page = new Page<>(pageNo,pageSize);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
System.out.println("总页数:"+page.getPages());
System.out.println("当前页码:"+page.getCurrent());
System.out.println("每页记录数:"+page.getSize());
System.out.println("总条数:"+page.getTotal());
System.out.println("当前页的记录:"+page.getRecords());
}
// 条件查询
@Test
void select(){
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
/* userQueryWrapper.lambda().gt(User::getId,3)
.like("username","%宋%");*/
/* LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
userLambdaQueryWrapper.gt(User::getId,3)
.like(User::getUsername,"%宋%");*/
userQueryWrapper.gt("id",2)
.like("username","%宋%");
List<User> user = userMapper.selectList(userQueryWrapper);
System.out.println(user);
}
// 分组查询
@Test
void group(){
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.select("gender","count(*) as gc")
.groupBy("gender");
List<Map<String,Object>> maps = userMapper.selectMaps(userQueryWrapper); System.out.println(maps);
}
//投影查询
@Test
void touying(){
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.select("id,username,gender");
List<User> user = userMapper.selectList(userQueryWrapper);
System.out.println(user);
}
标签:SpringBoot,userQueryWrapper,userMapper,改查,System,plus,user,println,out
From: https://www.cnblogs.com/oyww-2027/p/17761648.html