• springboot例子



    @Mapper
    public interface FinancingMapper {

    @Insert("<script>" +
    "insert into financingproduct values(#{id},#{risk},#{income},#{saleStarting},#{saleEnd},#{end})" +
    "</script>")
    int insertFinancingProduct (String id,int risk,String income,String saleStarting,String saleEnd,String end);

    @Delete("<script>" +
    "delete from financingproduct where id = #{id}" +
    "</script>")
    int delFinancingProductById (String id);

    @Update("<script>" +
    "update financingproduct set risk=#{risk},income=#{income},saleStarting=#{saleStarting},saleEnd=#{saleEnd},end=#{end} where id = #{id}" +
    "</script>")
    int updFinancingProductById (int risk,String income,String saleStarting,String saleEnd,String end,String id);

    @Select("<script>" +
    "select * from financingproduct where id = #{id}" +
    "</script>")
    FinancingProduct getFinancingProductById(@Param("id") String id);

    @Select("<script>" +
    "select count(1) from financingproduct limit #{offset},#{limit}"
    +
    "</script>")
    long getFinancingProductCount(Integer offset,Integer limit);

    @Select("<script>" +
    "select * from financingproduct limit #{offset},#{limit}"
    +
    "</script>")
    List<FinancingProduct> getFinancingProductList(Integer offset,Integer limit);
    }
     
    @Service
    public class FinancingService {
    @Autowired
    FinancingMapper financingMapper;

    public FinancingProduct getFinancingProductById(String id){
    return financingMapper.getFinancingProductById(id);
    }

    public int insertFinancingProduct(String id,int risk,String income,String saleStarting,String saleEnd,String end){
    return financingMapper.insertFinancingProduct(id, risk,income,saleStarting,saleEnd,end);
    }

    public int delFinancingProductById (String id){
    return financingMapper.delFinancingProductById(id);
    }

    public int updFinancingProductById (int risk,String income,String saleStarting,String saleEnd,String end,String id){
    return financingMapper.updFinancingProductById(risk,income,saleStarting,saleEnd,end,id);
    }

    public long getFinancingProductCount(Integer offset,Integer limit){
    return financingMapper.getFinancingProductCount(offset,limit);
    }

    public List<FinancingProduct> getFinancingProductList(Integer offset,Integer limit){
    return financingMapper.getFinancingProductList(offset,limit);
    }
    }

    @Controller

    public class FinancingController {
    @Autowired
    FinancingService financingServiceImpl;

    @RequestMapping("/insertFinancingProduct")
    @ResponseBody
    public int insertFinancingProduct(HttpServletRequest httpServletRequest, ModelMap modelMap,String id,int risk,String income,String saleStarting,String saleEnd,String end){
    return financingServiceImpl.insertFinancingProduct(id, risk,income+"%",saleStarting,saleEnd,end);
    }

    @RequestMapping("/delFinancingProductById")
    public String delFinancingProductById(HttpServletRequest httpServletRequest, ModelMap modelMap,String id){
    financingServiceImpl.delFinancingProductById(id);
    return "getFinancingProductList";
    }

    @RequestMapping("/updFinancingProductById")
    @ResponseBody
    public int updFinancingProductById(HttpServletRequest httpServletRequest, ModelMap modelMap,int risk,String income,String saleStarting,String saleEnd,String end,String id){
    return financingServiceImpl.updFinancingProductById(risk,income+"%",saleStarting,saleEnd,end,id);
    }

    @RequestMapping("/getFinancingProductById")
    public String getFinancingProductById(HttpServletRequest httpServletRequest, ModelMap modelMap,String id){
    FinancingProduct financingProduct = financingServiceImpl.getFinancingProductById(id);
    modelMap.addAttribute("financingProduct", financingProduct);
    return "index";
    }

    @RequestMapping("/getFinancingProductList")
    public String getFinancingProductList(HttpServletRequest httpServletRequest, ModelMap modelMap){
    int limit = 3;
    int offset = 0;
    Long total = 0L;
    PageHelper pageHelper = PageHelper.builder();
    offset = (pageHelper.getPageNumFromUrl(httpServletRequest) - 1)*limit; //limit后第一个参数
    total = pageHelper.getTotalFromUrl(httpServletRequest); //limit后最后一个参数

    //如果total为空,那么查询获取总记录数
    if (total == null) total = financingServiceImpl.getFinancingProductCount(offset,limit);

    //查询获取数据
    List<FinancingProduct> result = financingServiceImpl.getFinancingProductList(offset,limit);
    String banner = pageHelper.getPageBanner(limit, total, httpServletRequest);
    modelMap.addAttribute("page", banner);
    modelMap.addAttribute("result", result);
    return "index";
    }


    }


    utils中的PageHelper
    /**
    * 一个基于GET请求进行的分页类,可做适当的设置
    * 使用方法:
    * 默认样式PageHelper.builder()
    * 或者定制样式
    * PageHelper.builder("myid","mytotal",80000L,1000,7)
    * 完整例子:(控制器某方法)
    * public String foo(HttpServletRequest request,ModelMap model,Condition cond){
    * //从con参数进行解析...省略....
    * Integer limit = 20; //每页限制显示20条
    * PageHelper page = PageHelper.builder();
    * Long total = page.getTotalFromUrl(request);
    * if(total ==null){
    * total = service.getTotalFromDb(); //用自己的方法从数据库获取总记录数
    * }
    * Integer pageNum = page.getPageNumFromUrl(request);//得到请求的页码
    * String banner = page.getPageBanner(limit,total,request);//返回生成的分页按钮
    * model.put("pagebanner",banner);//加入模板引擎里
    * List<Object> list = service.getRecords(pageNum * limit,.....);//给出本次查询返回值的offset
    * .....省略
    */
    public class PageHelper {
    //样式属性,可设置
    private String pagetag = "_pgid_";//下一页在url上的name属性名
    private String totaltag = "_totalnums_";//总记录数在url上的name属性名
    private Long switchNumber = 100000L;//总记录数到达这数字,则切换样式(无尾页按钮)
    private Integer bannerWidth = 800;//分页banner的宽度
    private Integer btnNumber = 5;//分页显示的按钮数,需要单数,比如5,7,9
    //一些初始设置值
    private Integer pgid=1;//当前按钮页
    private Integer firstBtn=0;//第一个页按钮
    private Integer lastBtn=0;//最后一个页按钮
    private String baseURL="";//除_total=10000&_pgid=2 之外的uri
    private Integer pages=1; //总分页数
    private Long total=0L;//总记录数
    //省略号样式
    private String points = "style='margin:0;border-style:none;background:none;padding:5px 0px;color:#808080;float:left;'";
    //未选中的页码按钮样式
    private String a_style = "style='box-sizing:border-box;display:block;float:left;margin-right:10px;"
    + "padding:2px 12px;height:30px;line-height:24px;border:1px #cccccc solid;background:#fff;"
    + "text-decoration:none;color:#808080;font-size:12px;'";
    // 禁止使用的样式disable
    private String disable_style = "style='box-sizing:border-box;float:left;padding:2px 12px;height:30px;line-height:24px;font-size:12px;"
    + "color:#bbb;border:1px #ccc solid;background:#fcfcfc;margin:0;margin-right:10px;'";

    //选中按钮的样式
    private String select_style = "style='box-sizing:border-box;display:block;float:left;margin-right:10px;"
    + "padding:2px 12px;height:30px;line-height:24px;"
    + "text-decoration:none;font-size:12px;border:1px #cccccc solid;background:#077ee3;color:#fff;'";

    //总分页数和总记录数的样式
    private String total_style ="style='box-sizing:border-box;border-style:none;background:none;font-size:12px;"
    + "margin:0px;height:30px;padding:3px 0px;color:#666;display:block;float:left;line-height:24px;'";

    //按钮鼠标进入和离开时候的事件属性
    private String mouseEvent = "onmouseenter='nbf_mouseenter(this)' onmouseleave='nbf_mouseleave(this)'";

    //要执行的鼠标script语句
    private String scripts ="<script type='text/javascript'>"
    + "function nbf_mouseenter(obj){obj.style.color='#077ee3';obj.style.border='1px #077ee3 solid';}"
    + "function nbf_mouseleave(obj){obj.style.color='#808080';obj.style.border='1px #cccccc solid';}</script>";

    /**
    * 使用默认样式的构建方法
    * @return
    */
    public static PageHelper builder(){
    return new PageHelper();
    }

    /**
    * 带样式的构建方法
    * @param pagetag 页码的name值(附加在url上)
    * @param totaltag 记录总数的name值(附加在url上)
    * @param switchNumber 总记录数到达该值,则切换样式
    * @param bannerWidth 分页div的宽度
    * @param btnNumber 分页按钮的个数(必须是单数,至少5个)
    * @return 该类的实例
    */
    public static PageHelper builder(String pagetag, String totaltag
    , Long switchNumber, Integer bannerWidth
    , Integer btnNumber){
    return new PageHelper(pagetag,totaltag,switchNumber,bannerWidth,btnNumber);
    }

    private PageHelper(){}

    /**
    * 给对象赋样式的自定义值
    * @param pagetag
    * @param totaltag
    * @param switchNumber
    * @param bannerWidth
    * @param btnNumber
    */
    private PageHelper(String pagetag, String totaltag
    , Long switchNumber, Integer bannerWidth
    , Integer btnNumber){
    if(pagetag!=null && pagetag.trim().isEmpty()==false)
    this.pagetag = pagetag;
    if(totaltag!=null && totaltag.trim().isEmpty()==false)
    this.totaltag = totaltag;
    if(switchNumber!=null)
    this.switchNumber = switchNumber;
    if(bannerWidth!=null)
    this.bannerWidth = bannerWidth;
    if(btnNumber!=null)
    this.btnNumber = btnNumber;
    }

    /**
    * 从url上获取total的总记录值
    * @param request 由控制器透传的httpserverrequest
    * @return Long
    */
    public Long getTotalFromUrl(HttpServletRequest request) {
    String totalTag = request.getParameter(totaltag); //从页面获取总记录数
    if (totalTag != null && totalTag.trim().isEmpty() == false) { //说明已经查询过总数了
    try {
    return Long.valueOf(totalTag); //将String 类型的数字转化为Long类型
    } catch (Exception e) {
    return null; //报错则返回null
    }


    }
    return null; //如果页面没有传回总记录数那么返回null
    }

    /**
    * 从url上获取请求的页码数值,最小值是1
    * @param request
    * @return Integer
    */
    public Integer getPageNumFromUrl(HttpServletRequest request) {
    String pgid = request.getParameter(pagetag); //获取请求参数
    if (pgid != null && pgid.trim().isEmpty() == false) { //从url获取请求的页码
    try {
    return Integer.valueOf(pgid); //将String类型的数字转化为数字
    } catch (Exception e) {
    return 1; //如果发生错误,当前页数默认为1
    }


    }
    return 1; //如果没有从前端页面获取当前页数,默认为1
    }

    /**
    * 得到一个分页按钮的HTML字符串,可直接嵌入网页内
    * @param limit 每页限制返回的记录数
    * @param total 该查询返回的总记录数(需要提取用count语句获得)
    * @param request 控制器透传的HttpServletRequest
    * @return 分页的html代码(String)
    */
    public String getPageBanner(Integer limit,Long total,HttpServletRequest request){
    if(total<=limit) return "";
    this.total = total;
    String URI = request.getRequestURI();
    pgid = getPageNumFromUrl(request);

    String params ="";
    Enumeration<String> names = request.getParameterNames();
    while (names.hasMoreElements()){
    String name = names.nextElement();
    if(name.equalsIgnoreCase(pagetag) || name.equalsIgnoreCase(totaltag))
    continue;
    String value = request.getParameter(name);
    params+=name+"="+value+"&";
    }
    //params=params.substring(0,params.lastIndexOf("&"));
    baseURL=URI+"?"+params;
    pages =(int)Math.ceil(((double)total / (double)limit));//计算出总分页数
    Integer baseBtnNum = (int)Math.floor((double)(btnNumber/2));//计算当前按钮两边的按钮数
    //第一个页按钮
    firstBtn = pgid - baseBtnNum;
    //最后一个页按钮
    lastBtn = pgid + baseBtnNum;
    if(firstBtn<1){
    lastBtn=lastBtn+(1-firstBtn);
    firstBtn=1;
    }
    if(lastBtn>pages){
    firstBtn = firstBtn - (lastBtn-pages);
    lastBtn = pages;
    }
    if (firstBtn<1)
    firstBtn=1;

    //开始构建banner的html代码
    String Banner ="<div class='nbf_pageslice' "+buildBanner()+ ">"
    + buildHome()
    + buildPreBtn();
    if(total>0){
    Banner+=buildBtns();
    }
    Banner+=buildNextBtn();
    if(total>0 && total<switchNumber){
    Banner+=buildTail();
    }
    Banner+=buildTotalPage();
    Banner+=scripts;
    Banner+="<div style='clear:both'></div></div>";
    return Banner;
    }


    private String buildUrl(Integer pgid){
    String newUri = baseURL+totaltag+"="+total+"&"+pagetag+"="+pgid;

    return newUri;
    }

    /**
    * 构建首页
    * @return
    */
    private String buildHome(){
    if(pgid!=1){
    String URI = buildUrl(1);
    return "<a class='enable_btn' "+mouseEvent+" "+ a_style+" href='"+URI+"' title='首页'>首页</a>";
    }else{
    return "<p class='nbf_p_style' "+disable_style+">首页</p>";
    }
    }

    /**
    * 构建尾页
    * @return
    */
    private String buildTail(){
    if (pgid != pages) {
    String URI = buildUrl(pages);
    return "<a class='enable_btn'"+mouseEvent+" "+a_style+" href='"+URI+"' title='尾页'>尾页</a>";
    } else {
    return "<p class='nbf_p_style' "+disable_style+">尾页</p>";
    }
    }

    /**
    * 构建整个banner的样式
    * @return
    */
    private String buildBanner(){
    return "style='"+ bannerWidth +"px;margin-left:auto;margin-right:auto;height:40px;padding:20px 0px;'";
    }

    /**
    * 构建上一页按钮
    * @return
    */
    private String buildPreBtn(){
    if (pgid != 1) {
    String URI = buildUrl(pgid - 1);
    return "<a class='enable_btn' "+mouseEvent+" "+a_style+" href='"+URI+"' title='上一页'>上一页</a>";
    } else {
    return "<p class='nbf_p_style' "+disable_style+">上一页</p>";
    }
    }

    /**
    * 构建下一页按钮
    * @return
    */
    private String buildNextBtn(){
    if (pgid != pages) {
    String URI = buildUrl(pgid + 1);
    return "<a class='enable_btn'"+mouseEvent+" "+a_style+" href='"+URI+"' title='下一页'>下一页</a>";
    } else {
    return"<p class='nbf_p_style' "+disable_style+">下一页</p>";
    }
    }

    /**
    * 构建中间的所有页按钮
    * @return
    */
    private String buildBtns(){
    String btns="";
    if(firstBtn>1){
    btns="<p class='nbf_p_style' "+points+">...</p>";
    }
    for(int i=firstBtn;i<=lastBtn;i++){
    if(i==pgid){
    btns+="<a class='disable_btn' "+select_style+" href='javascript:;' title='第" + i + "页' >"+i+"</a>";
    }else{
    String URI = buildUrl(i);
    btns+="<a class='enable_btn' "+mouseEvent+" "+a_style+" href='"+URI+"' title='第" +i+ "页'>"+i+"</a>";
    }
    }
    if(lastBtn<pages){
    btns+="<p class='nbf_p_style' "+points+">...</p>";
    }
    return btns;
    }

    private String buildTotalPage(){
    return "<p class='nbf_p_style' "+total_style+" >共<span style='color:red;'> " + pages
    +" </span>页<span style='color:red;'> " + total + " </span>条数据</p>";
    }


    }


    index.html页面中的内容

    <script src="${request.contextPath}/static/dist/jquery.js"></script>
    <body>


    <div class="x-body lay-change-page">
    <div class="layui-card">
    <div class="layui-card-body">
    <table class="layui-table">
    <thead>
    <tr>
    <th>ID</th>
    <th>风险评级</th>
    <th>预期收益</th>
    <th>发售起始日</th>
    <th>发售结束日</th>
    <th>产品到期日</th>
    <th style="min-150px">操作</th>
    </tr>
    </thead>
    <tbody>
    <#if result??>
    <#list result as list>
    <tr>
    <td>${list.id}</td>
    <td>${list.risk!}</td>
    <td>${list.income!}</td>
    <td>
    <#if list.saleStarting??>
    ${list.saleStarting?string('yyyy-MM-dd hh:mm:ss')}
    </#if>
    </td>
    <td>
    <#if list.saleEnd??>
    ${list.saleEnd?string('yyyy-MM-dd hh:mm:ss')}
    </#if>
    </td>
    <td>
    <#if list.end??>
    ${list.end?string('yyyy-MM-dd hh:mm:ss')}
    </#if>
    </td>
    <td class="td-manage">
    <a title="删除" href="${request.contextPath}/delFinancingProductById?id=${list.id}"
    class="a-link" ><!--onclick="delFinancingById(${list.id})"-->
    删除
    </a>
    <a title="修改" href="javascript:;"
    class="a-link" onclick="OpenLayer('edit?id=${list.id}',['700px','80%'])">
    修改
    </a>
    |
    <a class="a-link" title="通过" href="javascript:;"
    onclick="audit(${list.id})"> <!--OpenFrame('停用','${request.contextPath}/search/pass?id=${list.id}',['确定','取消'],'.commit',['800px','400px'])-->
    通过
    </a>
    </td>
    </tr>
    </#list>
    </#if>
    </tbody>
    </table>
    </div>
    </div>
    <#if page??>${page}</#if>
    </div>


    <script>

    /*function delFinancingById(id) {
    alert(id);
    $.ajax({
    url:'delFinancingProductById',
    type:'GET',
    dataType:'text',
    data:{id:id},
    success:function (msg) {
    alert(id);
    alert(msg);
    alert("删除成功!");
    }
    })
    }*/
    </script>
    </body>

    application.yml
    server:
    port: 8083
    spring:
    profiles:
    active: dev
    mvc:
    static-path-pattern: /static/**



    ---
    spring:
    profiles: release
    datasource:
    url: jdbc:mysql://192.168.10.5:3306/keyword?useSSL=false
    password: xcm123
    username: client
    resources:
    static-locations:
    - file:/keywords/
    - file:/keywords/static/
    freemarker:
    cache: false
    charset: utf-8
    allow-request-override: false
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    suffix: .html
    template-loader-path: file:/keywords/templates/
    request-context-attribute: request

    ---
    spring:

    profiles: dev
    datasource:
    # url: jdbc:mysql://10.0.0.191/keyword?useUnicode=true&characterEncoding=utf-8&useSSL=false
    # password: xcM@12345
    # username: client
    url: jdbc:mysql://127.0.0.1/licai?useUnicode=true&characterEncoding=utf-8&useSSL=false
    password: root
    username: root
    freemarker:
    cache: false
    charset: utf-8
    allow-request-override: false
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    suffix: .html
    template-loader-path: classpath:/templates/
    request-context-attribute: request

    logging:
    level:
    com.hstt.antiweb.mapper.*: debug


    pom.xml

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>
    </dependency>

    <dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>2.5.0</version>

    </dependency>

    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
    </dependency>


    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    </dependency>

    <dependency>
    <groupId>com.google.collections</groupId>
    <artifactId>google-collections</artifactId>
    <version>1.0-rc2</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>










  • 相关阅读:
    1059 C语言竞赛
    1058 选择题
    1057 数零壹
    1056 组合数的和
    1055 集体照
    Mysql--分库分表
    Mysql--改表结构
    Mysql--开始阶段
    Mysql--常用语句
    Mysql--grant授权
  • 原文地址:https://www.cnblogs.com/fengzifengfeng/p/11281681.html
Copyright © 2020-2023  润新知