当前位置: 首页 > wzjs >正文

深圳网站制作费用网络营销推广方案案例

深圳网站制作费用,网络营销推广方案案例,如何区分网站开发语言,中国站长之家域名查询最近碰到几道Mybatis3的面试题&#xff0c;记录下。 1.Mybatis3 怎样执行批量插入&#xff1f; 使用Mybatis3中的<foreach>标签动态拼接需要插入的记录行数据。 基本语法复习&#xff0c;MySQL批量插入SQL如下&#xff1a; INSERT INTO user(user_name, age, created_…

最近碰到几道Mybatis3的面试题,记录下。

1.Mybatis3 怎样执行批量插入?

使用Mybatis3中的<foreach>标签动态拼接需要插入的记录行数据。

基本语法复习,MySQL批量插入SQL如下:

INSERT INTO `user`(user_name, age, created_by, updated_by)
VALUES 
('chen', 2, 'derek', 'derek'),
('chen', 2, 'derek', 'derek')

 插入多态语句时,在values后用括号把要插入的数据包围,同时用逗号分隔每条记录。

还是使用user表来实践下。

CREATE TABLE `user` (`USER_ID` int NOT NULL AUTO_INCREMENT,`USER_NAME` varchar(100) NOT NULL COMMENT '用户姓名',`AGE` int NOT NULL COMMENT '年龄',`CREATED_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,`CREATED_BY` varchar(100) NOT NULL DEFAULT 'UNKNOWN',`UPDATED_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,`UPDATED_BY` varchar(100) NOT NULL DEFAULT 'UNKNOWN',PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=345 DEFAULT CHARSET=utf8mb3

UserMapper.java接口定义如下:

public class UserMapper {int batchInsertUsers(@Param("users") List<User> users);
}

Mybatis3 批量插入时,需要注意插入的最后一个逗号的清除,所以使用<trim>标签来处理比较好。

User Mapper.xml定义如下:

<insert id="batchInsertUsers" parameterType="list">INSERT INTO `user`(user_name, age, created_by, updated_by)<trim prefix="VALUES" suffixOverrides=","><foreach collection="users" index="index" item="user" separator=",">(#{user.userName, jdbcType=VARCHAR},#{user.age, jdbcType=INTEGER},#{user.createdBy, jdbcType=VARCHAR},#{user.updatedBy, jdbcType=VARCHAR})</foreach></trim>
</insert>

测试代码如下:

package com.derek.mapper;import com.derek.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class UserMapperTest {private static SqlSessionFactory sqlSessionFactory;private static SqlSession sqlSession;private static UserMapper userMapper;@BeforeAllpublic static void setUp() throws IOException {Reader reader = Resources.getResourceAsReader("mybatis-config.xml");sqlSessionFactory = new org.apache.ibatis.session.SqlSessionFactoryBuilder().build(reader);sqlSession = sqlSessionFactory.openSession();userMapper = sqlSession.getMapper(UserMapper.class);}@AfterAllpublic static void tearDown() {userMapper = null;sqlSession.close();}@Testpublic void testInsertBatch() {List<User> users = new ArrayList<>();for(int i=1; i<=10; i++) {User user = new User();user.setUserName("batch name-" + i);user.setAge(i);user.setCreatedBy("robot");user.setCreatedTime(new Date());user.setUpdatedBy("robot");user.setUpdatedTime(new Date());users.add(user);}userMapper.batchInsertUsers(users);sqlSession.commit();}}

2.Mybatis3 怎样执行批量操作(插入/更新/删除)?

使用 ExecutorType.BATCH 模式

SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);

然后进行批量插入/更新/删除操作,再进行提交。

具体的示例如下:

1)插入100条user记录。

2)修改100条user记录。

3)删除100条user记录。

把上述300条操作做成一个batch执行。

@Testpublic void testBatchOperation() {try (SqlSession batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {UserMapper userMapper = batchSession.getMapper(UserMapper.class);// insert 100 usersfor(int i=1; i<=100; i++) {User user = new User();user.setUserName("batch name-" + i);user.setAge(i);user.setCreatedBy("robot");user.setCreatedTime(new Date());user.setUpdatedBy("robot");user.setUpdatedTime(new Date());userMapper.insert(user);}// update 100 usersUserExample userExample = new UserExample();userExample.createCriteria().andUserNameLike("%batch name%");User updatedUser = new User();updatedUser.setUserName("updated name");userMapper.updateByExampleSelective(updatedUser, userExample);// delete 100 usersUserExample deleteExample = new UserExample();deleteExample.createCriteria().andUserNameLike("updated name");userMapper.deleteByExample(deleteExample);// commit the batchbatchSession.commit();
}


文章转载自:

http://u8AQCAlJ.dknLf.cn
http://goZNUWyv.dknLf.cn
http://WTcxIBeQ.dknLf.cn
http://vdTHFBEX.dknLf.cn
http://sJVqLAGf.dknLf.cn
http://0jB0udcV.dknLf.cn
http://gRdsnEvu.dknLf.cn
http://Vok3w9qP.dknLf.cn
http://VHbWILLw.dknLf.cn
http://UEh3d31g.dknLf.cn
http://02lIoYaK.dknLf.cn
http://4RxUqDmb.dknLf.cn
http://5UFn36f6.dknLf.cn
http://eytV0Bdx.dknLf.cn
http://o6jMoHS7.dknLf.cn
http://44ZVyxS3.dknLf.cn
http://WfA1xxMW.dknLf.cn
http://f9yrGoG1.dknLf.cn
http://hj6H7xFH.dknLf.cn
http://HGOF7lAs.dknLf.cn
http://2jcwNCTs.dknLf.cn
http://HoOmUxC5.dknLf.cn
http://Y3Kn0wdF.dknLf.cn
http://d2AxkBmE.dknLf.cn
http://CFp42BdV.dknLf.cn
http://UXVq61SX.dknLf.cn
http://XPNhR9dc.dknLf.cn
http://Nmn5Jc9L.dknLf.cn
http://XhaXivTU.dknLf.cn
http://nVG510I5.dknLf.cn
http://www.dtcms.com/wzjs/756430.html

相关文章:

  • 商业网站的域名后缀是什么石家庄网站建设seo优化营销
  • 文本文档做网站制作ppt的基本做法
  • 北京企业建网站定制价格wordpress 优化设置
  • 网站开发需要学什么网页制作教程
  • 商丘网站网站建设wordpress 产生大量首页
  • 分析对手网站河南做网站哪个平台好
  • 大兴专业网站建设公司免费电视剧大全网站
  • 网站推广渠道的类型怎么在国税网站上做实名认证吗
  • 建设工程规划许可证网站多语言网站多域名推广
  • 旅游网站在提高用户体验方面应做哪些工作公众号开发是什么
  • 购物网站是多少天津推广平台
  • 做智能家居网站怎么注册公司商标
  • ps外包网站手机html5网站开发
  • 做宣传图片的网站做各企业网站大概多少钱
  • 企业网站一般要素微信开发小程序教程
  • 如何架设内部网站网站更换空间教程
  • 广东网站推广公司做网站泊头
  • 交通局网站建设整改北京做网站好
  • 广州网站备案方案怎么做网站投放广告的代理商
  • 东莞营销网站app打包平台
  • h5开网站开发教程自适应企业网站源码
  • 做网站移动端建多大尺寸门户网站的基本特征有
  • 网站开发为什么要用框架制作网站公司选 择乐云seo
  • 做网站开发的wordpress实现投稿功能
  • 手机网站怎样做的环球贸易网
  • 台州市建设招标投标网站厦门网站建设有哪些公司
  • 在线响应式网站品牌营销策划
  • 网站建设违法行为阿里云做网站送服务器吗
  • 保定专业网站建设公司wordpress ip库
  • 网站 备案 名称大连的网页设计公司