mybatis单表操作

  • 在Dao文件相应的映射配置文件(*.xml)中写sql语句以及传入的参数
  • 新增数据
<mapper namespace="com.itheima.dao.IUserDAO">  <!-- namespace必须是全限定类名 -->
    <insert id="saveUser" parameterType="com.itheima.domian.User">  <!-- id为DAO中的方法名,parameterType是传入参数的类型,也就是DAO方法中的参数类型 -->
        insert into user(username,address,sex,birthday) value (#{userName},#{address},#{sex},#{birthday});  <!-- #{}中是实体类中属性名称 -->
    </insert>
</mapper>
  • 删除操作
<mapper namespace="com.itheima.dao.IUserDAO"> 
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>
</mapper>
  • 更新数据
<mapper namespace="com.itheima.dao.IUserDAO"> 
    <update id="updateUser" parameterType="com.itheima.domian.User">
        update user set userName=#{userName},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id}
    </update>
</mapper>
  • 查询数据(这里主要是模糊查询的两种实现方式)
  1. 在传入参数时就将占位符写好一并传入
<mapper namespace="com.itheima.dao.IUserDAO"> 
    <select id="findByName" parameterType="java.lang.String" resultType="com.itheima.domian.User">
        select * from user where username like #{userName};
    </select>
</mapper>
  1. 在传入参数时不用处理,在xml文件中写,但是这种方式采用的是字符串拼接技术,效率较慢
<mapper namespace="com.itheima.dao.IUserDAO"> 
    <select id="findByName" parameterType="java.lang.String" resultType="com.itheima.domian.User">
        select * from user where username like '%${value}%';  <!-- 这里#{}中必须是value,不可以为其他值 -->
    </select>
</mapper>
  • 以上操作中,数据库中的列名与实体类的属性名是一致的,所以不用担心mybatis在封装时匹配不到相应的属性。当列名与属性名不匹配时,可以用以下两种方式来解决:
  1. 取别名
<mapper namespace="com.itheima.dao.IUserDAO"> 
    <select id="findAll" resultType="com.itheima.domian.User">
        select username as userName from user;   <!-- 前面是列名,后面是类的属性名 -->
    </select>
</mapper>
  1. 在映射配置文件中配置,在设置封装类时将参数resultType改为resultMap=“自定义的名字”
<mapper namespace="com.itheima.dao.IUserDAO">
    <resultMap id="userMap" type="com.itheima.domian.User">  <!-- id为自定义的名字,type是对应的实体类全名 -->
        <id property="id" column="id"></id>  <!-- id表示的是主键  -->
        <result property="username" column="userName"></result>  <!--  result表示的是非主键  -->
    </resultMap>
</mapper>
  • 动态sql语句
    动态sql语句的实现主要借助了mybatis的三个标签:if标签,where标签以及foreach标签。这里借助了QueryVo,将查询条件封装成一个类作为参数传入。该类代码如下:
public class QueryVo {

    private Integer[] ids;

    public Integer[] getIds() {
        return ids;
    }

    public void setIds(Integer[] ids) {
        this.ids = ids;
    }
}

DAO文件中添加一个相应的查询方法:

List<User> findUserByVo(QueryVo vo); //根据给定的查询对象进行查询

在DAO文件相应的映射配置文件中代码如下:

<select id="findUserByVo" parameterType="com.itheima.domian.QueryVo" resultType="com.itheima.domian.User">
        select * from user
        <where>  <!-- 在查询语句后面添加where,可与where 1=1相互替换 -->
            <if test="ids!=null and ids.length!=0">  <!-- test中需要用and而非&&,虽然其中的属性用到的是Vo类中的属性 -->
                <foreach collection="ids" open="id in (" close=")" item="id" separator=",">  <!--  collection指定集合,open指定查询条件的开头,close指定查询语句的末尾,item是指从集合中取到的元素,separator指的是用什么符号分隔item -->
                    #{id}  <!-- 中括号中的内容必须与item属性值一模一样 -->
                </foreach>
            </if>
        </where>
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值