datagrid---写后台数据交互

1.action的写法

开头写包,此外,我们还有一个和action并列的package-info.java的文件,该文件是包的信息,media为我的文件夹里面放三个文件夹(action,bpo,mapper)

@org.apache.struts2.convention.annotation.Namespace("/media")
package com.****.***.media.action;

package com.****.***.media.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.****.***.common.web.BaseAction;//导入基本的baseaction文件
import com.****.***.media.bpo.MediaManageBPO;//导入bpo
public class MediaManageAction extends BaseAction {//继承基本的baseaction

然后在类中写

private static final long serialVersionUID = 3791581176290177353L;

private String MEDIA_ID;//一般是和数据库对应的字段
private String MEDIA_NAME;
private String MEDIA_DOMAIN;
private String MEDIA_TYPE;

@Resource
private MediaManageBPO mediaManageBPO;

/**
* 获取所有数据库中媒体的信息
* @return
*/
@Action(value="getMediaInfo")
public String getMediaInfo(){
//定义一个列表,列表中保存多个map对象
List<Map<String,Object>> rsList = new ArrayList<Map<String,Object>>();
rsList = mediaManageBPO.getMediaInfo();
outputJson(rsList);
return NONE;
}


/**
* 查询数据库中媒体的信息
* @return
*/
@Action(value="searchMediaInfo")
public String searchMediaInfo(){
//定义一个map,保存多个键值对
Map<String,Object> argMap = new HashMap<String,Object>();
argMap.put("MEDIA_ID", MEDIA_ID);
argMap.put("MEDIA_NAME", MEDIA_NAME);
argMap.put("MEDIA_DOMAIN", MEDIA_DOMAIN);
argMap.put("MEDIA_TYPE", MEDIA_TYPE);
//定义一个列表,列表中保存多个map对象
List<Map<String,Object>> rsList = new ArrayList<Map<String,Object>>();
rsList = mediaManageBPO.searchMediaInfo(argMap);
outputJson(rsList);
return NONE;
}


/**
* 添加数据库中媒体的信息
* @return
*/
@Action(value="addMediaInfo")
public String addMediaInfo(){
//定义一个map,保存多个键值对
Map<String,Object> argMap = new HashMap<String,Object>();
argMap.put("MEDIA_ID", MEDIA_ID);
argMap.put("MEDIA_NAME", MEDIA_NAME);
argMap.put("MEDIA_DOMAIN", MEDIA_DOMAIN);
argMap.put("MEDIA_TYPE", MEDIA_TYPE);
//定义一个列表,列表中保存多个map对象
mediaManageBPO.addMediaInfo(argMap);
outputString("SUCCESS");
return NONE;
}


/**
* 删除数据库中媒体的信息
* @return
*/
@Action(value="removeMediaInfo")
public String removeMediaInfo(){
//定义一个map,保存多个键值对
Map<String,Object> argMap = new HashMap<String,Object>();
argMap.put("MEDIA_ID", MEDIA_ID);
argMap.put("MEDIA_NAME", MEDIA_NAME);
argMap.put("MEDIA_DOMAIN", MEDIA_DOMAIN);
argMap.put("MEDIA_TYPE", MEDIA_TYPE);
//定义一个列表,列表中保存多个map对象
mediaManageBPO.removeMediaInfo(argMap);
outputString("SUCCESS");
return NONE;
}


/**
* 修改数据库中媒体的信息
* @return
*/
@Action(value="editMediaInfo")
public String editMediaInfo(){
//定义一个map,保存多个键值对
Map<String,Object> argMap = new HashMap<String,Object>();
argMap.put("MEDIA_ID", MEDIA_ID);
argMap.put("MEDIA_NAME", MEDIA_NAME);
argMap.put("MEDIA_DOMAIN", MEDIA_DOMAIN);
argMap.put("MEDIA_TYPE", MEDIA_TYPE);
//定义一个列表,列表中保存多个map对象
mediaManageBPO.editMediaInfo(argMap);
outputString("SUCCESS");
return NONE;
}


public String getMEDIA_ID() {
return MEDIA_ID;
}

public void setMEDIA_ID(String mEDIA_ID) {
MEDIA_ID = mEDIA_ID;
}

public String getMEDIA_NAME() {
return MEDIA_NAME;
}

public void setMEDIA_NAME(String mEDIA_NAME) {
MEDIA_NAME = mEDIA_NAME;
}

public String getMEDIA_DOMAIN() {
return MEDIA_DOMAIN;
}

public void setMEDIA_DOMAIN(String mEDIA_DOMAIN) {
MEDIA_DOMAIN = mEDIA_DOMAIN;
}

public String getMEDIA_TYPE() {
return MEDIA_TYPE;
}

public void setMEDIA_TYPE(String mEDIA_TYPE) {
MEDIA_TYPE = mEDIA_TYPE;
}

2.写bpo

@Resource
MediaManageMapper mediaManageMapper;

//获取媒体信息
public List<Map<String, Object>> getMediaInfo() {
return mediaManageMapper.getMediaInfo();
}

//查询媒体信息
public List<Map<String, Object>> searchMediaInfo(Map<String, Object> argMap) {
return mediaManageMapper.searchMediaInfo(argMap);
}

//添加媒体信息
public void addMediaInfo(Map<String, Object> argMap) {
mediaManageMapper.addMediaInfo(argMap);
}

//删除媒体信息
public void removeMediaInfo(Map<String, Object> argMap) {
mediaManageMapper.removeMediaInfo(argMap);
}

//修改媒体信息
public void editMediaInfo(Map<String, Object> argMap) {
mediaManageMapper.editMediaInfo(argMap);
}

3.写mapper

(mapper中定义接口,getMediaInfo()和searchMediaInfo()方法有返回值,后面的三个方法没有返回值)

public interface MediaManageMapper {

//获取媒体信息
List<Map<String, Object>> getMediaInfo();

//查询媒体信息
List<Map<String, Object>> searchMediaInfo(Map<String, Object> argMap);

//添加媒体信息
void addMediaInfo(Map<String, Object> argMap);

//删除媒体信息
void removeMediaInfo(Map<String, Object> argMap);

//修改媒体信息
void editMediaInfo(Map<String, Object> argMap);
}

4.写xml(mybatis中的sql语句)

<?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-->
<mapper namespace="com.****.***.media.mapper.MediaManageMapper">

<!-- SQL查询语句 ,获取表的所有内容-->

<select id="getMediaInfo" parameterType="map" resultType="map">
select t.* from wpgl.wp_assess_media t
</select>

<!-- SQL查询语句 ,查询表的内容-->
<select id="searchMediaInfo" parameterType="map" resultType="map">
select t.* from wpgl.wp_assess_media t
<where>
<if test="MEDIA_ID!=null and MEDIA_ID!=''">
t.MEDIA_ID = #{MEDIA_ID}
</if>
<if test="MEDIA_NAME!=null and MEDIA_NAME!=''">
and t.MEDIA_NAME = #{MEDIA_NAME}
</if>
<if test="MEDIA_DOMAIN!=null and MEDIA_DOMAIN!=''">
and t.MEDIA_DOMAIN = #{MEDIA_DOMAIN}
</if>
<if test="MEDIA_TYPE!=null and MEDIA_TYPE!=''">
and t.MEDIA_TYPE = #{MEDIA_TYPE}
</if>
</where>
</select>

<!-- SQL插入语句 ,输入输出参数类型都定义为map类型-->

<insert id="addMediaInfo" parameterType="map" >
insert into wpgl.wp_assess_media t (t.MEDIA_ID,t.MEDIA_NAME,t.MEDIA_DOMAIN,t.MEDIA_TYPE)
values(#{MEDIA_ID},#{MEDIA_NAME},#{MEDIA_DOMAIN},#{MEDIA_TYPE})
</insert>


<!-- SQL删除语句 ,输入输出参数类型都定义为map类型-->

<delete id="removeMediaInfo" parameterType="map" >
delete from wpgl.wp_assess_media t
<where>
<if test="MEDIA_NAME!=null and MEDIA_NAME!=''">
t.MEDIA_NAME = #{MEDIA_NAME}
</if>
</where>
</delete>

<!-- SQL修改语句 ,输入输出参数类型都定义为map类型-->

<update id="editMediaInfo" parameterType="map" >
update wpgl.wp_assess_media t
<set>
<if test="MEDIA_NAME!=null and MEDIA_NAME!=''">
t.MEDIA_NAME = #{MEDIA_NAME} ,
</if>
<if test="MEDIA_DOMAIN!=null and MEDIA_DOMAIN!=''">
t.MEDIA_DOMAIN = #{MEDIA_DOMAIN} ,
</if>
<if test="MEDIA_TYPE!=null and MEDIA_TYPE!=''">
t.MEDIA_TYPE = #{MEDIA_TYPE}
</if>
</set>
<where>
<if test="MEDIA_ID!=null and MEDIA_ID!=''">
t.MEDIA_ID = #{MEDIA_ID}
</if>
</where>

</update>

</mapper>

上一篇:Python中的高级数据结构


下一篇:CLOSE_WAIT问题-TCP