MyBatis 映射文件分析:客户管理、房间管理、用户信息、文件管理

本篇博客分析了 MyBatis 映射文件,展示了如何通过 XML 配置实现对客户、房间、用户信息以及文件管理的数据库操作。

数据库表设计

以下展示了映射文件所对应数据库表的结构:

  • 客户信息表 (client)

    • 编号 (id): 主键,自增
    • 房间编号 (roomId): 外键,关联房间信息表
    • 姓名 (name)
    • 性别 (sex)
    • 手机号 (phone)
    • 年龄 (age)
    • 地址 (addr)
  • 房间信息表 (room)

    • 房间编号 (roomId): 主键,自增
    • 价格 (price)
    • 类型 (type)
    • 面积 (area)
    • 地址 (address)
    • 总数 (total)
    • 房间描述 (roomDescribe)
    • 类别编号 (categoryId): 外键,关联房间类别表
  • 房间类别表 (category)

    • 类别编号 (categoryId): 主键,自增
    • 类别名称 (categoryName)
  • 用户表 (user)

    • 编号 (userId): 主键,自增
    • 用户名 (userName)
    • 密码 (userPassword)
  • 文件表 (myfile)

    • 文件编号 (fileId): 主键,自增
    • 文件标题 (fileTitle)
    • 文件路径 (fileUrl)

映射文件分析

1. 客户管理

  • 查询所有客户:

    <select id="selectAllClient"  resultMap="baseResultMap">
    		select *   from  client a left join room b on a.roomId=b.roomId
    </select>
    

    该语句使用左连接查询客户表和房间表,并使用 baseResultMap 映射结果集到 cn.smbms.pojo.Client 对象。

  • 根据 ID 查询客户:

    <select id="selectById" resultType="cn.smbms.pojo.Client">
    		select * from  client where id=#{id}
    </select>
    

    该语句根据 ID 查询客户信息,结果类型为 cn.smbms.pojo.Client 对象。

  • 更新客户信息:

    <update id="updClientById" parameterType="cn.smbms.pojo.Client">
    		update client set roomId=#{roomId},name=#{name},sex=#{sex},age=#{age},phone=#{phone},addr=#{addr} where id=#{id}
    </update>
    

    该语句更新客户信息,参数类型为 cn.smbms.pojo.Client 对象。

  • 新增客户信息:

    <insert id="insertClient" parameterType="cn.smbms.pojo.Client">
    		INSERT INTO client(roomId,name,sex,age,phone,addr)
    		VALUES (#{roomId},#{name},#{sex},#{age},#{phone},#{addr})
    </insert>
    

    该语句新增客户信息,参数类型为 cn.smbms.pojo.Client 对象。

2. 房间管理

  • 查询所有房间:

    <select id="selectAllRoom" resultMap="baseResultMap">
    		select *   from  room a left join category b on a.categoryId=b.categoryId
    </select>
    

    该语句使用左连接查询房间表和房间类别表,并使用 baseResultMap 映射结果集到 cn.smbms.pojo.Room 对象。

  • 根据 ID 删除房间:

    <delete id="deleteRoom" parameterType="Integer">
    		 delete from  room where roomId = #{roomId}
    </delete>
    

    该语句根据 ID 删除房间信息,参数类型为 Integer

  • 更新房间信息:

    <update id="updRoomById" parameterType="cn.smbms.pojo.Room">
    		update room set price=#{price},type=#{type},area=#{area},total=#{total},address=#{address},roomDescribe=#{roomDescribe} where roomId=#{roomId}
    </update>
    

    该语句更新房间信息,参数类型为 cn.smbms.pojo.Room 对象。

  • 根据 ID 查询房间:

    <select id="selectById" resultType="cn.smbms.pojo.Room">
    		select * from  room where roomId=#{roomId}
    </select>
    

    该语句根据 ID 查询房间信息,结果类型为 cn.smbms.pojo.Room 对象。

  • 新增房间信息:

    <insert id="insertRoom" parameterType="cn.smbms.pojo.Room">
    		INSERT INTO room(roomId,type,area,price,total,address,roomDescribe)
    		VALUES (#{roomId},#{type},#{area},#{price},#{total},#{address},#{roomDescribe})
    </insert>
    

    该语句新增房间信息,参数类型为 cn.smbms.pojo.Room 对象。

3. 用户信息管理

  • 用户登录:

    <select id="findUser" parameterType="String" resultType="cn.smbms.pojo.User">
    		select *
    		from user
    		where userName=#{userName}
    		and userPassword =#{userPassword}
    </select>
    

    该语句根据用户名和密码查询用户信息,结果类型为 cn.smbms.pojo.User 对象。

  • 判断管理员是否存在:

    <select id="findUsersByUsername" resultType="cn.smbms.pojo.User">
    		select * from user
    		where userName=#{userName}
    </select>
    

    该语句根据用户名查询用户信息,结果类型为 cn.smbms.pojo.User 对象。

  • 修改密码:

    <update id="updateUser" parameterType="cn.smbms.pojo.User" >
    		update user set userPassword=#{userPassword} where userName=#{userName}
    </update>
    

    该语句更新用户密码,参数类型为 cn.smbms.pojo.User 对象。

4. 文件管理

  • 新增文件:
    <insert id="insertFile" parameterType="cn.smbms.pojo.MyFile">
    		INSERT INTO myfile(fileTitle,fileUrl)
    		VALUES (#{fileTitle},#{fileUrl})
    </select>
    
    该语句新增文件信息,参数类型为 cn.smbms.pojo.MyFile 对象。

总结

本篇博客展示了 MyBatis 映射文件是如何通过 XML 配置实现对数据库的各种操作,包括查询、插入、更新和删除。通过使用映射文件,可以简化数据库操作,提高开发效率。

MyBatis 映射文件分析:客户管理、房间管理、用户信息、文件管理

原文地址: https://www.cveoy.top/t/topic/lBpP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录