easyui 数据简单的增删改查

首先建张表,就3个字段,id_,name_,age_

然后写实体类

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_ +
'}';
}
}

然后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]数据访问接口</b>
* <p/>
* <p>
* 注意:此文件由AOS平台自动生成-禁止手工修改
* </p>
*
* @author AHei
* @date 2016-09-12 11:53:45
*/

public interface UsersMapper {
/**
* 查询所有记录
*
* @return
*/
List<UsersPO> listPage();

/**
* 插入一条数据
*
* @param usersPO
* @return
*/
int insert(UsersPO usersPO);

/**
* 更新一条数据
*
* @param usersPO
* @return
*/
int update(UsersPO usersPO);

/**
* 删除一条数据
* @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自动映射 -->
<!-- 注意:此文件由AOS平台自动生成-禁止手工修改 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>

然后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 {
/**
* 查询所有记录
* @return
*/
public List<UsersPO> listPage();

/**
* 插入一条数据
* @param usersPO
* @return
*/
public int insert(UsersPO usersPO);

/**
* 更新一条数据
* @param usersPO
* @return
*/
public int update(UsersPO usersPO);

/**
* 删除一条数据
* @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);
}
}

然后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";
}

/**
* 查询
* @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);
}

/**
* 新增
* @param request
* @param response
*/
@RequestMapping(value = "/e_insert")
public void e_insert(HttpServletRequest request, HttpServletResponse response){
//获取表单数据
Map<String,String[]> map = request.getParameterMap();
UsersPO usersPO = new UsersPO();
//将map数据复制到javabean usersPO中
AttributereplicationUtils.copyProperties(map,usersPO);
//新增
int a = userstService.insert(usersPO);
//返回map
Map<String,Object> outMap = new HashMap<String,Object>();
if(a==1){
outMap.put("issuccess",true);
outMap.put("msg","新增成功");
}else{
outMap.put("issuccess",false);
outMap.put("msg","新增失败");
}
try {
response.getWriter().write(JSON.toJSONString(outMap));
}catch (IOException e){
e.printStackTrace();
}
}

/**
* 更新
* @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();
//将map数据复制到javabean usersPO中
AttributereplicationUtils.copyProperties(map,usersPO);
int a = userstService.update(usersPO);
//返回map
Map<String,Object> outMap = new HashMap<String,Object>();
if(a==1){
outMap.put("issuccess",true);
outMap.put("msg","修改成功");
}else{
outMap.put("issuccess",false);
outMap.put("msg","修改失败");
}
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_);
//返回map
Map<String,Object> outMap = new HashMap<String,Object>();
if(a==1){
outMap.put("issuccess",true);
outMap.put("msg","删除成功");
}else{
outMap.put("issuccess",false);
outMap.put("msg","删除失败");
}
try {
response.getWriter().write(JSON.toJSONString(outMap));
}catch (IOException e){
e.printStackTrace();
}
}

}

jsp页面

<%--
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>
<!-- 导入jQuery插件包 -->
<script type='text/javascript' src="/ssm/static/jquery-easyui-1.5.1/jquery.min.js"></script>
<!-- 导入jQuery EasyUI的插件包 -->
<script type='text/javascript' src="/ssm/static/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<!-- 导入默认的easyui的css包 -->
<link rel="stylesheet" type="text/css" href="/ssm/static/jquery-easyui-1.5.1/themes/default/easyui.css"/>
<!-- 导入图标包-->
<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>
<%--表格主体--%>
<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>
<%--表格工具栏--%>
<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>
<%--新增和修改弹窗--%>
<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>年龄</label>
<input name="age_">
</div>

</form>
</div>
<%--弹窗按钮--%>
<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;//弹窗保存按钮url
//新增用户窗口
function newUser() {
$('#dlg').dialog('open').dialog('setTitle', 'New User');
$('#fm').form('clear'); //清除表单数据
url = "e_insert.do";
}
//修改用户窗口
function editUser() {
var row = $('#dg').datagrid('getSelected');//获取选中行记录
if (row) {
$('#dlg').dialog('open').dialog('setTitle', 'Edit User');
$('#fm').form('load', row); //加载数据来填充表单
}
url = "e_update.do";
}
//保存用户
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'); // 关闭弹窗
$('#dg').datagrid('reload'); // 重新加载数据
}
}
});
}
//删除用户
function destroyUser() {
var row = $('#dg').datagrid('getSelected');
if (row) {
$.messager.confirm('Confirm', '你确定删除这个用户?', 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>


这样就妥了。
参考: http://www.jeasyui.net/tutorial/147.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值