上善若水の技术簿

时光荏苒、不浮青春.


  • 首页

  • 标签57

  • 分类58

  • 归档283

  • 日程表

  • 站点地图

  • 搜索

mybatis实例

发表于 2017-03-24 | 更新于 2019-09-03 | 分类于 java , mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- 批量插入 遇到重复key 改为更新 需求设定-->
<insert id="addBatchDepartment" parameterType="java.util.List" >
INSERT INTO department (
branchId,
branchName,
common,
spellId)
VALUES
<foreach collection="list" item="item" index="index" separator="," >
(
#{item.branchId},
#{item.branchName},
#{item.common},
#{item.spellId}
)
</foreach>
ON DUPLICATE KEY UPDATE
branchId = values(branchId)
</insert>

{———-}
增

1
2
3
4
5
6
7
8
9
10
<insert id="addStudent" parameterType="java.util.Map">
insert into student
(id,name,age)
values
(
#{item.id},
#{item.name},
#{item.age}
)
</insert>

批量

1
2
3
4
5
6
7
8
9
10
11
12
13
<insert id="addStudent" parameterType="java.util.Map">
insert into student
(id,name,age,start_time)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.id},
#{item.name},
#{item.age},
now()
)
</foreach>
</insert>

删

1
这里写代码片

批量


改

1
2
3
4
5
6
7
8
9
10
11
12
<update id="updateStudent" parameterType="java.util.Map">
update student
<set>
<if test="userid != null and userid != '' ">
userid=#{userid},
</if>
</set>
where 1=1
<if test="id!= null and id!= '' ">
and id=#{id}
</if>
</update>

批量

1
2
3
4
5
6
7
8
<update id="updateStudent" parameterType="java.util.Map">
update student set state=2 where 1=1
and id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>

</update>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<update id="editStudent"  parameterType="java.util.List">  
<foreach collection="list" item="item" index="index" separator=";">
update ware_service
<set>
<if test="item.endTime!=null and item.endTime!=''">
end_time=#{item.endTime},
</if>
<if test="item.redeemcode!=null and item.redeemcode!=''">
redeemcode=#{item.redeemcode}
</if>
</set>
where id=#{item.id}
</foreach>
</update>

批量修改需设置db.properties
新增 &allowMultiQueries=true


查

1
2
3
4
5
6
7
8
9
10
11
12
 <select id="getAllStudent" resultType=com.mofangge.entity.student" parameterType="java.util.Map">
select
id,
name,
age,
start_time as startTime
from student where 1=1
<if test="userid != null and userid != '' ">
and userid=#{userid}
</if>
and end_time > now()
</select>

批量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<select id="getAllStudents" resultType="com.mofangge.entity.student" parameterType="java.util.Map">
select
id,
name,
age,
start_time as startTime
from student where 1=1 and age in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
and end_time > now()
<if test="userid!=null and userid!=''">
and userid=#{userid}
</if>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="getAllStudent" resultType="com.mofangge.entity.Student" parameterType="java.util.Map">
select
id,
name,
age,
from student where 1=1 and code in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
and end_time > now()
<if test="id!=null and id!=''">
and id=#{id}
</if>
</select>

mybatis大于小于写法

1
2
3
4
5
6
7
8
9
10
11
12
原符号       <        <=      >       >=       &        '        "
替换符号 &lt; &lt;= &gt; &gt;= &amp; &apos; &quot;
例如:sql如下:
create_date_time &gt;= #{startTime} and create_date_time &lt;= #{endTime}

第二种写法(2):
大于等于
<![CDATA[ >= ]]>
小于等于
<![CDATA[ <= ]]>
例如:sql如下:
create_date_time <![CDATA[ >= ]]> #{startTime} and create_date_time <![CDATA[ <= ]]> #{endTime}

spring filter 注入service

发表于 2017-03-06 | 更新于 2018-10-18 | 分类于 java , exception

需求背景:

spring filter 注入service 查询数据库

通过直接注入方式 service 一直是 null

解决办法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

public class AppFilter implements Filter {
    private AkskService akskService;//这个就是需要注入的service
 
    public void destroy() {
    }
    public void doFilter(ServletRequest servletReq, ServletResponse servletRes, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletReq;
        HttpServletResponse response = (HttpServletResponse) servletRes;
 
    }
 
    public void init(FilterConfig config) throws ServletException {
        ServletContext context = config.getServletContext();//这里获取applicationContext
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        akskService = (AkskService) ctx.getBean(AkskService.class);
    }
}
相关文章
  • 数据库链接已关闭
  • java7 异常分类总结
  • timestamp 转 date 少一天
  • spring 容器启动执行
  • spring springmvc 父子容器关系

freemarker 示例

发表于 2016-11-11 | 更新于 2019-09-03 | 分类于 essay
  • 判断字符串长度
  • 时间格式化
  • 数字格式化
  • 显示boolean值
  • 非空判断
  • list遍历

{———-}

判断字符串长度

1
2
3
4
5
6
<#if list.startTime?length lt 6>
<p>预定时间 <span><br/>${list.startTime!}-${list.endTime!}</span></p>
</#if>
<#if list.startTime?length gt 6>
<p>预定时间 <span><br/>${list.startTime!}<br/>${list.endTime!}</span></p>
</#if>

时间格式化

1
2
自己指定格式是这样
${dateVar?string("yyyy-MM-dd HH:mm:ss zzzz")}

数字格式化

1
2
3
4
5
6
<#assign tempNum=20>  
${tempNum}
${tempNum?c} 去掉整型中的","
${tempNum?string.number}或${tempNum?string(“number”)} 结果为20
${tempNum?string.currency}或${tempNum?string(“currency”)} 结果为¥20.00
${tempNum?string. percent}或${tempNum?string(“percent”)} 结果为2,000%

显示boolean值

1
2
<#assign foo=true/>  
${foo?string("yes", "no")}

非空判断

1
<h2>${list.title!}</h2>

list遍历

1
2
3
4
5
<#list listProduct as list>
<#list list.list01 as list01>
<td>${list01.name!}<td>
</#list>
</#list>
相关文章
  • 腾讯COS 阿里OSS 七牛 备份脚本
  • 深夜构思自动驾驶狙镜算法
  • OKHTTP 要点
  • MYSQL NULL和 空 区别
  • idea 激活
1…3536
上善若水

上善若水

283 日志
58 分类
57 标签
RSS
安卓app下载
Previous Releases
0%
京ICP备18026588号-1 © 2017 – 2021 上善若水