关于Mybatis中的一些注意点

一.关于实体类属性

当我们封装的时候我们一般要求实体类中和数据库的列名保持一致。
如果不一致将会导致查询结果为空。

解决属性名和数据库中表的字段名不一致的方法

1.在sql语句中给数据库中的字段起别名

如:

 <select id="findAll" resultType="com.domain.User">select product_name as name, product_type as type,product_price as price,regist_date as date from product ;</select>

2.给实体类和数据库建立对应关系

利用resultMap配置查询结果中的列名和实体类的属性名的对应关系,如下:

<mapper namespace="com.dao.UserDao"><!--配置查询结果中的列名和实体类的属性名的对应关系--><!--resultMap的id指定唯一标识,可以随便起,type指定结果集映射,返回的是User对象--><resultMap id="userMap" type="com.domain.User"><!--主键字段的对应如下,由于我的表没有设置主键,所以...--><!--<id property="userid" column="id"/>--><!--非主键字段的对应如下--><!--property对应的是实体类属性  column对应的是数据库的字段--><result property="name" column="product_name"/><result property="type" column="product_type"/><result property="price" column="product_price"/><result property="date" column="regist_date"/></resultMap><select id="findAll" resultMap="userMap">select * from product</select>

然后在sql语句中将resultType设置为"userMap"即可;

<select id="findAll" resultMap="userMap">select * from product
</select>

二.关于动态sql语句运用的理解

where标签

where标签类似于MySQL中的where语句。

<select id="findByProduct" parameterType="Product" resultType="Product">select * from product<where><if test="product_type!=null">and product_type like #{product_type}</if><if test="product_price!=null">and product_price>#{product_price}</if></where>
</select>

当上面的product_type和product_price都不为null的时候,那么打印出来的SQL语句为:
select * from product where product_type like “xxx” and product_price>xxx;
where标签会自动将其后第一个条件的and或者是or给忽略掉。

if标签

if标签就类似于我们平时使用的if语句,起着选择的作用,如下:

<select id="findByProduct" parameterType="Product" resultType="Product">select * from product<where><if test="product_type!=null">        <!--给出的条件-->and product_type like #{product_type}</if><if test="product_price!=null">     <!--给出的条件-->and product_price>#{product_price}</if></where>
</select>

我们可以根据product_type或product_price的符合条件来查询,只要满足了if标签里面给出的条件,即可进行查询。

只依照一个条件查询

@Test
public void testFindByProduct(){Product product = new Product();//product.setProduct_type("%用品%");   //select * from product where product_type like "%用品%";product.setProduct_price(100);         //select * from product where product_price>100;List<Product>productList = productDao.findByProduct(product);for (Product product1:productList){System.out.println(product1);}
}

当上面两个条件都启用的时候,即:

@Test
public void testFindByProduct(){Product product = new Product();product.setProduct_type("%用品%");product.setProduct_price(100);List<Product>productList = productDao.findByProduct(product);for (Product product1:productList){System.out.println(product1);}
}

表示的sql语句为:select * from product where product_type like “%用品%” and product_price>100;

foreach标签

foreach标签如其名,起到遍历集合类型的作用,如下:

<!--下面的查询语句相当于MySQL中的"select * from product where product_type in(xxx,xxx,xxx);"写法-->
<select id="findByType2" parameterType="QueryProduct" resultType="Product"><!--传入类型(parameterType)为实体类QueryProduct,返回类型(resultType)为实体类Product-->select * from product<where><if test="product_types!=null and product_types.size()>0"><!--该判断条件为product_types集合不为空,且product_types集合的长度大于0--><foreach collection="product_types" open="and product_type in(" close=")" item="product_type" separator=",">#{product_type}</foreach></if></where>
</select>

上面中的collection表示这是一个集合,由open开始,在close结束,并且将遍历出来的每一项存到item里面,由separator分割

    @Testpublic void testFindByType2(){QueryProduct queryProduct = new QueryProduct();List<String>list = new ArrayList<>();//往集合里面添加元素list.add("生活用品");list.add("乐器");list.add("文具");queryProduct.setProduct_types(list);List<Product>productList = productDao.findByType2(queryProduct);for (Product product1:productList){System.out.println(product1);}}

关于抽取重复sql语句的标签

利用sql标签来配置重复的sql语句,可以避免在项目开发的过程中重复编写大量相同的sql语句,如下:

<!--抽取重复的sql语句片段-->
<sql id="defaultUser">select * from product
</sql>

使用方式(使用include标签)

    <select id="findByProduct" parameterType="Product" resultType="Product"><include refid="defaultUser"/>   <!--使用include标签来引用前面已定义的sql语句片段,等价于select * from product--><where><if test="product_type!=null">and product_type like #{product_type}</if><if test="product_price!=null">and product_price>#{product_price}</if></where></select>

Mybatis复习笔记:4相关推荐

  1. Mybatis复习笔记:1

    关于模糊查找 模糊查找其实有两种基本操作(之前学的时候看的不太仔细,漏了-) 第一种 <select id="findByType" parameterType=" ...

  2. MyBatis复习笔记6:MyBatis缓存机制

    MyBatis缓存机制 MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制.缓存可以极大的提升查询效率. MyBatis系统中默认定义了两级缓存. 一级缓存和二级缓存. 默认情 ...

  3. MyBatis复习笔记5:MyBatis代码生成器

    前言:做过几个项目之后深感代码生成器的便捷,有了它我们可以少写许多重复的.基础的代码,如基本的增删改查的代码,我们可以交给代码生成器生成,而我们只需要专注于业务逻辑上的代码即可. MyBatis Ge ...

  4. Mybatis复习笔记3:映射文件详解

    映射文件详解 参数处理(#和$的区别) #{}:可以获取map中的值或者实体对象属性的值: ${}:可以获取map中的值或者实体对象属性的值: select * from person where i ...

  5. MyBatis复习笔记2:配置文件详解

    配置文件详解 属性(properties) MyBatis可以使用 properties 来引入外部 properties 配置文件的内容 resource:引入类路径下的资源 url:引入网络路径或 ...

  6. mybatis 复习笔记02

    1. 一对一查询: 1). 实体类: 2). 定义resultMap <!-- 订单查询关联用户的resultMap将整个查询的结果映射到cn.itcast.mybatis.po.Orders中 ...

  7. mybatis 复习笔记03

    参考:http://www.mybatis.org/mybatis-3/zh/configuration.html 入门 1. 从 XML 中构建 SqlSessionFactory 每个基于 MyB ...

  8. 【Web】javaEE课程复习笔记

    JavaEE复习笔记 根据上课的笔记整理与补充.涵盖web应用开发基础,jsp,标签,注解,struts,spring, mvc, 数据访问等内容 (因为转于个人blog,csdn图片无法显示,可至下 ...

  9. Spring复习笔记:4

    在复习笔记三中我们进行的案例的编写,我们可以发现,就算使用了注解的方式,xml配置文件文件还是不能够删除,现在我们来将一些新的注解可以让我们去掉xml配置文件. @Configuration 作用:指 ...

最新文章

  1. 收藏!深度学习计算机视觉模型解析!
  2. 静态编译qemu_使用QEMU chroot进行固件本地调试
  3. Windows8.1 64bit环境下搭建深度学习平台之CUDA安装与配置
  4. ABP框架搭建项目系列教程基础版
  5. lwip协议栈在linux运行,LwIP协议栈在uCOS II下的实现
  6. oracle笔试题目及答案,Oracle 笔试题目带答案.doc
  7. securecrt8.1破解版安装与注册机的使用方法
  8. My first essay
  9. AD10封装库转到PADS封装库里
  10. 一个可以接到“为所欲为”的成语接龙生成器
  11. 根据sam文件计算reads的GC含量
  12. 怎么清楚计算机硬盘搜索记录,如何去除电脑硬盘删除痕迹
  13. Horner规则求多项式
  14. Windows系统盘瘦身
  15. 华为vlan间路由:利用路由器实现不同vlan间的通信
  16. 安装 Ubuntu 22.04.1 LTS 桌面版(详细步骤)
  17. 变现 起航篇! 手把手交你用chatgpt快速生成视频!
  18. oracle rac 仲裁盘_11G ORACLE OCR和仲裁盘恢复
  19. iphone充电图_为什么我的iPhone无法充电?
  20. sshpass报错Host key verification failed

热门文章

  1. 在CentOS 6.8 x86_64的nginx 1.10.3上开启http2功能
  2. ATS中开启Refer防盗链功能
  3. 在Ubuntu 14.04 64bit上使用Sphinx转换MonaServer项目文档
  4. 第四章 python的turtle库的运用
  5. [BZOJ] 1606: [Usaco2008 Dec]Hay For Sale 购买干草
  6. UIView 的 autoresizingMask 属性 详解。
  7. 兼容Silverlight4的实用的Silverlight可拖放工具类源代码
  8. Markdown编辑器使用
  9. 内容协商 (Content Negotiation)
  10. C语言的sizeof和strlen