mybatis 遍历字段和字段对应的值循环插入
java代码:
/**
* 插入单条数据
* @param request
* @param response
* @param id
* @param str
* @return
* @throws Exception
*/
@RequestMapping(value="insertData")
@ResponseBody
public Map<String,Object> insertData(HttpServletRequest request,
HttpServletResponse response,Integer id,String str) throws Exception{
String key=null;
String value;
String tableName=null;
BusinessDesc businessDesc = businessDescService.queryById(id);
//取出所有字段
List<ColumnRule> columnnlist = columnRuleService.findByBusinessId(id);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
List<String> list2 = new ArrayList<String>();
Map<String,Object> maps=new HashMap<String,Object>();
JSONObject jsonObject = new JSONObject(str);
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()){
Map<String,Object> map=new HashMap<String,Object>();
key = (String) iterator.next();
value = jsonObject.getString(key);
for(ColumnRule rule:columnnlist){
if(rule.getName().equals(key)){
if(rule.getFieldType().contains("int")){
if(value!=null && !"".equals(value)){
map.put("value", Integer.parseInt(value));
}else{
map.put("value",0);
}
}else{
map.put("value", value);
}
}
}
map.put("key", key);
list.add(map);
}
if(businessDesc!=null){
tableName=businessDesc.getTablename();
}
maps.put("list", list);
maps.put("table", tableName);
businessDescService.insertData(maps);
return maps;
}
/**
* 修改单条数据
* @param request
* @param response
* @param dataId
* @param str
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value="updateData")
@ResponseBody
public Map<String,Object> updateData(HttpServletRequest request,
HttpServletResponse response,Integer dataId,String str,Integer id) throws Exception{
String tableName=null;
String key=null;
String value;
Map<String,Object> maps=new HashMap<String,Object>();
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//获取表名
BusinessDesc businessDesc = businessDescService.queryById(dataId);
//取出所有字段
List<ColumnRule> columnnlist = columnRuleService.findByBusinessId(dataId);
JSONObject jsonObject = new JSONObject(str);
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()){
Map<String,Object> map=new HashMap<String,Object>();
key = (String) iterator.next();
value = jsonObject.getString(key);
for(ColumnRule rule:columnnlist){
if(rule.getName().equals(key)){
if(rule.getFieldType().contains("int")){
if(value!=null && !"".equals(value)){
map.put("value", Integer.parseInt(value));
}else{
map.put("value", 0);
}
}else{
if(value!=null &&!"".equals(value)){
if(!key.equals("id")){
map.put("value", value);
}
}
}
}
}
map.put("key", key);
if(key.equals("id")){
map.put("value", id);
}
list.add(map);
}
if(businessDesc!=null){
tableName=businessDesc.getTablename();
}
maps.put("list", list);
maps.put("table", tableName);
maps.put("id", id);
businessDescService.updateData(maps);
return maps;
}
隐射文件:
<insert id="insertData" parameterType="java.util.Map">
insert into ${table}
(<foreach collection="list" item="item" index="index" separator=",">${item.key}</foreach>)
values
(
<foreach collection="list" item="subitem" index="index" separator=",">
#{subitem.value}
</foreach>
)
</insert>
<update id="updateData" parameterType="java.util.Map">
update
${table}
set
<foreach collection="list" item="item" index="index" separator=",">${item.key}=#{item.value}</foreach>
where id=#{id}
</update>
遍历list
dao类:List<Garage> findRecommendGarageById(List<String> list);
xml隐射文件:
<select id="findRecommendGarageById" resultMap="BaseResultMap" resultType="java.util.List">
select <include refid="Base_Column_List" /> from s_garage
<where>
id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</where>
</select>
mybatis 遍历字段和字段对应的值循环插入的更多相关文章
- mysql 动态拼接表字段,值 mybatis 动态获取表字段
-- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...
- Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值
我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...
- SQL Server 按某一字段分组 取 最大 (小)值所在行的数据
SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23 ...
- Mysql JSON字段提取某一个属性值的函数
mysql从5.7开始才支持JSON_EXTRACT等 JSON相关的函数, 项目里用到的mysql是5.6的,需要提取JSON字段里某一个属性值进行统计, 自己写了一个笨的提取方法: CREATE ...
- Python3+SQLAlchemy不使用字段名获取主键值教程
一.说明 1.1 环境说明 user model如下,且其现有一个实例user_inst: class User(Base): __tablename__ = 'users' username = C ...
- powerdesigner 字段添加注释和默认值
powerdesigner 字段添加注释和默认值 2017年01月06日 10:59:02 qingzhuoran 阅读数:27161更多 个人分类: powerdesigner 1.选中表,右键 ...
- 【PowerDesigner】【3】字段添加注释和默认值
问题:最开始生成的Table,表头有些字段没有 解决方案: 1,打开表(双击左键),点击下图圈起来的图标 2,找到comment(注释),勾选 3,找到default value(默认值),勾选 4, ...
- ArcGIS自定义工具箱-自增字段(可以设置初始值和步长)
ArcGIS自定义工具箱-自增字段(可以设置初始值和步长) 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:字段值初始值为设定值(默认为0),每次增加一个步长( ...
随机推荐
- Linux系统下Oracle执行SQL脚本后中文出现乱码解决方法
先确认Oracle的字符集,sqlplus登录Oracle后执行语句: [sql] select userenv('language') from dual; 返回值例如:AMERICAN_AME ...
- GCD详细用法
一.延迟执行 1.介绍 第一种方法,该方法在那个线程调用,那么run就在哪个线程执行(当前线程),通常是主线程. [self performSelector:@selector(run) withOb ...
- .each循环的两种使用方法
- MySQL存储引擎 -- MyISAM(表锁定) 与 InnoDB(行锁定) 锁定机制
前言 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就成为了各种数据库的核心技术之一.本章将对MySQL中两种 ...
- java输入输出--I/O操作基础知识学习
一.java的I/O流 1. 输入流(字节流和字符流,字节流操作的数据单元是8位的字节,字符流操作的是16位的字符)(InputStream 和Reader作为基类) 2.输出流(字节流和字符流,字节 ...
- 使用Eclipse的常见问题整理
我在Eclipse里新建一个android工程的时候附带着产生了一个名字为appcompat_v7的工程,这个工程是干什么用的啊?为何我新建的工程都出错了,错误信息提示为: error: Error ...
- 获取百度搜索结果的真实url以及摘要和时间
利用requests库和bs4实现,demo如下: #coding:utf- import requests from bs4 import BeautifulSoup import bs4 impo ...
- Codeforces 1077E (二分乱搞或者dp)
题意:给你一个数组,可以从中选区若干种元素,但每种元素选区的个数前一种必须是后一种的2倍,选区的任意2种元素不能相同,问可以选取最多的元素个数是多少? 思路1(乱搞):记录一下每种元素的个数,然后暴力 ...
- 【摘自张宴的"实战:Nginx"】nginx模块开发
Nginx的模块不能够像Apache那样动态的加载,所以模块都要预先编译进Nginx的二进制可执行文件中. Nginx的模块有三种角色: 1. Handler(处理模块) 用于处理Http请求 ...
- BuilderPattern(23种设计模式之一)
设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...