1. 定义sql语句
1.1 select 标签
- id :唯一的标识符.
- parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user
- resultType :语句返回值类型或别名。注意,如果是集合,那么这里填写的是集合的泛型,而不是集合本身(resultType 与resultMap 不能并用
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object">
select * from student where id=#{id}
</select>
1.2 insert、delete、update标签
- parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User
<insert id="insert" parameterType="Object">
insert into student <trim prefix="(" suffix=")" suffixOverrides="," >
<if test="name != null "> NAME, </if>
</trim> <trim prefix="values(" suffix=")" suffixOverrides="," >
<if test="name != null "> #{name}, </if>
</trim>
</insert>
<delete id="deleteByPrimaryKey" parameterType="Object">
delete from student where id=#{id}
</delete>
<update id="updateDictDataType" parameterType="String">
update sys_dict_data set dict_type = #{newDictType} where dict_type = #{oldDictType}
</update>
2. 配置JAVA对象属性与查询结果集中列名对应关系
2.1 resultMap 标签
- 建立SQL查询结果字段与实体属性的映射关系信息
- 查询的结果集转换为java对象,方便进一步操作。
- 将结果集中的列与java对象中的属性对应起来并将值填充进去
!注意:与java对象对应的列不是数据库中表的列名,而是查询后结果集的列名
<resultMap id="BaseResultMap" type="com.online.charge.platform.student.model.Student">
<id property="id" column="id" />
<result column="NAME" property="name" />
<result column="HOBBY" property="hobby" />
<result column="MAJOR" property="major" />
<result column="BIRTHDAY" property="birthday" />
<result column=&