SpringBoot整合Mabatis+Layui+json实现增删改查

总结构图

导入Layui框架相关css js font

1.配置.properties文件

#thymeleaf 配置
spring.mvc.view.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.prefix=classpath:/
spring.resources.static-locations=classpath:/templates/
#spring.thymeleaf.servlet.content-type=text/html
#缓存设置为false, 这样修改之后马上生效,便于调试
spring.thymeleaf.cache=false
#DB Configuration:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://47.105.135.34:3306/tianchen?serverTimezone=GMT%2B8
spring.datasource.username=tianchen
spring.datasource.password=futianzhuang
server.port=8080
spring.servlet.multipart.max-file-size=1GB
#多个文件
spring.servlet.multipart.max-request-size=2GB
#mybatis专用
#spring集成Mybatis环境
#pojo别名扫描包
mybatis.type-aliases-package=com.tianchen.entity
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*.xml
#spring.mvc.static-path-pattern=/static/**

2.新建mapper文件夹 创建InforMapper.xml

<?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" >
<!--//dao层-->
<mapper namespace="com.tianchen.dao.InforMapper">
    <select id="query" resultType="com.tianchen.entity.Infor">
        select * from infor
    </select>
    <insert id="add" parameterType="com.tianchen.entity.Infor">
        insert into infor value (#{
   id},#{
   country},#{
   cumulative},#{
   cure},#{
   death})
    </insert>
    <delete id="delete" parameterType="com.tianchen.entity.Infor">
        delete from infor where id = #{
   id}
    </delete>
    <update id="edit" parameterType="com.tianchen.entity.Infor">
        update infor
        <trim prefix="set" suffixOverrides=",">
            <if test="country!=null">country=#{
   country},</if>
            <if test="cumulative!=null">cumulative=#{
   cumulative},</if>
            <if test="cure!=null">cure=#{
   cure},</if>
            <if test="death!=null">death=#{
   death},</if>

        </trim>
        WHERE id = #{
   id}
    </update>
</mapper>

3.创建Infor实体类

package com.tianchen.entity;

public class Infor {
   
    private int id;
    private String country;
    private String cumulative;
    private String cure;
    private String death;

    public Infor(int id, String country, String cumulative, String cure, String death) {
   
        this.id = id;
        this.country = country;
        this.cumulative = cumulative;
        this.cure = cure;
        this.death = death;
    }

    public Infor() {
   
    }

    public int getId() {
   
        return id;
    }

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

    public String getCountry() {
   
        return country;
    }

    public void setCountry(String country) {
   
        this.country = country;
    }

    public String getCumulative() {
   
        return cumulative;
    }

    public void setCumulative(String cumulative) {
   
        this.cumulative = cumulative;
    }

    public String getCure() {
   
        return cure;
    }

    public void setCure(String cure) {
   
        this.cure = cure;
    }

    public String getDeath() {
   
        return death;
    }

    public void setDeath(String death) {
   
        this.death = death;
    }

    @Override
    public String toString() {
   
        return "Infor{" +
                "id=" + id +
                ", country='" + country + '\'' +
                ", cumulative='" + cumulative + '\'' +
                ", cure='" + cure + '\'' +
                ", death='" + death + '\'' +
                '}';
    }
}

4.创建InforController

package com.tianchen.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianchen.entity.Infor;
import com.tianchen.service.InforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

public class InforController {
   
    @Autowired(required = false)
    private InforService inforService;

    //查询信息
    @RequestMapping(value = "/query")
    public Map query(HttpSession session, @RequestParam(required = false, defaultValue = "1") int page,
                     @RequestParam(required = false) int limit) {
   

        //定义页数和条数
        PageHelper.startPage(page, limit);
        List<Infor> list = inforService.query();
        //将数据传给rolePageInfo
        PageInfo<Infor> rolePageInfo = new PageInfo<>(list);
        //转换格式
        Map map = new HashMap<String, Object>();

        //状态码如果成功返回0
        map.put("code", "0");
        //错误代码
        map.put("msg", "");
        //总数量(分页)
        map.put("count", rolePageInfo.getTotal());
        //返回实体对象集合
        map.put("data", rolePageInfo.getList());
        
        return map;

    }

    //添加信息
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Transactional
    public String add(@ModelAttribute
  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
学生成绩管理系统是一个常见的项目,基于SpringBoot+Mybatis+Layui进行开发可以快速构建一个高效、稳定、易于维护的Web应用程序。下面是一个简单的学生成绩管理系统的实现思路: 1. 搭建项目框架 使用SpringBoot框架搭建项目,通过Maven或Gradle来管理依赖。可以使用Mybatis框架来访问数据库,并使用Layui框架实现前端交互。 2. 确定数据库表结构 根据需求,设计学生成绩管理系统的数据库表结构。可以使用MySQL等关系型数据库,也可以使用NoSQL数据库(如MongoDB)。 3. 实现数据访问层 使用Mybatis框架实现数据访问层,包括DAO接口和Mapper文件。在Mapper文件中编写SQL语句来操作数据库,例如查询成绩、添加学生信息等。 4. 实现业务逻辑层 在业务逻辑层中,实现各种操作的具体逻辑,例如查询学生成绩、添加学生信息、更新学生成绩等。可以使用@Service注解来标注服务类。 5. 实现控制器层 使用SpringMVC框架实现控制器层,处理前端请求并调用业务逻辑层实现相应的操作。可以使用@Controller注解来标注控制器类。 6. 实现前端页面 使用Layui框架实现前端页面,包括登录页面、学生信息管理页面、成绩查询页面等。在前端页面中,通过Ajax向后台发送请求并获取数据,实现动态更新页面。 7. 运行测试 完成以上步骤后,可以运行测试来验证程序的正确性。可以使用JUnit框架来实现单元测试,或使用Postman等工具来模拟前端请求并验证后台响应。 以上是一个简单的学生成绩管理系统的实现思路,您可以根据具体需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值