本文主要涉及到mybatis,mybatis-plus,pageHelper三种技术
1、pom文件
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--Mybatis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.9</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<!--解决LocalDateTime在json序列化时不能转为“yy-MM-dd”的格式-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2、application.yml
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
global-config:
db-column-underline: true
id-type: 1
capital-mode: true
pagehelper:
reasonable: false
support-methods-arguments: true
params: count=countSql
row-bounds-with-count: true
helper-dialect: mysql
3、MybatisPlusConfig.java
@Configuration
public class MybatisPlusConfig {
@Bean
public PerformanceInterceptor performanceInterceptor(){
return new PerformanceInterceptor();
}
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
4、PhoneUser.java
@Data
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("mybatis_plus")
public class PhoneUser extends Model<PhoneUser> {
/**
* 编号
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 联系方式
*/
@NotEmpty(message = "联系方式不可为空")
private String phoneNumber;
/**
* 密码
*/
private String password;
/**
* 注册时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime registerDate;
@TableField(exist = false)
private String remarks;
private MybatisPlusChild mybatisPlusChild;
@Override
protected Serializable pkVal() {
return this.id;
}
}
5、PhoneUserMapper.java
@Repository
public interface PhoneUserMapper extends BaseMapper<PhoneUser> {
/**
* 获取mybatis_plus的联系方式列表
*
* @return 联系方式列表
*/
@Select("SELECT * FROM mybatis_plus")
@Results(id = "mybatisPlusMap", value = {
@Result(column = "id", property = "id"),
@Result(column = "id", property = "mybatisPlusChild",
one = @One(select = "com.example.mybatis.mapper.PhoneUserMapper.getChild")),
@Result(column = "phone_number", property = "phoneNumber")
})
List<PhoneUser> getList();
/**
* 获取mybatis_plus_child的记录
*
* @param id mybatis_plus_id
* @return
*/
@Select("SELECT * FROM mybatis_plus_child WHERE 1=1 AND mybatis_plus_id=#{id}")
MybatisPlusChild getChild(@Param("id") Long id);
/**
* 此处使用sqlProvider实现查询
*
* @param user
* @return 查询结果
*/
@SelectProvider(type = PhoneUserProvider.class, method = "select")
@ResultMap(value = "mybatisPlusMap")
List<PhoneUser> getListBySqlProvider(PhoneUser user);
/**
* 批量更新
*
* @return 是否成功,1,代表成功
*/
@Update({"<script>",
"<foreach item='item' index='key' collection='list' separator=';'>" +
"UPDATE mybatis_plus SET " +
"phone_number=#{item.phoneNumber}," +
"password=#{item.password}," +
"register_date=#{item.registerDate}" +
" WHERE 1=1 AND id=#{item.id}" +
"</foreach>",
"</script>"})
int updateBatchById(@Param("list") List<PhoneUser> list);
/**
* 批量删除
* @param list id列表
* @return 删除结果
*/
@Delete({"<script>",
"DELETE FROM archive_classify WHERE id OR parent_id in(" +
"<foreach collection='list' item='item' separator=','>" +
"#{item}" +
"</foreach>)" +
"</script>"})
int deleteBatchByClassifyIds(@Param("list") List<String> list);
}
6、PhoneUserProvider.java
public class PhoneUserProvider {
public String select(PhoneUser user) {
StringBuffer sql = new StringBuffer("SELECT * FROM mybatis_plus WHERE 1 = 1");
if (user.getPassword() != null) {
sql.append(" AND password = #{password}");
}
return sql.toString();
}
}
7、PhoneUserService.java
@Service
public class PhoneUserService extends ServiceImpl<PhoneUserMapper, PhoneUser> {
@Autowired
PhoneUserMapper mapper;
public List<PhoneUser> selectList() {
List<String> array = new ArrayList<>();
array.add("id");
List<PhoneUser> list = mapper.selectList(
new EntityWrapper<PhoneUser>()
.where("password", "123")
// .eq("password", "123")
.or("password=123456")
// .andNew("id={0}", "1")
.like("phone_number", "1316")
// .groupBy("password")
.orderDesc(array)
);
return list;
}
}