在myslq连接中的url中属性介绍:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://114.55.147.226:3306/elifedev3?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&allowMultiQueries=true&useServerPrepStmts=true useUnicode=true useUnicode 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true rewriteBatchedStatements=true MySql 的批量操作,要加rewriteBatchedStatements参数 Mysql JDBC驱动,各个版本测试结果: MySql JDBC 驱动版本号 插入10万条数据用时 5.0.8 加了rewriteBatchedStatements参数,没有提高还是17.4秒 5.1.7 加了rewriteBatchedStatements参数,没有提高还是17.4秒 5.1.13 加了rewriteBatchedStatements参数,插入速度提高到1.6秒 allowMultiQueries=true 允许批量操作; useServerPrepStmts=true useServerPrepStmts:如果服务器支持,是否使用服务器端预处理语句? 默认值为“真” 只有使用了useServerPrepStmts=true才能开启Mysql的预编译。 要想知道你的应用是否真正的使用了预编译,请执行:show global status like '%prepare%';看看曾经编译过几条,当前Prepared_stmt_count 是多少。 autoReconnect 当数据库连接异常中断时,是否自动重新连接? autoReconnectForPools 是否使用针对数据库连接池的重连策略 failOverReadOnly 自动重连成功后,连接是否设置为只读? maxReconnects autoReconnect设置为true时,重试连接的次数 initialTimeout autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒 connectTimeout 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本 socketTimeout socket操作(读写)超时,单位:毫秒。 0表示永不超时oracle下支持执行多条语句,下面3个相同
1、<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";" > update T_EMP_1 <set> age = #{item.age}+1,name=#{item.name} </set> where id = #{item.id} </foreach> </update> 2、<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close="end;" separator="" > update T_EMP_1 <set> age = #{item.age}+1,name=#{item.name} </set> where id = #{item.id}; </foreach> </update> 3、<update id="batchUpdate" parameterType="java.util.List"> begin <foreach collection="list" item="item" index="index" separator="" > update T_EMP_1 <set> age = #{item.age}+1,name=#{item.name} </set> where id = #{item.id}; </foreach> end; </update>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了,当然单参数也可
eg:自己把参数封装成Map的类型 <select id="dynamicForeach3Test" parameterType="java.util.HashMap" resultType="Blog"> 2 select * from t_blog where title like "%"#{title}"%" and id in 3 <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> 4 #{item} 5 </foreach> 6 </select> 上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码: public List dynamicForeach3Test(Map params); 对应测试代码: @Test public void dynamicForeach3Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); final List ids = new ArrayList(); ids.add(1); ids.add(2); ids.add(3); ids.add(6); ids.add(7); ids.add(9); Map params = new HashMap(); params.put("ids", ids); params.put("title", "中国"); List blogs = blogMapper.dynamicForeach3Test(params); for (Blog blog : blogs) System.out.println(blog); session.close(); }批量删除
<delete id="batchDeleteStudent" parameterType="List"> DELETE FROM STUDENT WHERE id IN <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete>批量更新 注意:oracle中 形如 update *** set *** where ** in(....) 这种语句 in所在的集合有条数限制 为1000条
<update id="batchUpdateStudent" parameterType="List"> UPDATE STUDENT SET name = "5566" WHERE id IN <foreach collection="list" item="item" index="index" open="(" separator="," close=")" > #{item} </foreach> </update> <update id="batchUpdateStudentWithMap" parameterType="Map" > UPDATE STUDENT SET name = #{name} WHERE id IN <foreach collection="idList" index="index" item="item" open="(" separator="," close=")"> #{item}</foreach></update>批量插入 mysql和oracle不一样
mysql: <insert id="batchInsertStudent" parameterType="java.util.List"> INSERT INTO STUDENT (id,name,sex,tel,address) VALUES <foreach collection="list" item="item" index="index" separator="," > (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address}) </foreach> </insert> oracle: <insert id="insertBatch4Oracle" parameterType="List"> insert into aa(a,b) <foreach collection="list" item="item" index="index" separator="union all" > select #{item.a},#{item.b} from dual </foreach> </insert>
转载于:https://www.cnblogs.com/wwwcf1982603555/p/9110000.html
