背景介绍
写这篇文章的动机:我在抖音上一个视频中看到孩子洗了碗,家长翻一本打印的册子,从里面找表扬的话。这个册子左侧写了孩子的行为,比如:洗碗,右边写了家长应该说的表扬的话。
所以,我寻思写一个程序让这件事变得更方便,家长只需要输入孩子的行为,便能查询出相应的话语。
编程过程
child表的create语句
CREATE TABLE `child` (
`id` int NOT NULL AUTO_INCREMENT,
`child` varchar(45) DEFAULT NULL,
`parent` varchar(450) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
child表导出的csv文件
id,child,parent
1,赖床,坐在旁边看着孩子
2,网络成瘾,告诉孩子你有“网瘾”,就像告诉抽烟的人,你患了“烟瘾”一样
3,望子成龙,对孩子的期望要设置到合适的水平上,目标要跳一跳就能碰到
4,学英语,13岁之前学
5,攀比,让孩子做家务换取报酬
6,犯错,不允许孩子犯错,本质上就是剥夺孩子的自由
7,不知道做什么,当你不知道做什么时,就什么都不做
8,孩子让你不舒服,说出不舒服的感受,而不是直接批评
9,对孩子讲道理,孩子需要的是同理心,不是道理
10,何时进行教育,用餐或睡前不要教育
11,富养vs穷养,富养内心,穷养外在
12,用手抓饭,不放纵
13,打碎杯子,不责骂,说:“好漂亮的牛奶海洋。”然后跟孩子一起找可以替代杯子喝水的容器
14,装饰房间,建议孩子学习剪纸装饰自己的房间
15,磕碰到桌子,对孩子说:“桌子也会疼,让我们一起安慰桌子好不好?”
16,被老师批评,倾听孩子的感受,不说老师的坏话
17,哭闹着要玩具,与孩子约定,多长时间买一个新玩具;鼓励孩子与其他小朋友交换玩具
18,爱的条件,不说“我为你付出那么多,你还不听我的话?”
child.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<th:block th:include="include :: header('孩子行为列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
<div class="row">
<div class="col-sm-12 search-collapse">
<form id="formId">
<div class="select-list">
<ul>
<li>
<label>孩子行为:</label>
<input type="text" name="child"/>
</li>
<li>
<label>家长语言:</label>
<input type="text" name="parent"/>
</li>
<li>
<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i> 搜索</a>
<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i> 重置</a>
</li>
</ul>
</div>
</form>
</div>
<div class="btn-group-sm" id="toolbar" role="group">
<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:child:add">
<i class="fa fa-plus"></i> 添加
</a>
<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:child:edit">
<i class="fa fa-edit"></i> 修改
</a>
<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:child:remove">
<i class="fa fa-remove"></i> 删除
</a>
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:child:export">
<i class="fa fa-download"></i> 导出
</a>
</div>
<div class="col-sm-12 select-table table-striped">
<table id="bootstrap-table"></table>
</div>
</div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
var editFlag = [[${@permission.hasPermi('system:child:edit')}]];
var removeFlag = [[${@permission.hasPermi('system:child:remove')}]];
var prefix = ctx + "system/child";
$(function() {
var options = {
url: prefix + "/list",
createUrl: prefix + "/add",
updateUrl: prefix + "/edit/{id}",
removeUrl: prefix + "/remove",
exportUrl: prefix + "/export",
modalName: "孩子行为",
columns: [{
checkbox: true
},
{
field: 'id',
title: '编号',
visible: false
},
{
field: 'child',
title: '孩子行为'
},
{
field: 'parent',
title: '家长语言'
},
{
title: '操作',
align: 'center',
formatter: function(value, row, index) {
var actions = [];
actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" οnclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" οnclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
return actions.join('');
}
}]
};
$.table.init(options);
});
</script>
</body>
</html>
ChildController.java
package com.ruoyi.web.controller.system;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Child;
import com.ruoyi.system.service.IChildService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
* 孩子行为Controller
*
* @author ruoyi
* @date 2024-04-28
*/
@Controller
@RequestMapping("/system/child")
public class ChildController extends BaseController
{
private String prefix = "system/child";
@Autowired
private IChildService childService;
@RequiresPermissions("system:child:view")
@GetMapping()
public String child()
{
return prefix + "/child";
}
/**
* 查询孩子行为列表
*/
@RequiresPermissions("system:child:list")
@PostMapping("/list")
@ResponseBody
public TableDataInfo list(Child child)
{
startPage();
List<Child> list = childService.selectChildList(child);
return getDataTable(list);
}
/**
* 导出孩子行为列表
*/
@RequiresPermissions("system:child:export")
@Log(title = "孩子行为", businessType = BusinessType.EXPORT)
@PostMapping("/export")
@ResponseBody
public AjaxResult export(Child child)
{
List<Child> list = childService.selectChildList(child);
ExcelUtil<Child> util = new ExcelUtil<Child>(Child.class);
return util.exportExcel(list, "孩子行为数据");
}
/**
* 新增孩子行为
*/
@GetMapping("/add")
public String add()
{
return prefix + "/add";
}
/**
* 新增保存孩子行为
*/
@RequiresPermissions("system:child:add")
@Log(title = "孩子行为", businessType = BusinessType.INSERT)
@PostMapping("/add")
@ResponseBody
public AjaxResult addSave(Child child)
{
return toAjax(childService.insertChild(child));
}
/**
* 修改孩子行为
*/
@RequiresPermissions("system:child:edit")
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, ModelMap mmap)
{
Child child = childService.selectChildById(id);
mmap.put("child", child);
return prefix + "/edit";
}
/**
* 修改保存孩子行为
*/
@RequiresPermissions("system:child:edit")
@Log(title = "孩子行为", businessType = BusinessType.UPDATE)
@PostMapping("/edit")
@ResponseBody
public AjaxResult editSave(Child child)
{
return toAjax(childService.updateChild(child));
}
/**
* 删除孩子行为
*/
@RequiresPermissions("system:child:remove")
@Log(title = "孩子行为", businessType = BusinessType.DELETE)
@PostMapping( "/remove")
@ResponseBody
public AjaxResult remove(String ids)
{
return toAjax(childService.deleteChildByIds(ids));
}
}
Child.java
package com.ruoyi.system.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
/**
* 孩子行为对象 child
*
* @author ruoyi
* @date 2024-04-28
*/
public class Child extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 编号 */
private Long id;
/** 孩子行为 */
@Excel(name = "孩子行为")
private String child;
/** 家长语言 */
@Excel(name = "家长语言")
private String parent;
public void setId(Long id)
{
this.id = id;
}
public Long getId()
{
return id;
}
public void setChild(String child)
{
this.child = child;
}
public String getChild()
{
return child;
}
public void setParent(String parent)
{
this.parent = parent;
}
public String getParent()
{
return parent;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("child", getChild())
.append("parent", getParent())
.toString();
}
}
ChildServiceImpl.java
package com.ruoyi.system.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ChildMapper;
import com.ruoyi.system.domain.Child;
import com.ruoyi.system.service.IChildService;
import com.ruoyi.common.core.text.Convert;
/**
* 孩子行为Service业务层处理
*
* @author ruoyi
* @date 2024-04-28
*/
@Service
public class ChildServiceImpl implements IChildService
{
@Autowired
private ChildMapper childMapper;
/**
* 查询孩子行为
*
* @param id 孩子行为主键
* @return 孩子行为
*/
@Override
public Child selectChildById(Long id)
{
return childMapper.selectChildById(id);
}
/**
* 查询孩子行为列表
*
* @param child 孩子行为
* @return 孩子行为
*/
@Override
public List<Child> selectChildList(Child child)
{
return childMapper.selectChildList(child);
}
/**
* 新增孩子行为
*
* @param child 孩子行为
* @return 结果
*/
@Override
public int insertChild(Child child)
{
return childMapper.insertChild(child);
}
/**
* 修改孩子行为
*
* @param child 孩子行为
* @return 结果
*/
@Override
public int updateChild(Child child)
{
return childMapper.updateChild(child);
}
/**
* 批量删除孩子行为
*
* @param ids 需要删除的孩子行为主键
* @return 结果
*/
@Override
public int deleteChildByIds(String ids)
{
return childMapper.deleteChildByIds(Convert.toStrArray(ids));
}
/**
* 删除孩子行为信息
*
* @param id 孩子行为主键
* @return 结果
*/
@Override
public int deleteChildById(Long id)
{
return childMapper.deleteChildById(id);
}
}
ChildMapper.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">
<mapper namespace="com.ruoyi.system.mapper.ChildMapper">
<resultMap type="Child" id="ChildResult">
<result property="id" column="id" />
<result property="child" column="child" />
<result property="parent" column="parent" />
</resultMap>
<sql id="selectChildVo">
select id, child, parent from child
</sql>
<select id="selectChildList" parameterType="Child" resultMap="ChildResult">
<include refid="selectChildVo"/>
<where>
<if test="child != null and child != ''"> and child like concat('%', #{child}, '%')</if>
<if test="parent != null and parent != ''"> and parent like concat('%', #{parent}, '%')</if>
</where>
</select>
<select id="selectChildById" parameterType="Long" resultMap="ChildResult">
<include refid="selectChildVo"/>
where id = #{id}
</select>
<insert id="insertChild" parameterType="Child" useGeneratedKeys="true" keyProperty="id">
insert into child
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="child != null">child,</if>
<if test="parent != null">parent,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="child != null">#{child},</if>
<if test="parent != null">#{parent},</if>
</trim>
</insert>
<update id="updateChild" parameterType="Child">
update child
<trim prefix="SET" suffixOverrides=",">
<if test="child != null">child = #{child},</if>
<if test="parent != null">parent = #{parent},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteChildById" parameterType="Long">
delete from child where id = #{id}
</delete>
<delete id="deleteChildByIds" parameterType="String">
delete from child where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>