SpringBoot前后端分离
导入依赖
<!--阿里连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 分页插件 pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
配置全局属性文件
#数据库配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db8?useUnicode=true&characterEncoding=UTF-8&&useSSL=false&serverTimezone=Hongkong
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
#mybatis
#mapper映射路径
mybatis.mapper-locations=classpath:mapper/*.xml
#mapper映射扫描包路径开启别名
mybatis.type-aliases-package=com.Mr_xiao.helloboot.entity
#开启驼峰命名转换
mybatis.configuration.map-underscore-to-camel-case=true
练习分页查询(数据由数据库得到)
实体类heros:
public class Heros {
int id;
String name;
String nickname;
String sex;
String first;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
@Override
public String toString() {
return "\nHeros{" +
"id=" + id +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
", sex='" + sex + '\'' +
", first='" + first + '\'' +
'}';
}
}
mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Mr_xiao.helloboot.dao.HeroDao">
<select id="getHeros" resultType="Heros">
select *from heros
</select>
</mapper>
dao层
@Mapper
public interface HeroDao {
//取出所有
public List getHeros();
}
实现类
public interface HeroService {
public PageInfo getHeros(int num, int size);
}
@Service
public class HeroServiceImpl implements HeroService {
@Autowired
HeroDao heroDao;
@Override
public PageInfo getHeros(int num,int size) {
System.out.println("进入了HeroService服务");
//分页 当前页,每页条数
PageHelper.startPage(num,size);
List list=heroDao.getHeros();
PageInfo page=new PageInfo(list);//所有信息
System.out.println("总条数:"+page.getTotal());
System.out.println("总页数:"+page.getPages());
System.out.println("当前页:"+page.getPageNum());
int sz[]=page.getNavigatepageNums();
System.out.println("导航:"+ Arrays.toString(sz));
System.out.println(page.getList().size());
return page;
}
}
控制器:
@Controller
public class HeroController {
@Autowired
HeroService heroService;
@RequestMapping("/showhero/{num}")
public String show(ModelMap map,@PathVariable("num") int num){
if(num==0){
num=1;
}
PageInfo page =heroService.getHeros(num,10);
System.out.println("page="+page);
List<Heros> heros=page.getList();
map.put("page",page);
map.put("Heroslist",heros);
return "users/hero";
}
}
传值采用的是restful APi设计,可参考我的另一篇博客https://blog.csdn.net/m_xiaozhilei/article/details/121068110?spm=1001.2014.3001.5501
前台展示页:
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>LOL</title>
<style>
.btn{
background-color: red;
}
.boder{
float: left;
border: 1px solid blue;
margin: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>显示英雄</h1>
<table border="1px">
<tr>
<td>编号</td>
<td>英雄名</td>
<td>别名</td>
<td>性别</td>
<td>一技能</td>
</tr>
<tr th:each="h : ${Heroslist}">
<td th:text="${h.id}"></td>
<td th:text="${h.name}"></td>
<td th:text="${h.nickname}"></td>
<td th:if="${h.sex=='1'}" th:text="男"></td>
<td th:if="${h.sex=='0'}" th:text="女"></td>
<td th:text="${h.first}"></td>
</tr>
</table>
<a href="/showhero/1" th:text="首页" class="boder"></a>
<div th:each="s : ${page.navigatepageNums}">
<a th:href="${'/showhero/'+s}" th:class="${s==page.pageNum ? 'btn boder':'boder'}" th:text="${s}"></a>
</div>
<a th:text="尾页" th:href="${'/showhero/'+page.pages}" class="boder"></a>
</body>
</html>
最终效果图: