一.maven配置
注释掉的部分因为版本的原因无法使用,所以就注释掉了,2中配置都是可以的
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<!--<artifactId>pagehelper</artifactId>-->
<!--<version>4.1.6</version>-->
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
二、PageUtil类
//注解@Configuration 表示PageHelperConfig 这个类是用来做配置的。
//注解@Bean 表示启动PageHelper这个拦截器。
//没有生效有可能maven版本的原因
@Configurable
public class PageUtil {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
// offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.
p.setProperty("offsetAsPageNum", "true");
// rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.
p.setProperty("rowBoundsWithCount", "true");
// reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
三、分页使用
@RequestParam(value = “start”,defaultValue = “0”)int start-------分页开始
@RequestParam(value = “size”,defaultValue = “5”)int size------分页长度
PageHelper.startPage(start, size, “user_id desc”);------开始,长度,排序
@RestController
public class HelloWord {
private static final Logger logger = LoggerFactory.getLogger(HelloWord.class);
@Autowired
private User user;
@RequestMapping("/hello")
public PageInfo<UserEntity> hello(@RequestParam(value = "start",defaultValue = "0")int start,@RequestParam(value = "size",defaultValue = "5")int size) throws Exception {
try {
PageHelper.startPage(start, size, "user_id desc");
List<UserEntity> list = user.fillAll();
PageInfo<UserEntity> pageInfo = new PageInfo<>(list);
System.out.println(pageInfo);
return pageInfo;
}catch (Exception e){
logger.error("HelloWord出错了");
return new PageInfo<>();
}
}
}
四、实体类
@Data类的使用看一下链接
https://blog.youkuaiyun.com/yilihuang/article/details/80617398
@Entity
//name--表名 schema-- 用户名 catalog--数据库名 uniqueConstraints和下方unique作用相当
@Table(name = "user",schema = "root",catalog = "new",uniqueConstraints = {@UniqueConstraint(columnNames = {"username","email"})})
@Data
public class UserEntity {
@Id
/*表明自增长方式*/
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
@Column(name = "username",columnDefinition = "varchar(255)",nullable = false,unique = true)
private String username;
@Column(name = "password",columnDefinition = "varchar(255)",nullable = false)
private String password;
@Column(name = "email",columnDefinition = "varchar(255)",nullable = false,unique = true)
private String email;
}
五、application.properties实体自动创建
#每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.ddl-auto=create----每次运行该程序,没有表格会新建表格,表内有数据会清空
#spring.jpa.hibernate.ddl-auto=create-drop---每次程序结束的时候会清空表
#spring.jpa.hibernate.ddl-auto=validate---运行程序会校验数据与数据库的字段类型是否相同,不同会报错
六、实体类使用驼峰命名法命名字段,数据库创建时会把userId变为user_id
在使用查询语句查询时,会出现sql字段和数据库不对应的情况,可以使用一下配置
查询出来会自动转换回来,如果传值的话还是要用user_id和数据库对应
# 经过试验,两种配置方法都可以。但如果同时配置,前者mybatis.configuration.mapUnderscoreToCamelCase的优先级更高。
#mybatis.configuration.mapUnderscoreToCamelCase=true
#设为true表示开启驼峰转换。
mybatis.configuration.map-underscore-to-camel-case=true
我的完整版spring boot配置
https://blog.youkuaiyun.com/qq_37060384/article/details/95942348