SSM框架实战系列之十一_MyBatis框架之DAO代码

来源:互联网 发布:it网络技术 编辑:程序博客网 时间:2024/06/08 09:51

  SSM框架实战系列之十一_MyBatis框架之DAO代码


  前面我们已经在项目中配置好了MyBatis框架,现在让我们用MyBatis框架来写点代码。


  通常情况下,一个DAO模块包含两个文件:DAO接口和包含SQL语句的xml文件。


  一、DAO接口

  假如我们有一个用户表,表结构如下:



  那么DAO接口的代码可以这样写:

@Repositorypublic 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_userset is_del = 1where user_id = #{id}</update><update id="update" parameterType="com.hanhf.ssm.bean.User">update tbl_userset 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_userwhere is_del = 0 and user_id = #{id}</select><select id="list" resultMap="BaseResultMap">select user_id,<include refid="dataFields" />from tbl_userwhere 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值配合。