
而在修改数据的时候,考虑到安全性的问题,确保运行不会报异常的话,每次在做修改字段的时候,都需要先查
寻该字段是否存在,若不存在则需要主动添加。如以下的 userId,是并没有外键的!
如果每次都先查询再修改的话,是相当影响效率的,所以呢,mysql也帮我们准备好了解决方式,感谢mysql团队
1. ** 首先,你需要一个触发条件,也就是唯一索引啦,这是必要的!!! 譬如我想我的userId不重复,又想达到该效果,就需要把该字段设置为唯一索引,譬如:
2. 语法: duplicate key
怎么使用已经很明显了吧 :
insert into 表名(字段1,userId,字段3,....) values(值1,值,值3,....) 意思是,当userId不重复时,则会触发这个插入语句
update 字段1=值1,字段2=值2 .... 就是userId的值已经存在时,则触发这个修改语句
那么我的语句就是:
3. 最后
顺带一提,可以在myBatis的xml文件中获取当前时间哦,使用 now()
学而时记之,人笨只能多写啦,加油哦!!!.....
根据客户编号查询唯一的步骤如下:1、在MySQL数据库中,创建一个名为mybatis的数据库,在此数据库中创建一个tcustomer表,同时预先插入几条数据。
2、在Eclipse中,创建一个名为chapter06的Web项目,将MyBatis的核心JAR包、lib目录中的依赖JAR包,以及MySQL数据库的驱动JAR包一同添加到项目的lib目录下,并发布到类路径中。
3、在classpath路径下配置其日志文件。在项目的src目录下创建log4j.properties文件。
4、在src目录下,创建一个com.itheima.po包,在该包下创建持久化类Customer,并在类中声明id、username、jobs和phone属性,及其对应的getter/setter方法。
5、在src目录下,创建一个com.itheima.mapper包,并在包中创建映射文件CustomerMapper.xml。
6、在src目录下,创建MyBatis的核心配置文件mybatis-config.xml。
7、在src目录下,创建一个com.itheima.test包,在该包下创建测试类MybatisTest,并在类中编写测试方法findCustomerByIdTest。
1、批量插入 *** 作
mapper.java层定义:
int batchInsert(List stockList)
mapper.xml层的sql语句:
insert into t_stock (status, asset_classify_id,asset_id,asset_item_id, name,num, batch_num, tag_id,rfid, epc, barcode,qr_code, erp, unit,pic_url, specification, model,material, color,length,width, height, weight,density, volume, price01,price02,warehouse_id, storage_zone_id,storage_location_id,storage_location_tag_id,remark, attr01, attr02,attr03, create_date,last_update,creater, client_id)values (#{item.status,jdbcType=VARCHAR},#{item.assetClassifyId,jdbcType=BIGINT},#{item.assetId,jdbcType=BIGINT}, #{item.assetItemId,jdbcType=BIGINT},#{item.name,jdbcType=VARCHAR},#{item.num,jdbcType=VARCHAR},#{item.batchNum,jdbcType=VARCHAR}, #{item.tagId,jdbcType=VARCHAR},#{item.rfid,jdbcType=VARCHAR}, #{item.epc,jdbcType=VARCHAR},#{item.barcode,jdbcType=VARCHAR},#{item.qrCode,jdbcType=VARCHAR},#{item.erp,jdbcType=VARCHAR}, #{item.unit,jdbcType=VARCHAR},#{item.picUrl,jdbcType=VARCHAR},#{item.specification,jdbcType=VARCHAR},#{item.model,jdbcType=VARCHAR},#{item.material,jdbcType=VARCHAR},#{item.color,jdbcType=VARCHAR}, #{item.length,jdbcType=DECIMAL},#{item.width,jdbcType=DECIMAL}, #{item.height,jdbcType=DECIMAL},#{item.weight,jdbcType=DECIMAL},#{item.density,jdbcType=DECIMAL},#{item.volume,jdbcType=DECIMAL}, #{item.price01,jdbcType=DECIMAL},#{item.price02,jdbcType=DECIMAL},#{item.warehouseId,jdbcType=BIGINT},#{item.storageZoneId,jdbcType=BIGINT},#{item.storageLocationId,jdbcType=BIGINT},#{item.storageLocationTagId,jdbcType=BIGINT},#{item.remark,jdbcType=VARCHAR}, #{item.attr01,jdbcType=VARCHAR},#{item.attr02,jdbcType=VARCHAR},#{item.attr03,jdbcType=VARCHAR},#{item.createDate,jdbcType=TIMESTAMP},#{item.lastUpdate,jdbcType=TIMESTAMP},#{item.creater,jdbcType=BIGINT}, #{item.clientId,jdbcType=BIGINT})
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)