这个是基于SSM框架,用AJAX写得动态表格,可以单行入库,也可多行入库。写得不是很完善,欢迎指正!
1,view层
<tbody>
<body>
<table cellspacing="0" cellpadding="0" border="1"
class="normTbe tabInfo">
<thead>
<tr>
<th>姓名</th>
<th>工资</th>
<th>奖金</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tbodyinsert"></tbody>
<input type="button" id="smbut" value="保存">
<input type="button" value="添加" id="insertBtn">
</table>
</body>
<script type="text/javascript">
function deltr(opp) {
$(opp).parent().parent().remove();//移除当前行
}
$().ready(
function() {
//点击保存,把循环的值放到json传到后台
$("#smbut").click(
function() {
$.ajax({
url : '${path}/dynamic',
data : JSON.stringify(getJson()),
dataType : "json",
type : "post",
contentType : "application/json",
success : function(data) {
if (data=="1" ) {
window.location.href = "${path}/findemp";
} else {
alert("失败");
}
}
})
});
//点击动态添加需要的列
$("#insertBtn").click(
function() {
var tr = $("<tr class='t'></tr>");
tr.append("<td><input type='text' id='ename' name='ename' ></td>");
tr.append("<td><input type='text' id='Sal' name='Sal' ></td>");
tr.append("<td><input type='text' id='comm' name='comm' ></td>");
tr.append("<td><a href='javascript:;' name='delete' onClick='deltr(this)'>删除</a></td>");
$("#tbodyinsert").append(tr);
});
//循环遍历,把值放到数组中
function getJson() {
var jsonArray = new Array();
$("#tbodyinsert.t").each(
function() {
var ename = $(this).find("input[name='ename']").val();
var Sal = $(this).find("input[name='Sal']").val();
var comm = $(this).find("input[name='comm']").val();
var obj = new Array();
obj = {"ename" : ename,
"Sal" : Sal,
"comm" : comm};
jsonArray.push(obj);});
return {
"jsonArray" : jsonArray
}
}
});
</script>
2,controller层
@ResponseBody
@RequestMapping("/dynamic")
public String dynamicFrom(@RequestBody Map list){
List jsonInfo= (List) list.get("jsonArray");
bizemp.insertDynamic(jsonInfo);
return "1";
}
3,service层
/**
* 动态新增
*/
public void insertDynamic(List jsonInfo) {
for (int i=0;i<jsonInfo.size();i++) {
Map m=(Map) jsonInfo.get(i);
map.insertDynamic(jsonInfo);
}
}
4.dao层
<sql id="allColumns" >empno,ename,sal,comm,hiredate</sql>
<insert id="insertDynamic" parameterType="java.util.List">
insert into emp
<trim prefix="(" suffix=")" >
<include refid="allColumns" />
</trim>
select xu_index.nextval,t.* from(
<foreach collection="list" item="item" index="index" separator="union all">
select
#{item.ename} as ename,
#{item.Sal} as Sal,
#{item.comm} as comm,
sysdate as hiredate
from dual
</foreach>
) t
</insert>
5.MyBatis的foreach语句详解
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
下面分别来看看上述三种情况的示例代码:
1.单参数List的类型:
<select id="dynamicForeachTest"resultType="Blog">
select *from t_blog where id in
<foreach collection="list" index="index" item="item"open="(" separator="," close=")">
#{item}
</foreach>
</select>
上述collection的值为list,对应的Mapper是这样的
List<Integer> ids = newArrayList<Integer>();
ids.add(1);
ids.add(3);
ids.add(6);
2.单参数array数组的类型:
<select id="dynamicForeach2Test"resultType="Blog">
select *from t_blog where id in
<foreach collection="array" index="index"item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
上述collection为array,对应的Mapper代码:
3.自己把参数封装成Map的类型
<select id="dynamicForeach3Test"resultType="Blog">
select *from t_blog where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item"open="(" separator="," close=")">
#{item}
</foreach>
</select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
List<Integer> ids = newArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
ids.add(6);
ids.add(7);
ids.add(9);
Map<String, Object> params = newHashMap<String, Object>();
params.put("ids", ids);
params.put("title", "中国");
6.批量sql写法
批量查询
<select id="selectBySomePoiIds" resultType="list" parameterType="java.util.Map">
SELECT
<include refid="Base_Column_List" /> FROM 表名 WHERE poi_id in
<foreach collection="poiIds" item="poiId" index="index" open="(" close=")" separator=",">
#{poiId}
</foreach>
AND pass_uid = #{passUid}
<if test="status != null">
AND status = #{status,jdbcType=BIGINT}
</if>
</select>
批量新增
<insert id="insertBatch" >
insert into 表名 ( uid, groupon_id, create_time, receive_time) values
<foreach collection="list" item= "item" index ="index" separator=",">
(#{item.uid,jdbcType=BIGINT}, #{item.grouponId,jdbcType=BIGINT},
#{item.createTime,jdbcType=INTEGER}, #{item.receiveTime,jdbcType=INTEGER})
</foreach >
</insert>
批量更新
<update id= "updateSubmitTimeByUids" parameterType= "map">
update 表名
set submit_time = #{submitTime,jdbcType=BIGINT} where uid in
<foreach collection="list" item= "uid" index ="index"
open= "(" close =")" separator=",">
#{ uid}
</foreach >
</update >
批量删除
<delete id= "deleteBatchByXXX" parameterType= "list">
delete from 表名 where groupon_id in
<foreach collection="list" item= "item" index ="index"
open= "(" close =")" separator=",">
#{item}
</foreach >
</delete >