mybatis中SQL多条件查询映射结果集的两种方式:resultMap和resultUser

版权声明:本站所提供的文章资讯、软件资源、素材源码等内容均为本作者提供、网友推荐、互联网整理而来(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考,如有侵犯您的版权,请联系我,本作者将在三个工作日内改正。 https://blog.csdn.net/weixin_42323802/article/details/82531135

多条件查询,使用封装一个类,封装多个条件查询的SQL语句,然后传递pojo对象进行多条件查询。

①多条件的映射,结果集映射用到resultMap 结果集映射,映射多个sql查询条件。

②使用别名代替 , 映射到resultType中

使用pojo和resultMap区别:

pojo和SQL中查询的字段名不一致时候,使用resultMap(或者把mysql中Filedname和Java实体类的Filedname不一致,使用别名指定)把关系映射 并把映射结果映射到pojo中。

使用resultMap映射结果集;

<?xml version="1.0" encoding="UTF-8" ?>

<!--多条件查询,使用userMap可以使用  统配符进行查询
或者使用 Alias 别名进行多条件查询,这样映射的结果集就无需使用ResultMap
  并且不进行通配符进行查询,使用单个条件( 定义别名 )作为条件进行查询

  -->

<selects>
<!-- sql  查询语句,map-->
<select  id="findUserByUsernameAndPassword" parameterType="java.lang.String" resultMap="UserMap" >
    select * from user where username=#{username} password=#{password}
</select>
    <!--resultMap  配置如下-->
<resultMap  type="user" id="userMap">
<id property="userId" column="user_id" />
    <result  property="username" colum="username" />
    <result  property="age" colum="age" />
    <result  property="address" colum="address" />
    <result  property="password" colum="password" />
</resultMap>




<select id="findUserByUsernameAndPassword"  parameterType="java.lang.String" resultUser="User">
</select>
</selects>

使用pojo;

<?xml version="1.0" encoding="UTF-8" ?>

<!--多条件查询,使用userMap可以使用  统配符进行查询
或者使用 Alias 别名进行多条件查询,这样映射的结果集就无需使用ResultMap
  并且不进行通配符进行查询,使用单个条件( 定义别名 )作为条件进行查询

  -->

<selects>
<!-- sql  多条件查询语句解决  mysql的字段名称 和 java实体类中的名称不一致,使用别名Alias-->
<select  id="findUserByUsernameAndPassword" parameterType="java.lang.String" resultType="user" >
    select user_id userId,username,age,password from user where username=#{username} password=#{password}
</select>
</selects>

MYSQL社区版(社区绿色免费版)mysql-8.0.12-winx64.zip性能比5.7版本快2倍,提供下下载地址:

猜你喜欢

转载自blog.csdn.net/weixin_42323802/article/details/82531135