<?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="july.wild.dao.IUserDao"> <!--id属性为parameterType的名称随便起, type为全限定类名--> <resultMap id="userResultMap" type="july.wild.domain.Users"> <!--首先配置主键id property是属性名 column是对应的数据库列名--> <id property="Sno" column="sno"/> <result property="username" column="sname"></result> <result property="userage" column="sage"></result> <result property="userscore" column="score"></result> <result property="usersex" column="ssex"></result> </resultMap> <!--parameter的配置 id属性为parameterType的名称随便起, type为全限定类名--> <parameterMap id="userMap" type="july.wild.domain.Users"> <parameter property="Sno" resultMap="ueerResultMap"></parameter> <parameter property="username" resultMap="userResultMap"></parameter> <parameter property="userage" resultMap="userResultMap"></parameter> <parameter property="userscore" resultMap="userResultMap"></parameter> <parameter property="usersex" resultMap="userResultMap"></parameter> </parameterMap> <!--配置查询所有--> <select id="FindAll" resultType="july.wild.domain.Users"> select * from stu; </select> <insert id="insert" parameterMap="userMap"> <selectKey order="AFTER" > select last_insert_id() </selectKey> insert into stu(sno,sname,sage,score,ssex) values(#{Sno},#{username},#{userage},#{userscore},#{usersex}) </insert> <select id="findOne" resultType="users" > select * from stu where 1 = 1 <if test="_parameter != null"> and sname = #{sname} </if> <if test="_parameter != null"> and ssex = #{ssex} </if> </select> </mapper>
parameterMap 一定要和resultMap一起使用,用于指定map集合中key value之间的对应关系指定所有的属性均为resultMap中的值
指定resultMap中属性值和数据库中的列名对应关系