springboot+thymeleaf+mybatis逆向工程和pageHelper(1)
1.pageHelper的使用注意事项,springboot的话一定要导入以下两个包!!!:
<!-- mybatis的分页插件包: -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
<!--springboot一定要有这个,否则分页失效:-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
html页面(thyme leaf引擎渲染):
顶部:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
页码部分:
<div class="col-md-6"> <nav aria-label="Page navigation"> <ul class="pagination"> <!--首页:--> <li><a th:href="@{/manage/product/list?pageNum=1}">首页</a></li> <!-- mybatis的分页插件封装了一个是否有上下页的逻辑属性: --> <!--上一页:--> <li th:if="${pageInfo.hasPreviousPage}"> <a th:href="@{/manage/product/list?pageNum=}+${pageInfo.pageNum-1}" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <!-- 判断当前页是否是选择的那夜,是的话高亮显示: --> <!--中间页码的渲染:--> <li > <ul th:each="items:${pageInfo.navigatepageNums }"> <li th:if="${items == pageInfo.pageNum}" class="active"><a href="#" th:text="${items}"></a></li> <li th:if="${items != pageInfo.pageNum}"><a th:href="@{/manage/product/list?pageNum=}+${items}" th:text="${items}"></a></li> </ul> </li> <!--下一页:--> <li th:if="${pageInfo.hasNextPage}"> <!--<a th:href="/manage/product/list?pageNum=${pageInfo.pageNum+1}" aria-label="Next">--> <a th:href="@{/manage/product/list?pageNum=}+${pageInfo.pageNum+1}" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> <!--末页:--> <li><a th:href="@{/manage/product/list?pageNum=}+${pageInfo.pages}">末页</a></li> </ul> </nav> </div>
调用层,只需要传入对应的第几页和每页的大小即可:
controller层:
@RequestMapping("list")
public String getList(Model model,HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
PageInfo result = productService.getProductList(2,10);
model.addAttribute("pageInfo",result);
return "charts";
}
dao层:
/** * * @param pageNum:第几页 * @param pageSize:每页的大小 * @return */ @Override public PageInfo getProductList(int pageNum, int pageSize) { //startPage--start //填充自己的sql查询逻辑 //pageHelper-收尾 PageHelper.startPage(pageNum,pageSize); List<Product> productList = productMapper.selectList(); PageInfo pageResult = new PageInfo(productList); return pageResult; }
2.mybatis逆向生成注意事项:user表最好是tb_user,不要用user命名数据库表名:
1.目录结构:

2.逆向工程配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <!-- mybatis逆向工程需要的文件演示: --> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 去除逆向工程生成的注释: --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 配置数据库连接: --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/supermarket" userId="root" password="root123"> </jdbcConnection> <!-- java类型解析: --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- (java模型生成)指定javabean生成的位置: --> <javaModelGenerator targetPackage="com.supermarket.bean" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 指定映射文件: --> <sqlMapGenerator targetPackage="com.supermarket.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 指定dao接口生成的位置,也就是mapper接口: --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.supermarket.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定每个表的生成策略,就是哪个表对应哪个javabean: --> <!-- 表名、生成的类名 --> <table tableName="cart" domainObjectName="Cart"></table> <table tableName="order" domainObjectName="Order"></table> <table tableName="payinfo" domainObjectName="Payinfo"></table> <table tableName="product" domainObjectName="Product"></table> <table tableName="tb_user" domainObjectName="User"></table> </context> </generatorConfiguration>
3.逆向工程执行类:
/** * mybatis逆向生成工具类: */ public class MBGTest { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("mbg.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }
4.properties配置文件:
#注意对应目录结构:
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.supermarket.bean
5.启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
//注意如果是通用mapper插件的话要导入这个包: import tk.mybatis.spring.annotation.MapperScan; @MapperScan("com.supermarket.mapper") @SpringBootApplication public class SupermarketApplication { public static void main(String[] args) { SpringApplication.run(SupermarketApplication.class, args); } }

浙公网安备 33010602011771号