SpringBoot前后端分离(springboot+mybatis+pagehelper)

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>

最终效果图:
效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碰磕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值