使用bootstrap-table,bootstrap,以及MyBatis框架链接数据库

1 school的index页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Student</title>

    <th:block th:replace="common/script"></th:block>
    <th:block th:replace="common/link"></th:block>
</head>
<body>
    <table class="table table-striped" id="tabList"></table>
<th:block th:include="school/js/index_js"/>
</body>
</html>

2  操作school index页面的js代码  注意!!!!!!!其中$('#tabList')这个标签选择器中的id是table标签中的id

 url: 'school/getData '  所指的是controller中的@RequestMapping("/getData")

<script>
    $(function () {
        //1.初始化Table
        var oTable = new TableInit();
        oTable.Init();
    });


    var TableInit = function () {
        var oTableInit = new Object();
        //初始化Table
        oTableInit.Init = function () {
            $('#tabList').bootstrapTable({//school欢迎页面中table按钮的id
                url: 'school/getData',         //请求后台的URL(*)
                method: 'get',                      //请求方式(*)
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
                pagination: true,                   //是否显示分页(*)
                sortable: false,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: oTableInit.queryParams,//传递参数(*)
                sidePagination: "server",           //分页方式:client客户端分页,server服务端分页(*)
                pageNumber: 1,                       //初始化加载第一页,默认第一页
                pageSize: 10,                       //每页的记录行数(*)
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数(*)
                search: true,                       //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
                contentType: "application/x-www-form-urlencoded",
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: true,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                height: 700,                        //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
                uniqueId: "no",                     //每一行的唯一标识,一般为主键列
                showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                columns: [
                    {
                        "field" : "id",
                        "title" : "id",
                        "align" : "center"
                    },
                    {
                        "field" : "name",
                        "title" : "名称",
                        "align" : "center"
                    },
                    {
                        "field" : "remark",
                        "title" : "类型" +
                            "",
                        "align" : "center"
                    },
                    {
                        "field" : "score",
                        "title" : "分数",
                        "align" : "center"
                    },
                    {
                        "field" : "level",
                        "title" : "级别",
                        "align" : "center"
                    },
                    {
                        field: 'operate',
                        title: '操作',
                        formatter: operateFormatter //自定义方法,添加操作按钮
                    },
                ],
                rowStyle: function (row, index) {
                    var classesArr = ['success', 'info'];
                    var strclass = "";
                    if (index % 2 === 0) {//偶数行
                        strclass = classesArr[0];
                    } else {//奇数行
                        strclass = classesArr[1];
                    }
                    return { classes: strclass };
                },//隔行变色
            });

        };


        //得到查询的参数
        oTableInit.queryParams = function (params) {
            var temp = {   //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
                limit: params.limit,   //页面大小
                offset:params.offset
            };
            return temp;
        };
        return oTableInit;
    };


    function operateFormatter(value, row, index) {//赋予的参数
        return [
            '<a class="btn active disabled" href="#">编辑</a>',
            '<a class="btn active" href="#">档案</a>',
            '<a class="btn btn-default" href="#">记录</a>',
            '<a class="btn active" href="#">准入</a>'
        ].join('');
    }
</script>

3 使用Mybatis框架将数据库中的数据在网页中显示

MyBatis框架的步骤!!!!!!!!!!!!!!!!!!!!!!!!!

1.写实体类School

package com.stu.frame.myspringboot.model;

public class MySchool {

    private Integer id;

    private String seq;

    private String score;

    private String name;

    private String level;

    private String remark;

    @Override
    public String toString() {
        return "MySchool{" +
                "id=" + id +
                ", seq='" + seq + '\'' +
                ", score='" + score + '\'' +
                ", name='" + name + '\'' +
                ", level='" + level + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getSeq() {
        return seq;
    }

    public void setSeq(String seq) {
        this.seq = seq;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

 2.写SchoolMapper接口,并在接口中定义方法,由于该类为接口所以无需实现方法。

package com.stu.frame.myspringboot.dao;

import com.stu.frame.myspringboot.model.MySchool;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MySchoolMapper {

    List<MySchool> selSchool();
}

3.写SchoolService类  使用@Service注解表示该类为service层  @Autowired注解是将UserMapper 加载到该类中相当与new UserMapper 。

package com.stu.frame.myspringboot.service;

import com.stu.frame.myspringboot.dao.UserMapper;
import com.stu.frame.myspringboot.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User selByName(String name){
        return userMapper.selByName(name);
    }
}

4.写SchoolMapper.xml配置文件 用于操作数据库

注意!!!!!!!!!!

其中Mapper标签中的 namespace属性的值是 SchoolMapper接口的全路径

select标签中id属性的值是SchoolMapper接口中定义的方法名,切记一定要与方法名一样,建议直接复制,resultType属性的值是School实体类的全路径

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.stu.frame.myspringboot.dao.MySchoolMapper">
    <select id="selSchool" resultType="com.stu.frame.myspringboot.model.MySchool">
        select * from school;
    </select>
</mapper>

5.conterller层代码

package com.stu.frame.myspringboot.controller;

import com.stu.frame.myspringboot.common.BootstrapTable;
import com.stu.frame.myspringboot.common.QueryParam;
import com.stu.frame.myspringboot.common.TestData;
import com.stu.frame.myspringboot.model.MySchool;
import com.stu.frame.myspringboot.model.School;
import com.stu.frame.myspringboot.service.ISchoolService;
import com.stu.frame.myspringboot.service.MySchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    MySchoolService mySchoolService;

    @RequestMapping({"/index", ""})
    public String getLogin(Model model) {
        return "school/index";
    }


    @RequestMapping("/getData")
    @ResponseBody
    public BootstrapTable getData() {

        List<MySchool> list = mySchoolService.selSchool();

        return new BootstrapTable(30, list);
    }

}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值