MyBatis配置Sql映射文件(xxxMapper.xml)

1、多条件查询

   VO(View Object),  BO(业务层对象),POJO(普通类), PO(与数据库的表对应的实体类) 对实体类进行分类

数据表:

desc   t_user
名称   空值         类型
-------- -------- -------------
ID   NOT   NULL   NUMBER(11)
USERNAME      VARCHAR2(20)
PASSWORD      VARCHAR2(50)
SEX         VARCHAR2(2)
BRITHDAY         DATE
ADDRESS         VARCHAR2(200)

user实体类:表字段与实体类属性名不一样时

private Integer UserId;
    private String name;
    private String password;
    private String sex;
    private Date brithday;
    private String address;
    
    public Integer getUserId() {
        return UserId;
    }
    public void setUserId(Integer userId) {
        UserId = userId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
。。。。
View Code

创建一个条件类:

public class UserVO {
    private User user;
    //添加其他属性...
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

Usermapper.xml:

 <!-- 查询根据姓名模糊查询、性别 -->
    <select id="findByLikeNameAndSex" parameterType="UserVO" resultMap="UserMap">
        select * from t_user where username like #{user.name} and sex = #{user.sex}
    </select>
<!-- 使用resurtMap,映射属性 -->
<resultMap type="User" id="UserMap">
    <id column="id" property="UserId"/>
    <result column="username" property="name"/>
</resultMap>

  user.name:使用的是ognl(对象导航语言)

Usermapper.java:接口

//查询根据姓名模糊查询、性别
List<User> findByLikeNameAndSex(UserVO userVO) throws Exception;

测试代码:

//查询根据姓名模糊查询、性别
UserVO userVO = new UserVO();
User user = new User();
user.setName("%五%");
user.setSex("男");
userVO.setUser(user);
List<User>  list= userMapper.findByLikeNameAndSex(userVO);
System.out.println(list);

2、使用resultMap来进行属性与列的映射

1、第一种解决方案,使用 resultMap 映射别名

2、第二种:给表定义别名

  <resultMap type="User" id="userMap">
         <!-- 属性名与列名的映射 -->
         <!-- id标签, 映射的主键列 
               column:列名  不区分大小写
               property: 属性名, 区分大小写
         -->
         <id column="id" property="userId" />
         <!-- 非主键列  result-->
         <result column="username"  property="name"/>
   </resultMap>

在statement中使用, resultMap 属性作为输出参数映射,  resultType与resultMap只能二选一

3、动态sql:

  •  <If test=”属性的条件”>   (and、or)
  •  <where>  作为where关键字,  忽略第一个and或者or
  •  <set> 作为set关键字使用,  忽略最后一个逗号, 只在update语句中使用
  •  <foreach> foreach循环, 批量删除,批量添加,批量修改, 批量查询

if:

<!-- 高级查询 -->
    <select id="queryUser" parameterType="UserVO" resultMap="UserMap">
        select * from t_user 
        <where>
            <if test="user.UserId != null">
                and id = #{user.UserId}
            </if>
            <if test="user.name != null and user.name != ''">
                and username like #{user.name}
            </if>
            <if test="user.password != null and user.password != ''">
                and password = #{user.password}
            </if>
            <if test="user.sex != null and user.sex != ''">
                and sex = #{user.sex}
            </if>
            <if test="user.address != null and user.address != ''">
                and address = #{user.address}
            </if>
            <if test="user.brithday != null ">
                and brithday = #{user.brithday}
            </if>
        </where>
    </select>

set:

<!-- 按条件修改 -->
    <update id="queryUpdate" parameterType="user">
        update t_user
        <set>
            <if test="name != null and name != '' ">
                 username = #{name},
            </if>
            <if test="password != null and password != '' ">
                 password = #{password},
            </if>
            <if test="sex != null and sex != '' ">
                 sex = #{sex},
            </if>
            <if test="brithday != null ">
                 brithday = #{brithday},
            </if>
            <if test="address != null and address != '' ">
                 address = #{address},
            </if>
        </set>
        <where>
            id = #{UserId}
        </where>
    </update>

foreach:

  • for(变量: 集合对象/数组对象)
  • collection: array(对应方法参数是数组)、list(对应方法参数是List集合)
  • item: 变量
  • open: 开始符
  • close: 结束符
  • separator: 分割符
<!-- 使用foreach进行查询 、 查询id编号为27,28,29的用户信息-->
    <select id="selectFor" resultMap="UserMap">
        select * from t_user
        <where>
            <!-- 使用or
            <foreach collection="array" item="i" open="(" close=")" separator="or">
                id = #{i}
            </foreach> -->
            
            <!-- 使用in -->
            <foreach collection="array" item="i" open="id in(" close=")" separator=",">
                #{i}
            </foreach>
        </where>
    </select>

sql片段: 存放的sql片段,  重复的sql代码提出出来, 达到sql重用

    <!-- 查询所有用户 -->
    <select id="findAll" resultMap="UserMap">
        select <include refid="userColumn"/> from t_user
    </select>
    <!-- sql片段 -->
    <sql id="userColumn">id,username,password,sex,brithday,address</sql>

4、如果参数是简单数据类型, statement的parameterType可以省略,但是如果参数是自定义类,不能省略

Mybatis中的修改属性,属性为空,执行sql语句,默认报错:处理

原因:属性为null值, 而这个null 在数据库是关键字, 数据库的任意数据类型都可以接收null, mybatis无法通过null判断数据库这一列的数据类型, 报错

第一种解决方案:

  在#{属性名, jdbcType=数据库数据类型}

<!-- 添加 -->
<insert id="insertUser" parameterType="User"  >
  insert into t_user values(sep_user.nextval,#{name,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},
  #{sex,jdbcType=VARCHAR},#{brithday,jdbcType=DATE},#{address,jdbcType=VARCHAR})
</insert>

第二种方案:

  在mybatis全局配置文件中设置对null的处理

<!-- jdbcType对应属性的值为null,处理方式 
           默认值: OTHER, 如果属性为null, 报错
               NULL: 如果属性为null, 给数据库对应的列赋值为null
        -->
<settings>
    <setting name="jdbcTypeForNull" value="NULL"/>
</settings>
原文地址:https://www.cnblogs.com/64Byte/p/13033682.html