数据库的应用

数据库的实战应用

1、联表查询之resultMap的使用

Orders实体类里面添加需要联表的实体类(这样就可以获得整个Room类和RoomType类)

//因为是联表查询,所以要添加变量。
//又因为是resultMap的所以是类 类型,如果是resultType的添加类里面的变量就可以了
//房间对象
private Room room;
//房型对象
private RoomType roomType;

   public Room getRoom() {
       return room;
  }

   public void setRoom(Room room) {
       this.room = room;
  }

   public RoomType getRoomType() {
       return roomType;
  }

   public void setRoomType(RoomType roomType) {
       this.roomType = roomType;
  }

 

mapper.xml

基础类是主表也就是orders表

<!-- 定义基础的ResultMap property(实体类名)column(数据库名)-->
<resultMap id="BaseResultMap" type="com.bdqn.entity.Orders">
   <id column="id" property="id" />
   <result column="ordersNo"  property="ordersno" />
   <result column="accountId" property="accountid" />
   <result column="roomTypeId"  property="roomtypeid" />
   <result column="roomId"  property="roomid" />
   <result column="reservationName" property="reservationname" />
   <result column="idCard" property="idcard" />
   <result column="phone" property="phone" />
   <result column="status"  property="status" />
   <result column="reserveDate"  property="reservedate" />
   <result column="arriveDate"  property="arrivedate" />
   <result column="leaveDate" property="leavedate" />
   <result column="reservePrice"  property="reserveprice" />
   <result column="remark"  property="remark" />
</resultMap>

需要的联表(好处就是维护方便,如果需要获取roomType的price直接加条字段就可以了)

<resultMap id="ordersRoomAndTypeResultMap" type="com.bdqn.entity.Orders" extends="BaseResultMap">
   <!-- 配置订单与房间关系 -->
   <association property="room" javaType="com.bdqn.entity.Room">
       <id column="id" property="id"/>
       <result property="roomnum" column="roomnum"/>
   </association>
   <!-- 配置订单与房型关系 -->
   <association property="roomType" javaType="com.bdqn.entity.RoomType">
       <id property="id" column="id"/>
       <result property="typename" column="typename"/>
       <result property="price" column="price"/>
   </association>
</resultMap>
<select id="findOrdersList" resultType="com.bdqn.entity.Orders">
   select o.*,r.`roomNum`,t.`typeName` from t_orders o
   inner join t_room r on r.id = o.`roomId`
   inner join t_room_type t on t.id = r.`roomTypeId`
   <where>
       <if test="reservationname!=null and reservationname!=''">
           and o.reservationname like concat('%',#{reservationname},'%')
       </if>
       <if test="startDate!=null">
           <![CDATA[ and o.reservedate >= #{startDate} ]]>
       </if>
       <if test="endDate!=null">
           <![CDATA[ and o.reservedate <= #{endDate }]]>
       </if>
   </where>
</select>

 

ordermanager.jsp(前端显示查到的数据)

{field: 'id', width: 100, title: '预订编号', align: "center"},
{field: 'roomType', width: 100, title: '预订房型', align: "center",templet:function (d) {
       return d.roomType.typename;
}},

如果是主表的直接写实体类名就能获取到了,如果是其他表的写法如上,roomtype就是在实体类里面定义的,typename就是在数据库的<!-- 配置订单与房型关系 -->里面加的需要查询的一条字段

 

2、联表查询之resultType的使用

resultype 每次添加就需要在实体类里面 定义需要的参数,然后在get、set方法

实体类

private String typeName;
private String roomNum;

public String getTypeName() {
   return typeName;
}

public void setTypeName(String typeName) {
   this.typeName = typeName;
}

public String getRoomNum() {
   return roomNum;
}

public void setRoomNum(String roomNum) {
   this.roomNum = roomNum;
}

 

mapper.xml

<select id="findOrdersList" resultType="com.bdqn.entity.Orders">
   select o.*,r.`roomNum`,t.`typeName` from t_orders o
   inner join t_room r on r.id = o.`roomId`
   inner join t_room_type t on t.id = r.`roomTypeId`
   <where>
       <if test="reservationname!=null and reservationname!=''">
           and o.reservationname like concat('%',#{reservationname},'%')
       </if>
       <if test="startDate!=null">
           <![CDATA[ and o.reservedate >= #{startDate} ]]>
       </if>
       <if test="endDate!=null">
           <![CDATA[ and o.reservedate <= #{endDate }]]>
       </if>
   </where>
</select>

 

ordermanage.jsp

{field: 'typeName', width: 170, title: '预订房型', align: "center"},
{field: 'roomNum', width: 170, title: '房间号', align: "center"},

 

3、给定时间段搜索数据

搜索框 (startDate和endDate是自己加上去的实体类中没有)

<div class="layui-inline">
   <label class="layui-form-label">开始日期</label>
   <div class="layui-input-inline">
       <input type="text" name="startDate" id="startTime" autocomplete="off" class="layui-input" placeholder="请选择开始日期(预订)" readonly>
   </div>
</div>
<div class="layui-inline">
   <label class="layui-form-label">结束日期</label>
   <div class="layui-input-inline">
       <input type="text" name="endDate" id="endTime" autocomplete="off" class="layui-input" placeholder="请选择结束日期(预订)" readonly>
   </div>
</div>

 

VO类

@DateTimeFormat(pattern = "yyyy-MM-dd")   //需要加上日期格式,不然会报错
private Date startDate;//开始日期
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endDate;//结束日期

   public Date getStartDate() {
       return startDate;
  }

   public void setStartDate(Date startDate) {
       this.startDate = startDate;
  }

   public Date getEndDate() {
       return endDate;
  }

   public void setEndDate(Date endDate) {
       this.endDate = endDate;
  }

mapper.xml

![CDATA[ ]] //这是大于等于的写法

<where>
   <if test="startDate!=null">
       <![CDATA[ and o.reservedate >= #{startDate} ]]>
   </if>
   <if test="endDate!=null">
       <![CDATA[ and o.reservedate <= #{endDate }]]>
   </if>
</where>

以上的SQL就是给定开始和结束的时间段,查询预定时间在这个时间段里面的所有数

 

4、动态SQL的写法

select的动态SQL

<select id="findOrdersList" resultMap="ordersRoomAndTypeResultMap">
   select o.*,r.`roomNum`,t.`typeName` from t_orders o
   inner join t_room r on r.id = o.`roomId`
   inner join t_room_type t on t.id = r.`roomTypeId`
   <where>
       <if test="reservationname!=null and reservationname!=''">
           and o.reservationname like concat('%',#{reservationname},'%')  //模糊查询
       </if>
       <if test="endDate!=null">
           <![CDATA[ and o.reservedate <= #{endDate }]]>
       </if>
   </where>
</select>

 

update的动态SQL

    <update id="updateRole">
       update sys_role
       <set>
           <if test="roleDesc!=null and roleDesc!=''">
               roleDesc = #{roleDesc},
           </if>
       </set>
       where id = #{id}
   </update>

 

posted @ 2021-12-22 11:50  敲代码的体育生  阅读(182)  评论(0)    收藏  举报