easyui data simple addition, deletion and modification

First build a table, just 3 fields, id_, name_, age_

and then write the entity class
package com.hgf.ssm.dao.po;

/**
 * Created by hasee on 2017/2/9.
 */
public class UsersPO {

    public int id_;
    public String name_;
    public int age_;

    public int getId_() {
        return id_;
    }

    public void setId_(int id_) {
        this.id_ = id_;
    }

    public String getName_() {
        return name_;
    }

    public void setName_(String name_) {
        this.name_ = name_;
    }

    public int getAge_() {
        return age_;
    }

    public void setAge_(int age_) {
        this.age_ = age_;
    }

    @Override
    public String toString() {
        return "UsersPO{" +
                "id_=" + id_ +
                ", name_='" + name_ + '\'' +
                ", age_=" + age_ +
                '}';
    }
}

then mapper
package com.hgf.ssm.dao.mapper;


import com.hgf.ssm.dao.po.UsersPO;

import java.util.List;

/**
 * <b>tax_out_ticket[tax_out_ticket]Data access interface</b>
 * <p/>
 * <p>
 * Note: This file is automatically generated by the AOS platform - manual modification is prohibited
 * </p>
 *
 * @author AHei
 * @date 2016-09-12 11:53:45
 */

public interface UsersMapper {
    /**
     * Query all records
     *
     * @return
     */
    List<UsersPO> listPage();

    /**
     * insert a piece of data
     *
     * @param usersPO
     * @return
     */
    int insert(UsersPO usersPO);

    /**
     * Update a piece of data
     *
     * @param usersPO
     * @return
     */
    int update(UsersPO usersPO);

    /**
     * delete a piece of data
     * @param id_
     * @return
     */
    int delete(int id_);
}

<?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">

<!-- tax_out_ticket[tax_out_ticket]SQLMapper automatic mapping -->
<!-- Note: This file is automatically generated by the AOS platform - manual modification is prohibited 2016-09-12 11:53:45 -->
<mapper namespace="com.hgf.ssm.dao.mapper.UsersMapper">

    <select id="listPage" resultType="UsersPO">
		SELECT
		*
		FROM users

	</select>
    <insert id="insert" parameterType="UsersPO">
        INSERT INTO users (
        <if test="name_!=null and name_!=''">
            name_,
        </if>
        <if test="age_!=null and age_!=''">
            age_,
        </if>
        <if test="id_!=null and id_!=''">
            id_
        </if>)VALUES (

        <if test="name_!=null and name_!=''">
            #{name_, jdbcType=VARCHAR},
        </if>
        <if test="age_!=null and age_!=''">
            #{age_, jdbcType=INTEGER},
        </if>
        <if test="id_!=null and id_!=''">
            #{id_, jdbcType=INTEGER}
        </if>)

    </insert>
    <update id="update" parameterType="UsersPO">
        UPDATE users
        <set>
            <if test="name_!=null ">
                name_ = #{name_,jdbcType=VARCHAR},
            </if>
            <if test="age_!=null">
                age_ = #{age_,jdbcType=INTEGER}
            </if>

        </set>
        WHERE id_=#{id_,jdbcType=INTEGER}
    </update>
    <delete id="delete">
        DELETE  FROM users WHERE id_ = #{id_}

    </delete>
</mapper>

then service
package com.hgf.ssm.service;


import com.hgf.ssm.dao.po.UsersPO;

import java.util.List;

/**
 * Created by hasee on 2016/12/30.
 */
public interface UserstService {
    /**
     * Query all records
     * @return
     */
    public List<UsersPO> listPage();

    /**
     * insert a piece of data
     * @param usersPO
     * @return
     */
    public int insert(UsersPO usersPO);

    /**
     * Update a piece of data
     * @param usersPO
     * @return
     */
    public int update(UsersPO usersPO);

    /**
     * delete a piece of data
     * @param id_
     * @return
     */
    public int delete(int id_);
}

package com.hgf.ssm.service.impl;

import com.hgf.ssm.dao.mapper.UsersMapper;
import com.hgf.ssm.dao.po.UsersPO;
import com.hgf.ssm.service.UserstService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hasee on 2016/12/30.
 */
@Service
public class UsersServiceImpl implements UserstService {

    @Resource
    private UsersMapper usersMapper;


    public List<UsersPO> listPage() {
        return usersMapper.listPage();
    }

    @Override
    public int update(UsersPO usersPO) {
        return usersMapper.update(usersPO);
    }

    @Override
    public int delete(int id_) {
        return usersMapper.delete(id_);
    }

    @Override
    public int insert(UsersPO usersPO) {
        return usersMapper.insert(usersPO);
    }
}

then controller
package com.hgf.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.hgf.ssm.dao.po.UsersPO;
import com.hgf.ssm.service.UserstService;
import com.hgf.ssm.util.AttributereplicationUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hasee on 2016/11/24.
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {
    @Resource
    private UserstService userstService;

    @RequestMapping(value = "/test")
    public String testSelect(){
        System.out.println(userstService.listPage().size());
        return "test";
    }
    @RequestMapping(value = "/easyui")
    public String easyui(){
        return "easyui_crud";
    }

    /**
     * Inquire
     * @return json
     */
    @RequestMapping(value = "/e_query")
    @ResponseBody
    public Object e_query(){
        List<UsersPO> list = userstService.listPage();
        ObjectMapper mapper = new ObjectMapper();
       // String listjson = JSON.toJSONString(list);

        return JSON.toJSON(list);
    }

    /**
     * new
     * @param request
     * @param response
     */
    @RequestMapping(value = "/e_insert")
    public void e_insert(HttpServletRequest request, HttpServletResponse response){
        //get form data
        Map<String,String[]> map = request.getParameterMap();
        UsersPO usersPO = new UsersPO();
        //Copy map data to javabean usersPO
        AttributereplicationUtils.copyProperties(map,usersPO);
        //add
        int a =  userstService.insert(usersPO);
        //return map
        Map<String,Object> outMap = new HashMap<String,Object>();
        if(a==1){
            outMap.put("issuccess",true);
            outMap.put("msg","Added successfully");
        }else{
            outMap.put("issuccess",false);
            outMap.put("msg","Add failed");
        }
        try {
            response.getWriter().write(JSON.toJSONString(outMap));
        }catch (IOException e){
            e.printStackTrace ();
        }
    }

    /**
     * renew
     * @param request
     * @param response
     */
    @RequestMapping(value = "/e_update")
    public void e_update(HttpServletRequest request, HttpServletResponse response){
        Map<String,String[]> map = request.getParameterMap();
        UsersPO usersPO = new UsersPO();
        //Copy map data to javabean usersPO
        AttributereplicationUtils.copyProperties(map,usersPO);
        int a =  userstService.update(usersPO);
        //return map
        Map<String,Object> outMap = new HashMap<String,Object>();
        if(a==1){
            outMap.put("issuccess",true);
            outMap.put("msg","Modified successfully");
        }else{
            outMap.put("issuccess",false);
            outMap.put("msg","modification failed");
        }
        try {
            response.getWriter().write(JSON.toJSONString(outMap));
        }catch (IOException e){
            e.printStackTrace ();
        }
    }
    @RequestMapping(value = "/e_delete")
    public void delete(HttpServletRequest request, HttpServletResponse response){
        int id_ = Integer.valueOf(request.getParameter("id_"));
        int a =  userstService.delete(id_);
        //return map
        Map<String,Object> outMap = new HashMap<String,Object>();
        if(a==1){
            outMap.put("issuccess",true);
            outMap.put("msg","Delete successful");
        }else{
            outMap.put("issuccess",false);
            outMap.put("msg","Deletion failed");
        }
        try {
            response.getWriter().write(JSON.toJSONString(outMap));
        }catch (IOException e){
            e.printStackTrace ();
        }
    }

}

jsp page
<%--
  Created by IntelliJ IDEA.
  User: hasee
  Date: 2017/2/9
  Time: 15:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
    <!-- Import jQuery plugin package -->
    <script type='text/javascript' src="/ssm/static/jquery-easyui-1.5.1/jquery.min.js"></script>
    <!-- Import the plugin package of jQuery EasyUI-->
    <script type='text/javascript' src="/ssm/static/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
    <!-- Import the default easyui css package-->
    <link rel="stylesheet" type="text/css" href="/ssm/static/jquery-easyui-1.5.1/themes/default/easyui.css"/>
    <!-- import icon pack-->
    <link rel="stylesheet" type="text/css" href="/ssm/static/jquery-easyui-1.5.1/themes/icon.css"/>
    <link rel="stylesheet" type="text/css" href="/ssm/static/jquery-easyui-1.5.1/demo/demo.css"/>
</head>
</head>
<body>
<%--Form body--%>
<table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px"
       url="e_query.do"
       toolbar="#toolbar"
       rownumbers="true" fitColumns="true" singleSelect="true">
    <thead>
    <tr>
        <th field="id_" width="50">ID_</th>
        <th field="name_" width="50">姓名</th>
        <th field="age_" width="50">年龄</th>

    </tr>
    </thead>
</table>
<%--Table Toolbar--%>
<div id="toolbar">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新增用户</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">修改用户</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除用户</a>
</div>
<%--Add and modify pop-up windows--%>
<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
     closed="true" buttons="#dlg-buttons">
    <div class="ftitle">User Information</div>
    <form id="fm" method="post">
        <div class="fitem">
            <label>id_</label>
            <input name="id_">
        </div>
        <div class="fitem">
            <label>姓名</label>
            <input name="name_">
        </div>
        <div class="fitem">
            <label>Age</label>
            <input name="age_">
        </div>

    </form>
</div>
<%--Popup button--%>
<div id="dlg-buttons">
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">保存</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>
<script type="text/javascript">
    var url;//Pop-up save button url
    //Add user window
    function newUser() {
        $('#dlg').dialog('open').dialog('setTitle', 'New User');
        $('#fm').form('clear'); //Clear form data
        url = "e_insert.do";
    }
    //modify the user window
    function editUser() {
        var row = $('#dg').datagrid('getSelected');//Get the selected row record
        if (row) {
            $('#dlg').dialog('open').dialog('setTitle', 'Edit User');
            $('#fm').form('load', row); //Load data to fill the form
        }
        url = "e_update.do";
    }
    // save the user
    function saveUser() {
        $('#fm').form('submit', {
            url: url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (result.issuccess === false) {
                    $.messager.show({
                        title: 'Error',
                        msg: result.msg
                    });
                } else {
                    $ .messager.alert ({
                        title: 'success',
                        msg: result.msg,
                        icon: 'info'
                    });
                    $('#dlg').dialog('close'); // close the popup
                    $('#dg').datagrid('reload'); // reload data
                }
            }
        });
    }
    //delete users
    function destroyUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', 'Are you sure you want to delete this user?', function (r) {
                if (r) {
                    $.post('e_delete.do', {id_: row.id_}, function (result) {
                        var result = eval('(' + result + ')');
                        if (result.issuccess===true) {
                            $('#dg').datagrid('reload');	// reload the user data
                        } else {
                            $.messager.show({	// show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    });
                }
            });
        }
    }

</script>
</body>
</html>


That's it.
Reference: http://www.jeasyui.net/tutorial/147.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326978519&siteId=291194637