• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
cnsdhzzl
博客园    首页    新随笔    联系   管理    订阅  订阅
mybatis高级(2)_数据库中的列和实体类不匹配时的两种解决方法_模糊查询_智能标签

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cnsdhzzl.dao.StudentDao">

    <!-- 当数据库中的列和实体类不匹配时,方法一使用resultMapper -->
    <resultMap type="student" id="studentMapper">
        <!-- 数据库中的列,实体类的属性 -->
        <result column="dataName" property="entityName" />
    </resultMap>
    <!-- 当数据库中的列和实体类不匹配时,方法二语句中使用as,数据库中的列 as 实体类的属性 -->

    <!-- 返回集合要加resultType -->
    <select id="findStudent" resultType="student">
        select * from student
    </select>

    <!-- sql语句区分数据库,以下使用oracle数据库拼接语句使用|| -->

    <!-- 传入map集合时参数需要使用map的key -->
    <select id="likeStudent" resultType="student">
        <!-- 传入map -->
        select * from student where name like '%'||#{mname}||'%' and address
        like '%'||#{maddress}||'%'
        <!-- 传入零散参数 -->
        <!-- select * from student where name like '%'||#{0}||'%' and address like 
            '%'||#{1}||'%' -->
    </select>

    <!-- 智能标签 -->
    <select id="smartTag" resultType="student">
        select * from student
        <if test="name !=null">
            and name like '%'||#{name}||'%'
        </if>
        <if test="address !=null">
            and address like '%'||#{address}||'%'
        </if>
    </select>
</mapper>

 

posted on 2016-12-19 10:26  cnsdhzzl  阅读(754)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3