update
返回值为匹配条数
不论最终是否对数据进行了修改,只要某条记录符合匹配条件,返回值就加1
insert
如果成功,返回值为插入数据库的条数,失败返回的是exception,需要处理异常
delete
返回值为删除条数
select
四种基本操作中,以查询操作的返回值最多
在 Mapper 文件中,通过
resultType
或 resultMap
指定返回值返回一般数据类型
String getDeptName(Long id);
<select id="getDeptName" parameterType="java.lang.Long" resultType="string"> select dept_name from department where id = #{id,jdbcType=BIGINT} </select>
返回javaBean类型
Department getDeptById(Long id);
<select id="getDeptById" parameterType="java.lang.Long" resultType="com.wg.demo.po.Department"> select * from department where id = #{id,jdbcType=BIGINT} </select>
当实体类字段与数据库字段不一致时,可以使用
resultMap
<select id="getDeptById" parameterType="java.lang.Long" resultMap="BaseResultMap"> select * from department where id = #{id,jdbcType=BIGINT} </select>
<resultMap id="BaseResultMap" type="com.wg.demo.po.Department"> <id column="id" jdbcType="BIGINT" property="id" /> <result column="dept_name" jdbcType="VARCHAR" property="deptName" /> <result column="descr" jdbcType="VARCHAR" property="descr" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> </resultMap>
返回List类型
List<Department> selectAll();
<select id="selectAll" resultMap="BaseResultMap"> select * from department </select>
返回Map类型
该类型返回值可分为两种方式:
- 返回单条数据,map 对应表中字段
Map getDeptAsMap(Long id);
<select id="getDeptAsMap" parameterType="java.lang.Long" resultType="map"> select * from department where id = #{id,jdbcType=BIGINT} </select>
- 返回多条数据
以 Map 形式返回多条数据,map 中每一条记录对应一条查询记录,(该方式与List返回值类似,不过是数据的组织方式不同而已),这里需要指定使用哪个属性作为Map主键
@MapKey("id") //指定Map的主键为返回结果的id Map getDeptAsMaps();
<select id="getDeptAsMaps" resultType="map"> select * from department </select>
返回JSONObject
以 JSONObject 的形式返回查询结果,多用于查询结果列数不定的情况(查询结果是动态的)
@PostMapping("/test") public ResultMsg findJSONObject( ) { return ResultMsg.getMsg(employeeMapper.findJSONResult()); }
JSONArray findJSONResult();
<select id="findJSONResult" resultType="com.alibaba.fastjson.JSONObject" > SELECT * from employee </select>