1.controller
public class DemandController {
@Autowired
private DemandService demandService;
@PostMapping("/add")
public R add(@RequestBody Demand demand, HttpServletRequest request){
// Principal userPrincipal = request.getUserPrincipal();
// String userName = userPrincipal.getName();
Demand result = demandService.add(demand, "admin");
if(null != result){
return R.ok(result, "提交成功!");
}else{
return R.fail("提交失败!");
}
}
@GetMapping("/{id}}")
public R selectById(@PathVariable Long id){
return R.ok(demandService.selectById(id), "查询成功!");
}
@PostMapping
public R selectDemands(@RequestBody Demand demand){
return R.ok(demandService.selectDemands(demand), "查询成功!");
}
}
2. service
@Service
public class DemandServiceImpl implements DemandService {
@Autowired
private DemandMapper demandMapper;
@Autowired
private SysUserService userService;
@Override
@Transactional
public Demand add(Demand demand, String userName) {
LoginUser loginUser = userService.selectUserByUserName(userName);
demand.setUserId(loginUser.getSysUser().getUserId());
demand.setCreateBy(userName);
int rows = demandMapper.add(demand);
return rows > 0 ? demand : null;
}
@Override
public Demand selectById(Long id) {
return demandMapper.selectById(id);
}
@Override
public List<Demand> selectDemands(Demand demand) {
return demandMapper.selectDemands(demand);
}
@Override
@Transactional
public int delById(Long id) {
return 0;
}
@Override
@Transactional
public int delByIds(List<Long> ids) {
return 0;
}
@Override
@Transactional
public Demand updateDemand(Demand demand) {
return null;
}
}
3. mapper
public interface DemandMapper {
int add(Demand demand);
Demand selectById(Long id);
List<Demand> selectDemands(Demand demand);
// int delById(Long id);
//
// int delByIds(List<Long> ids);
//
// Demand updateDemand(Demand demand);
}
4. 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.guanwei.dao.DemandMapper">
<resultMap id="DemandResult" type="com.guanwei.model.Demand">
<id property="id" column="id" />
<result property="userId" column="user_id" />
<result property="headName" column="head_name" />
<result property="headGender" column="head_gender" />
<result property="headPhone" column="head_phone" />
<result property="summarize" column="summarize" />
<result property="filePath" column="file_path" />
<result property="type" column="type" />
<result property="solve" column="solve" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
</resultMap>
<sql id="selectDemand">
select d.id, d.user_id, d.head_name, d.head_phone, d.summarize, d.file_path, d.type, d.solve,
d.create_by, d.create_time, d.update_by, d.update_time, d.remark
from demand d
</sql>
<insert id="add" parameterType="com.guanwei.model.Demand" useGeneratedKeys="true" keyProperty="id">
insert into demand (
<if test="id !=null and id !=0">id,</if>
<if test="userId !=null and userId !=0">user_id,</if>
<if test="headName !=null and headName !=''">head_name,</if>
<if test="headPhone !=null and headPhone !=''">head_phone,</if>
<if test="summarize !=null and summarize !=''">summarize,</if>
<if test="filePath !=null and filePath !=''">file_path,</if>
<if test="type !=null and type !=''">type,</if>
<if test="solve !=null and solve !=''">solve,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="remark != null and remark != ''">remark,</if>
create_time
) values (
<if test="id !=null and id !=0">#{id},</if>
<if test="userId !=null and userId !=0">#{userId},</if>
<if test="headName !=null and headName !=''">#{headName},</if>
<if test="headPhone !=null and headPhone !=''">#{headPhone},</if>
<if test="summarize !=null and summarize !=''">#{summarize},</if>
<if test="filePath !=null and filePath !=''">#{filePath},</if>
<if test="type !=null and type !=''">#{type},</if>
<if test="solve !=null and solve !=''">#{solve},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="remark != null and remark != ''">#{remark},</if>
sysdate()
)
</insert>
<select id="selectById" parameterType="Long" resultMap="DemandResult">
<include refid="selectDemand"/>
where id=#{id}
</select>
<select id="selectDemands" parameterType="com.guanwei.model.Demand" resultMap="DemandResult">
<include refid="selectDemand"/>
where 1=1
<if test="headName != null and headName != ''">
AND d.head_name like concat('%', #{headName}, '%')
</if>
<if test="headPhone != null and headPhone != ''">
AND d.head_phone like concat('%', #{headPhone}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
AND date_format(u.create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
AND date_format(u.create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
<if test="type != null and type != ''">
AND type = #{type}
</if>
<if test="solve != null and solve != ''">
AND solve = #{solve}
</if>
<if test="createBy != null and createBy != ''">
AND createBy = #{createBy}
</if>
<!-- 数据范围过滤 -->
${params.dataScope}
</select>
</mapper>
4.分析
4.1 返回主键?
int 没办法直接是主键,主键只能赋值给对象的id;
想要获取,getId()吧!!!