zoukankan      html  css  js  c++  java
  • SSM框架中写sql在dao文件中以注解的方式

    1以注解方式

    //两个参数其中一个是对象需写,对象.属性

    @Update("update delivery_address set consignee = #{address.consignee},country = #{address.country},city = #{address.city},address = #{address.address},phone_number = #{address.phoneNumber},telnumber = #{address.telNumber},zipcode = #{address.zipcode},update_time = UNIX_TIMESTAMP(NOW())*1000 where id = #{id}") void update(@Param("address") Address address, @Param("id") Long id);

    OrderProvider.class

    另外一种写法

      @Select("<script>" +
                "SELECT COUNT(*) FROM excel_template   " +
                " where create_uid = ${sqlMap.create_uid} and is_delete = 0 " +
                "<if test="sqlMap.end_time != null and sqlMap.end_time != '' "> and UNIX_TIMESTAMP(create_date) ${sqlMap.end_time} </if>" +
                "<if test="sqlMap.start_time != null and sqlMap.start_time != ''"> and UNIX_TIMESTAMP(create_date) >= ${sqlMap.start_time} </if>" +
                "<if test="sqlMap.keyword != null and sqlMap.keyword != ''"> and (file_name like CONCAT('%',#{sqlMap.keyword},'%') )</if>" +
                "</script>")
        int countList(PageBean<ExcelTemplate> pageBean);

     在上一篇中提到查询中in()语句用foreach查询

    现在用另外一种方式自定义注解方式

    public List<TaskChildFinished> findListByIds(String ids) {
            List<Long> idList = StringUtils.stringToLongList(ids);
            List<TaskChildFinished> taskChildFinisheds = endProductDao.findListByIds(idList);
            return this.format(taskChildFinisheds);
    }
     public static List<Long> stringToLongList(String string){
            return Arrays.asList(string.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
     }
       @Select("select * from task_child_finished where id in (#{idList})")
        @Lang(SimpleSelectInLangDriver.class)
        List<TaskChildFinished> findListByIds(@Param("idList")List<Long> ids);
    public class SimpleSelectInLangDriver extends XMLLanguageDriver implements LanguageDriver {
    
        private static final Pattern inPattern = Pattern.compile("\(#\{(\w+)\}\)");
    
        @Override
        public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
    
            Matcher matcher = inPattern.matcher(script);
            if (matcher.find()) {
                script = matcher.replaceAll("<foreach collection="$1" item="_item" open="(" "
                        + "separator="," close=")" >#{_item}</foreach>");
            }
    
            script = "<script>" + script + "</script>";
            return super.createSqlSource(configuration, script, parameterType);
        }
    }
  • 相关阅读:
    剑指21.栈的压入、弹出序列
    剑指20.包含min函数的栈
    剑指19.顺时针打印矩阵
    Java--使用反编译工具,打开jar包,查看源码
    没想到 Unicode 字符还能这样玩?
    angularjs中响应回车事件
    两个很好的angular调试工具-——batarang(stable)和ng-inspector
    基于 ThinkPHP5 的 cltphp 被搜索劫持,篡改首页的解决过程记录
    国内外CDN服务商CNAME特征串调研
    开源中国/码云 README.md上传图片的爬坑记录
  • 原文地址:https://www.cnblogs.com/G-JF/p/9305262.html
Copyright © 2011-2022 走看看