Mybatis XML文件详解

Mybatis XML文件详解

<?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标签开始,由/mapper结束,可以把它想成一个空间,是映射文件
属性namespace:空间名,主要在代理中使用。这个namespace是唯一的。
这里把mapper标签和接口联系在一起了,namespace=写接口路径,映射文件要和接口在同一目录下
 -->
<mapper namespace="com.dao.UserinfoDAO">
    <!-- =============映射关系标签=============
    属性type:写po类的包名类名,由于之前定义了po类的别名,这里就写这个别名
    属性id:是这个映射标签的唯一标识
    id标签是查询结果集中的唯一标识
    属性column:查询出来的列名
    属性property:是po类里所指定的列名
    通常会在原列名后面加下划线,这是固定的,这里就是id后面_
     -->
    <resultMap type="com.po.UserinfoPO" id="userinfoMap">
        <result column="userid" property="userid"/>
        <result column="loginname" property="loginname"/>
        <result column="loginpass" property="loginpass"/>
        <result column="username" property="username"/>
        <result column="upower" property="upower"/>
        <result column="birthday" property="birthday"/>
        <result column="sex" property="sex"/>
    </resultMap>
    <!-- ==================定义sql片段==============
    sql:是sql片段标签属性id是该片段的唯一标识 -->
    <sql id="zd">
        userid,loginname,loginpass,username,upower,birthday,sex
    </sql>
    <!-- 增删改查标签里的id:一定要和接口里对应的方法名一致,
         resultMap输出类型里写映射标签里的id 
         parameterType:输入类型,规范输入数据类型,指明查询时使用的参数类型-->
    <!-- 验证登录 -->
    <select id="login" resultMap="userinfoMap" parameterType="com.po.UserinfoPO">    
        <!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
        select <include refid="zd"/> from userinfo
        <where>            
                loginname=#{loginname} and loginpass=#{loginpass}
        </where>
    </select>
    
    <!-- 查询用户列表 -->
    <select id="userList" resultMap="userinfoMap" parameterType="com.po.UserinfoPO">
        <!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
        select <include refid="zd"/> from userinfo
    </select>
    
    <!-- 查询修改用户信息的id -->
    <select id="updateid" resultMap="userinfoMap" parameterType="com.po.UserinfoPO">
        <!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
        select <include refid="zd"/> from userinfo
        <where>userid=#{userid}</where>
    </select>
    
    <!-- 修改用户信息 -->
     <update id="update" parameterType="com.po.UserinfoPO">
         update userinfo 
         set loginname=#{loginname},loginpass=#{loginpass},username=#{username},
             upower=#{upower},birthday=#{birthday},sex=#{sex}
         where userid=#{userid}     
     </update>
     
    <!-- 添加用户信息 -->
    <insert id="insert" parameterType="com.po.UserinfoPO">
        insert into userinfo(<include refid="zd"/>) 
        values
        (#{userid},#{loginname},#{loginpass},#{username},#{upower},#{birthday},#{sex})
    </insert>
        
    <!-- 增删改查标签里的id:一定要和接口里对应的方法名一致 -->
    <delete id="delete" parameterType="int">
        delete from userinfo where userid=#{userid}
    </delete>
    
    <!-- 根据用户名模糊查询,根据权限查询 -->
    <select id="select" resultMap="userinfoMap" parameterType="java.util.Map">
        <!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
        select <include refid="zd"/> from userinfo
        <!-- 当页面没有输入用户名和选择权限,就让它的条件永远为真,就变成全查询了 -->
        <where>
            <if test="username == null and username = '' and upower == -1">
                and 1=1
            </if>
            <if test="username != null and username !=''">
                and username LIKE '%${username}%' 
            </if>        
            <if test="upower != -1">
                and upower=#{upower} 
            </if>            
        </where>
    </select>
</mapper>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mybatis是一种Java持久化框架,它可以将Java对象映射到数据库中的表中。Mybatis的配置文件主要包括两个部分:mybatis-config.xml和mapper.xml。其中mapper.xmlMybatis的核心配置文件,它定义了SQL语句、映射关系、参数映射等信息,下面详细介绍mapper.xml文件的各个部分。 1、mapper.xml的命名空间 每个mapper.xml文件都应该设置一个命名空间,命名空间用来定义映射的SQL语句和参数映射。命名空间的格式为:mapper namespace="com.xxx.mapper.XxxMapper",其中com.xxx.mapper是mapper接口所在的包名,XxxMapper是mapper接口的类名。 2、映射SQL语句 在mapper.xml中,可以定义各种SQL语句,例如查询、插入、更新和删除等。SQL语句的格式为<select|insert|update|delete>,其中<select>表示查询语句,<insert>表示插入语句,<update>表示更新语句,<delete>表示删除语句。具体的SQL语句可以根据需要自行定义,例如: <select id="selectUserById" parameterType="int" resultType="com.xxx.model.User"> select * from user where id=#{id} </select> 在上面的例子中,id表示SQL语句的唯一标识符,parameterType表示传入参数的类型,resultType表示返回结果的类型。 3、参数映射 在mapper.xml中,可以定义参数映射,将Java对象映射到SQL语句中的参数,例如: <select id="selectUserByName" parameterType="java.lang.String" resultType="com.xxx.model.User"> select * from user where name=#{name} </select> 在上面的例子中,parameterType表示传入参数的类型,#{name}表示将Java对象中的name属性映射到SQL语句中的参数。 4、结果集映射 在mapper.xml中,可以定义结果集映射,将SQL语句返回的结果封装成Java对象,例如: <select id="selectAllUser" resultType="com.xxx.model.User"> select * from user </select> 在上面的例子中,resultType表示返回结果的类型,Mybatis会将SQL语句返回的结果封装成com.xxx.model.User对象。 5、动态SQL语句 在mapper.xml中,可以使用动态SQL语句来构建复杂的SQL语句。动态SQL语句可以根据不同的条件来生成不同的SQL语句,例如: <select id="selectUser" parameterType="com.xxx.model.User" resultType="com.xxx.model.User"> select * from user where 1=1 <if test="id != null"> and id=#{id} </if> <if test="name != null and name != ''"> and name=#{name} </if> </select> 在上面的例子中,<if>标签用来判断条件,根据条件生成不同的SQL语句。 6、其它元素 在mapper.xml中,还可以使用其它元素来完成更多的功能,例如: (1)<resultMap>元素:定义结果集映射关系; (2)<include>元素:引入其它的SQL语句; (3)<where>、<set>等元素:用于构建复杂的SQL语句; (4)<foreach>元素:用于遍历集合或数组等。 总之,mapper.xml文件Mybatis框架中非常重要的配置文件,它定义了SQL语句、映射关系、参数映射等信息,是Mybatis实现持久化操作的核心。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT枫斗者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值