mybatis----小细节

1、起别名

在MyBatis中,<typeAliases>元素用于定义类型别名,它可以将Java类名映射为一个更简短的别名,这样在映射文件中可以直接使用别名而不需要完整的类名。

下面是一个示例:

在mybatis核心配置文件中配置typeAliases标签

复制代码
<typeAliases>
  <typeAlias alias="Acount" type="duhong.entity.Account"/>
</typeAliases>

为duhong.entity.Account类指定了别名Account。

定义了这些别名后,我们可以在映射文件中直接使用这些别名来引用对应的Java类,例如:

复制代码
<resultMap id="AccountMap" type="Account">

通过使用别名,我们可以简化映射文件中的配置,并提高代码可读性和可维护性。

2、mybatis核心配置文件加载映射文件的其他方式

复制代码
<package name="duhong.dao"/>

这种声明方式,会使mybatis查找duhong/dao下对应的xml文件,不过这种方式要求xml资源目录与dao层目录一致,而且xml名称要与mapper接口名称一致。

3、#{}与${}的区别

在MyBatis中,#{} 和 ${} 用于处理SQL语句中的参数,但是它们在作用和安全性方面有所不同。

  1. #{} 语法:
    在MyBatis中,当你使用 #{} 来表示一个参数占位符时,MyBatis会在运行时将其替换为一个参数的值,并且会使用预处理语句(PreparedStatement)来处理参数。也就是说,不管参数值是什么,它都会被视为一个字符串值并且正确地转义。

    SELECT * FROM some_table WHERE id = #{id}

  2. {} 语法: 与 #{} 不同,当你使用 {} 时,MyBatis会直接将这个位置替换为变量的字面值。也就是说,如果你的变量中包含了特定的SQL关键词或结构,它将不做任何转义直接嵌入SQL语句中。

    SELECT * FROM ${tableName} WHERE column = #{value}

测试:

dao层添加接口

复制代码
//查询所有,按降序排列
List<Account> selectAllByDesc(String key);

mapper中添加查询语句

复制代码
resultMap id="AccountMap" type="duhong.entity.Account">
<id property="id" column="id"></id>
<result property="accountNumber" column="account_number"></result>
<result property="accountType" column="account_type"></result>
<result property="balance" column="balance"></result>
</resultMap>
<select id="selectAllByDesc" resultMap="AccountMap">
     select * from account ORDER BY balance ${order};
</select>

添加junit,测试

复制代码
SqlSession sqlSession= SqlSessionUtil.openSession();
 @Test
public void test(){
     AccountDao mapper = sqlSession.getMapper(AccountDao.class);
     List<Account> account = mapper.selectAllByDesc("DESC");
     for (Account account1 : account) {
         System.out.println(account);
     }
 }

将${}换成#{},执行出错,原因在于DESC是sql语句的关键字,而#{}会将参数转化为字符串。

4、模糊查询

复制代码
<select id="selectLikeAll" resultMap="AccountMap">
    select * from account where account_number like '%${key}%';
</select>

//模糊查询
List<Account> selectLikeAll(String key);

@Test
public void likeTest(){
    AccountDao accountDao=sqlSession.getMapper(AccountDao.class);
    List<Account> accounts = accountDao.selectLikeAll("1");
    System.out.println(accounts);
}

同样直接使用#{}也会出错,#{}在' '中并不会被赋值

改进方式sql中使用concat拼接字符串

复制代码
<select id="selectLikeAll" resultMap="AccountMap">
    select * from account where account_number like concat('%',#{key},'%');
</select>
相关推荐
codingPower6 小时前
升级mybatis-plus导致项目启动报错: net.sf.jsqlparser.statement.select.SelectBody
java·spring boot·maven·mybatis
123461611 天前
互联网大厂Java面试:从Spring Boot到微服务的探索
java·数据库·spring boot·微服务·面试·mybatis·orm
2301_801252221 天前
Mybatis的添加和修改功能
java·开发语言·mybatis
W.Buffer3 天前
MyBatis 源码深度解析:从 Spring Boot 实战到底层原理
spring boot·后端·mybatis
CS Beginner3 天前
【搭建】个人博客网站的搭建
java·前端·学习·servlet·log4j·mybatis
m0_564264184 天前
IDEA DEBUG调试时如何获取 MyBatis-Plus 动态拼接的 SQL?
java·数据库·spring boot·sql·mybatis·debug·mybatis-plus
没有bug.的程序员4 天前
Spring Boot 整合第三方组件:Redis、MyBatis、Kafka 实战
java·spring boot·redis·后端·spring·bean·mybatis
迷了璐的航4 天前
mybatis解决查询中使用group by时出现sql_mode=only_full_group_by
数据库·sql·mybatis
悟能不能悟4 天前
mybatis的sql中,如果一个条件column=#{column},column的值为null时,会不会匹配出column is null 的记录
sql·tomcat·mybatis
技术砖家--Felix4 天前
Spring Boot数据访问篇:整合MyBatis操作数据库
数据库·spring boot·mybatis