日常开发笔记20240338

本文围绕MyBatis和Vue展开。介绍了MyBatis的@Param注解、#{}和${}的使用与区别,还提及where语句可选与必选条件的写法、domain对象配置、mapper接口返回Integer的优势、bigdecimal小数点位数设置。此外,讲解了Vue前端panel组件换图标的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.mybatis的@Param注解

在写java的mybatis的mapper接口的时候,如果需要传递多个参数,则需要声明在xml文件中参数的引用名称,如果有多个参数,这个写的过程就觉得比较麻烦,在mybatis的版本升级到3.4.1 版本之后,@Param就可以省略掉了,直接变量名称就是xml文件中的引用名称,如下代码:

List<SceneTemplateDetectionEvent> selectSceneTemplateDetectionEventBetween(String tableName, String start, String end);

<select id="selectSceneTemplateDetectionEventBetween"
        resultMap="SceneTemplateDetectionEventResult">
    select id, gmtCreateTime, eventName, imgPath, status from ${tableName}
    where gmtCreateTime between #{start} and #{end}
</select>

参考博客:https://blog.csdn.net/Park33/article/details/129861007

2.mybatis的#{}和${}的使用与区别

在MyBatis 的映射配置文件中,动态传递参数有两种方式:

  • #{} 占位符
  • ${} 拼接符

#{} 和 ${} 的区别:

(1)

1)#{} 为参数占位符 ?,即sql 预编译

2)${} 为字符串替换,即 sql 拼接

(2)

1)#{}:动态解析 -> 预编译 -> 执行

2)${}:动态解析 -> 编译 -> 执行

(3)

1)#{} 的变量替换是在DBMS 中

2)${} 的变量替换是在 DBMS 外

(4)

1)变量替换后,#{} 对应的变量自动加上单引号 ‘’

2)变量替换后,${} 对应的变量不会加上单引号 ‘’

(5)

1)#{} 能防止sql 注入

2)${} 不能防止sql 注入

注意:
(1)不论是单个参数,还是多个参数,一律都建议使用注解@Param(“”)
(2)能用 #{} 的地方就用 #{},不用或少用 ${}
(3)表名作参数时,必须用 ${}。如:select * from ${tableName}
(4)order by 时,必须用 ${}。如:select * from t_user order by ${columnName}
(5)使用 ${} 时,要注意何时加或不加单引号,即 和 ′ {} 和 ' {}’

参考内容:https://www.cnblogs.com/xiao-lin-unit/p/13644004.html

3. mybatis中where语句有可选条件有必选条件,怎么写

只要写在where标签内部就可以了。比如下面的status=0.

    <select id="selectEcOrderList" parameterType="EcOrder" resultMap="EcOrderResult">
        <include refid="selectEcOrderVo"/>
        <where>
            status = 0
            <if test="feesAttributed != null  and feesAttributed != ''"> and fees_attributed like concat('%', #{feesAttributed}, '%')</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="params.beginGmtCreateTime != null and params.beginGmtCreateTime != '' and params.endGmtCreateTime != null and params.endGmtCreateTime != ''"> and gmt_create_time between #{params.beginGmtCreateTime} and #{params.endGmtCreateTime}</if>
        </where>
        order by gmt_create_time desc
    </select>

4.mybatis的domain对象

domain对象不能随便乱写,都是有配置的,一旦乱写会找到对象的位置,就会报错。比如下面的配置,就必须按照这个来写,这样在xml文件中,映射成为对象时候,才能只写对象名字。

# MyBatis配置
mybatis:
  # 搜索指定包别名
  typeAliasesPackage: com.ruoyi.project.**.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mybatis/**/*Mapper.xml
  # 加载全局的配置文件
  configLocation: classpath:mybatis/mybatis-config.xml

xml中只写对象名字:比如下面的type=EcOrder

    <resultMap type="EcOrder" id="EcOrderResult">
        <result property="id"    column="id"    />
        <result property="feesAttributed"    column="fees_attributed"    />
        <result property="name"    column="name"    />
        <result property="specs"    column="specs"    />
        <result property="fixedCost"    column="fixed_cost"    />
        <result property="num"    column="num"    />
        <result property="money"    column="money"    />
        <result property="gmtCreateTime"    column="gmt_create_time"    />
        <result property="remark"    column="remark"    />
        <result property="status"    column="status"    />
    </resultMap>

5.mapper接口返回Integer的问题

java的mapper接口,可以返回int,也可以返回Integer,推荐返回Integer,因为一旦影响的数据量是0,或者更改的数据不存在,int就不能表达null的概念了,但是Integer可以表达null的概念。

参考资料:https://blog.csdn.net/u010648883/article/details/100544426

6.bigdecimal的小数点位数

mysql的bigdecimal数据库类型,小数点后面的数量不要写太多,一般2位就足够了。如果写多了,比如写11,mybatis返回double类型的时候就会报错了。

7.vue的前端panel组件换图标

若伊的前端,使用vue来实现,在实现四联panle的时候,会有换图标的要求,图标可以从下图中取得svg文件的文件名,设置为icon-class。这样图标就换了。

<svg-icon icon-class="monitor" class-name="card-panel-icon" />

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值