SSM框架實戰系列之十一_MyBatis框架之DAO代碼

  SSM框架實戰系列之十一_MyBatis框架之DAO代碼


  前面我們已經在項目中配置好了MyBatis框架,現在讓我們用MyBatis框架來寫點代碼。


  通常情況下,一個DAO模塊包含兩個文件:DAO接口和包含SQL語句的xml文件。


  一、DAO接口

  假如我們有一個用戶表,表結構如下:



  那麼DAO接口的代碼可以這樣寫:

@Repository
public interface UserDao {
	void add(User user);

	void del(int id);

	void update(User user);

	User getById(int id);

	List<User> list();
}

  MyBatis框架不要求編寫DAO實現類。


  二、包含SQL語句的xml文件

  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 namespace="com.hanhf.ssm.dao.UserDao">
	<resultMap id="BaseResultMap" type="com.hanhf.ssm.bean.User">
		<id column="user_id" jdbcType="INTEGER" property="id" />
		<result column="user_name" jdbcType="VARCHAR" property="name" />
		<result column="password" jdbcType="VARCHAR" property="password" />
		<result column="picture" jdbcType="VARCHAR" property="picture" />
		<result column="real_name" jdbcType="VARCHAR" property="realName" />
		<result column="id_number" jdbcType="VARCHAR" property="idNumber" />
		<result column="remark" jdbcType="VARCHAR" property="remark" />
		<result column="add_date" jdbcType="TIMESTAMP" property="addDate" />
		<result column="is_use" jdbcType="BIT" property="use" />
	</resultMap>
	<sql id="dataFields">
		user_name, password, picture, real_name, id_number, remark, add_date, is_use
	</sql>
	<insert id="add" parameterType="com.hanhf.ssm.bean.User">
		insert into tbl_user(<include refid="dataFields" />)
		values (#{name}, #{password}, #{picture}, #{realName}, #{idNumber}, #{remark}, #{addDate}, #{use})
	</insert>
	<update id="del" parameterType="java.lang.Integer">
		update tbl_user
		set is_del = 1
		where user_id = #{id}
	</update>
	<update id="update" parameterType="com.hanhf.ssm.bean.User">
		update tbl_user
		set real_name = #{realName},
		id_number = #{idNumber},
		remark = #{remark}
		where user_id = #{id}
	</update>
	<select id="getById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
		select user_id,
		<include refid="dataFields" />
		from tbl_user
		where is_del = 0 and user_id = #{id}
	</select>
	<select id="list" resultMap="BaseResultMap">
		select user_id,
		<include refid="dataFields" />
		from tbl_user
		where is_del = 0
	</select>
</mapper>

  1、<mapper namespace="com.hanhf.ssm.dao.UserDao">

  此xml文件的根節點是mapper元素,即映射,命名空間必須與上面的DAO接口名稱一致,這樣才能配合。


  2、<resultMap id="BaseResultMap" type="com.hanhf.ssm.bean.User">

  resultMap,即結果映射,是MyBatis框架中的一個重要概念,它可以將查詢結果自動封裝爲一個Java Bean。

  在此xml文件中,每一個元素的id值都必須唯一。type值指定將查詢結果封裝爲哪個類的Java Bean。

  接下來,就是結果的映射,例如:

  <id column="user_id" jdbcType="INTEGER" property="id" />
  <result column="user_name" jdbcType="VARCHAR" property="name" />

  id和result都是將數據表中的字段映射爲實體類中的屬性,其中column指定數據表中的字段名,property指定實體類中的屬性名。id是表中的主鍵列,需要特殊聲明,普通列都用result聲明。


  3、<sql id="dataFields">

  MyBatis中的sql節點可以定義一段SQL腳本,以供後續使用,通常用於定義重複使用的SQL腳本,提升代碼重用性。

  後面可以用<include refid="dataFields" />來引用這段SQL。


  4、insert

  <insert id="add" parameterType="com.hanhf.ssm.bean.User">
    insert into tbl_user(<include refid="dataFields" />)
    values (#{name}, #{password}, #{picture}, #{realName}, #{idNumber}, #{remark}, #{addDate}, #{use})
  </insert>


  常用的數據庫操作有增刪改查4種,因此就有相應的4種標籤節點。

  insert節點用於指定插入操作,id值必須與DAO接口中的方法名稱一致,以便配合。

  方法參數使用parameterType指定。

  使用#{屬性名}的方式,取得實體類中某個屬性的值用於生成SQL。


  增刪改語句的標籤結構都類似。


  5、select

  查詢分爲按id查找對象和查找所有對象。

  MyBatis會根據DAO接口返回值的聲明,封裝查詢結果爲一個對象或一個集合。

  使用resultMap聲明返回的結果應如何進行映射轉換,這裏的值應與第2點的resultMap的id值配合。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章