1、输入映射(往数据库输入)parameterMap(不常用)与 parameterType(常用)
2、输出映射(从数据库输出)resultMap与resultType
parameterType
获取用户详细信息时输入为Long id,在xml文件里parameterType对应的值为Long,
新增用户时输入为User user,在xml文件里parameterType对应的值为User,
修改用户时输入为User user,在xml文件里parameterType对应的值为User,
删除用户时输入的为Long[] ids,Long[]是一个对象数组,在xml文件里parameterType对应的值为String
基本可以得出parameterType与传入数据库的参数基本一致。
parameterMap负责将Java Bean的属性映射成statement的参数
resultMap与resultType
1、resultmap:mybatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap。
2、resulttype:resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在
在代码中可以看出两个查询语句中存在resultMap,印证了第二条中resultMap则是对外部ResultMap的引用这句话,数据表中的列和需要返回的对象的属性可能不完全一致,外部的ResultMap进行结果集映射,正好解决这一问题。
PS:下列代码将导入的依赖删掉了
Controller
package com.zx.controller;
/**
* Controller
* @date 2022-11-16
*/
@RestController
@RequestMapping("/com/zx")
public class UserController extends BaseController
{
@Autowired
private UserService userService;
/**
* 查询用户列表
*/
@PreAuthorize("@ss.hasPermi('com:zx:list')")
@GetMapping("/list")
public AjaxResult list(User user)
{
List<User> list = userService.selectUserList(user);
return AjaxResult.success(list);
}
/**
* 导出用户列表
*/
@PreAuthorize("@ss.hasPermi('com:zx:export')")
@Log(title = "用户", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, User user)
{
List<User> list = userService.selectUserList(user);
ExcelUtil<User> util = new ExcelUtil<User>(User.class);
util.exportExcel(response, list, "用户数据");
}
/**
* 获取用户详细信息
*/
@PreAuthorize("@ss.hasPermi('com:zx:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return AjaxResult.success(userService.selectUserById(id));
}
/**
* 新增用户
*/
@PreAuthorize("@ss.hasPermi('com:zx:add')")
@Log(title = "用户", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody User user)
{
return toAjax(userService.insertUser(user));
}
/**
* 修改用户
*/
@PreAuthorize("@ss.hasPermi('com:zx:edit')")
@Log(title = "用户", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody User user)
{
return toAjax(userService.updateUser(user));
}
/**
* 删除用户
*/
@PreAuthorize("@ss.hasPermi('com:zx:remove')")
@Log(title = "用户", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids)
{
return toAjax(userService.deleteUserByIds(ids));
}
}
Service
package com.zx.service;
/**
* 用户Service接口
* @date 2022-11-16
*/
public interface UserService
{
/**
* 查询用户
*/
public User selectUserById(Long id);
/**
* 查询用户列表
*/
public List<User> selectUserList(User user);
/**
* 新增用户
*/
public int insertUser(User user);
/**
* 修改用户
*/
public int updateUser(User user);
/**
* 批量删除用户
*/
public int deleteUserByIds(Long[] ids);
/**
* 删除用户信息
*/
public int deleteUserById(Long id);
}
ServiceImpl
package com.zx.service.impl;
/**
* 用户Service业务层处理
* @date 2022-11-16
*/
@Service
public class UserServiceImpl implements UserService
{
@Autowired
private UserMapper userMapper;
/**
* 查询用户
*/
@Override
public User selectUserById(Long id)
{
return userMapper.selectUserById(id);
}
/**
* 查询用户列表
*/
@Override
public List<User> selectUserList(User user)
{
return userMapper.selectUserList(user);
}
/**
* 新增用户
*/
@Override
public int insertUser(User user)
{
user.setId(IdUtils.nextId());
user.setCreateBy(SecurityUtils.getUsername());
user.setCreateTime(DateUtils.getNowDate());
return userMapper.insertUser(user);
}
/**
* 修改用户
*/
@Override
public int updateUser(User user)
{
user.setUpdateBy(SecurityUtils.getUsername());
user.setUpdateTime(DateUtils.getNowDate());
return userMapper.updateUser(user);
}
/**
* 批量删除用户
*/
@Override
public int deleteUserByIds(Long[] ids)
{
return userMapper.deleteUserByIds(ids);
}
/**
* 删除用户信息
*/
@Override
public int deleteUserById(Long id)
{
return userMapper.deleteUserById(id);
}
}
Mapper
package com.zx.mapper;
/**
* 用户Mapper接口
* @date 2022-11-16
*/
public interface UserMapper
{
/**
* 查询用户
*/
public User selectUserById(Long id);
/**
* 查询用户列表
*/
public List<User> selectUserList(User user);
/**
* 新增用户
*/
public int insertUser(User user);
/**
* 修改用户
*/
public int updateUser(User user);
/**
* 删除用户
*/
public int deleteUserById(Long id);
/**
* 批量删除用户
*/
public int deleteUserByIds(Long[] ids);
}
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.zx.mapper.UserMapper">
<resultMap type="User" id="UserResult">
<result property="id" column="id" />
<result property="userName" column="username" />
<result property="passWord" column="password" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
</resultMap>
<sql id="selectUserVo">
select id, username, password, create_by, create_time, update_by, update_time from user
</sql>
<select id="selectUserList" parameterType="User" resultMap="UserResult">
<include refid="selectUserVo"/>
<where>
<if test="userName != null "> and username = #{userName}</if>
<if test="passWord != null "> and password = #{passWord}</if>
</where>
</select>
<select id="selectUserById" parameterType="Long" resultMap="UserResult">
<include refid="selectUserVo"/>
where id = #{id}
</select>
<insert id="insertUser" parameterType="User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="userName != null">username,</if>
<if test="passWord != null">password,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="userName != null">#{username},</if>
<if test="passWord != null">#{password},</if>
</trim>
</insert>
<update id="updateUser" parameterType="User">
update user
<trim prefix="SET" suffixOverrides=",">
<if test="userName != null">username = #{userName},</if>
<if test="passWord != null">password = #{passWord},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteUserById" parameterType="Long">
delete from user where id = #{id}
</delete>
<delete id="deleteUserByIds" parameterType="String">
delete from user where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>