mybatis related

Mybatis 

1. New

Mapper 

  <insert id="insert" keyProperty="id" parameterType="org.zsf.entity.UserBase">
    insert into userbase (id, userName, password,nickName, email,role)
    values (
     #{id,jdbcType=BIGINT},
     #{userName,jdbcType=VARCHAR}, 
     #{password,jdbcType=VARCHAR}, 
     #{nickName,jdbcType=VARCHAR},
     #{email,jdbcType=VARCHAR},
     #{role,jdbcType=INTEGER}
      )
  </insert>

Dao:Long insert(UserBase record);


2. Delete

Mapper

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from userbase
    where id = #{id,jdbcType=BIGINT}
  </delete>

Dao

    int deleteByPrimaryKey(Long id);

 

3. The query returns a single piece of data

Mapper

  <resultMap id="BaseResultMap" type="org.zsf.entity.UserBase">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="userName" jdbcType="VARCHAR" property="userName" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="nickName" jdbcType="VARCHAR" property="nickName" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="role" jdbcType="INTEGER" property="role" />
  </resultMap>


 <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from userbase
    where id = #{id,jdbcType=BIGINT}
  </select>

Dao:

 UserBase selectByPrimaryKey(Long id);

返回列表

Mapper

  <sql id="Base_Column_List">
    id, userName, password,nickName,role,email
  </sql>


  <select id="getUsersByType" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from userbase
    where role = #{role,jdbcType=INTEGER}
  </select>

Dao:    List<UserBase> getUsersByType(int type);


4、更新

Mapper

  <update id="updateByPrimaryKey" parameterType="org.zsf.entity.UserBase">
    update userbase
    set userName = #{userName,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      nickName = #{nickName,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      role = #{role,jdbcType=INTEGER},
    where id = #{id,jdbcType=BIGINT}
  </update>

Dao:

    int updateByPrimaryKey(UserBase record);


MyBatis 多参数

方法1

Mapper:

  <select id="getUserStudentListByType"  resultType="org.zsf.entity.UserStudent">
  select ub.id, ub.userName as userName, ub.role,ub.email,us.gender,us.age,us.address,ub.nickName
  from userbase ub left join user_student us on ub.id = us.userId
  where ub.role=#{type} and ub.type2 = #{type2}
  </select>

Dao:

    List<UserStudent> getUserStudentListByType(@Param("type")int type,@Param("type2")int type2);


方法2

     Mapper

  <select id="getUserStudentListByType"  resultType="org.zsf.entity.UserStudent">
  select ub.id, ub.userName as userName, ub.role,ub.email,us.gender,us.age,us.address,ub.nickName
  from userbase ub left join user_student us on ub.id = us.userId
  where ub.role=#{0} and ub.type2=#{1}
  </select>

Dao

     List<UserStudent> getUserStudentListByType(int type,int type2);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326265227&siteId=291194637