文章目录
- 1.Cotroller
- 2.Service接口
- 3.serviceImpl
- 4.dao接口
- 5.dao.xml
1.Cotroller
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
@Reference
private CheckGroupService checkGroupService;
@RequestMapping("/add")
public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds) {
try {
checkGroupService.add(checkGroup,checkitemIds);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
}
return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
}
@RequestMapping("/findpage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
return checkGroupService.pageQuery(queryPageBean);
}
@RequestMapping("/findById")
public Result findById(Integer id){
try {
CheckGroup checkGroup= checkGroupService.findById(id);
return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,checkGroup);
}catch (Exception e){
e.printStackTrace();
return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
}
}
@RequestMapping("/findCheckItemIdsByCheckGroupId")
public Result findCheckItemIdsByCheckGroupId(Integer id){
try {
List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupId(id);
return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
}catch (Exception e){
e.printStackTrace();
return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
}
}
@RequestMapping("/edit")
public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds) {
try {
checkGroupService.edit(checkGroup,checkitemIds);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
}
return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
}
@RequestMapping("/findAll")
public Result findAll() {
try {
List<CheckGroup> list= checkGroupService.findAll();
return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
}
}
}
2.Service接口
public interface CheckGroupService {
public void add(CheckGroup checkGroup,Integer[] checkitemIds);
public PageResult pageQuery(QueryPageBean queryPageBean);
public CheckGroup findById(Integer id);
public List<Integer> findCheckItemIdsByCheckGroupId(Integer id);
public void edit(CheckGroup checkGroup,Integer[] checkitemIds);
public List<CheckGroup> findAll();
}
3.serviceImpl
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
@Autowired
private CheckGroupDao checkGroupDao;
@Override
public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
checkGroupDao.add(checkGroup);
Integer checkGroupId = checkGroup.getId();
this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
}
@Override
public PageResult pageQuery(QueryPageBean queryPageBean) {
Integer currentPage = queryPageBean.getCurrentPage();
Integer pageSize = queryPageBean.getPageSize();
String queryString = queryPageBean.getQueryString();
PageHelper.startPage(currentPage,pageSize);
Page<CheckGroup> page=checkGroupDao.findByCondition(queryString);
return new PageResult(page.getTotal(),page.getResult());
}
@Override
public CheckGroup findById(Integer id) {
return checkGroupDao.findById(id);
}
@Override
public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
}
@Override
public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
checkGroupDao.edit(checkGroup);
checkGroupDao.deleteAssocication(checkGroup.getId());
Integer checkGroupId = checkGroup.getId();
this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
}
@Override
public List<CheckGroup> findAll() {
return checkGroupDao.findAll();
}
public void setCheckGroupAndCheckItem(Integer checkGroupId ,Integer[] checkitemIds){
if(checkitemIds!=null&&checkitemIds.length>0){
for (Integer checkitemId : checkitemIds) {
Map<String,Integer> map =new HashMap<>();
map.put("checkgroupId",checkGroupId);
map.put("checkitemId",checkitemId);
checkGroupDao.setCheckGroupAndCheckItem(map);
}
}
}
}
4.dao接口
public interface CheckGroupDao {
public void add(CheckGroup checkGroup);
public void setCheckGroupAndCheckItem(Map map);
public Page<CheckGroup> findByCondition(String queryString);
public CheckGroup findById(Integer id);
public List<Integer> findCheckItemIdsByCheckGroupId(Integer id);
public void edit(CheckGroup checkGroup);
public void deleteAssocication(Integer id);
public List<CheckGroup> findAll();
}
5.dao.xml
<mapper namespace="com.fightting.dao.CheckGroupDao">
<insert id="add" parameterType="com.fightting.pojo.CheckGroup">
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
select LAST_INSERT_ID()
selectKey>
insert into t_checkgroup(code,name,sex,helpCode,remark,attention)
values
(#{code},#{name},#{sex},#{helpCode},#{remark},#{attention})
insert>
<insert id="setCheckGroupAndCheckItem" parameterType="map">
insert into t_checkgroup_checkitem(checkgroup_id,checkitem_id)
values
(#{checkgroupId},#{checkitemId})
insert>
<select id="findByCondition" parameterType="string" resultType="com.fightting.pojo.CheckGroup">
select * from t_checkgroup
<if test="value!=null and value!='' and value.length>0">
where code=#{value} or name=#{value} or helpCode=#{value}
if>
select>
<select id="findById" parameterType="int" resultType="com.fightting.pojo.CheckGroup">
select * from t_checkgroup where id=#{id}
select>
<select id="findCheckItemIdsByCheckGroupId" parameterType="int" resultType="int">
select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{id}
select>
<update id="edit" parameterType="com.fightting.pojo.CheckGroup">
update t_checkgroup
<set>
<if test="name != null">
name = #{name},
if>
<if test="sex != null">
sex = #{sex},
if>
<if test="code != null">
code = #{code},
if>
<if test="helpCode != null">
helpCode = #{helpCode},
if>
<if test="attention != null">
attention = #{attention},
if>
<if test="remark != null">
remark = #{remark},
if>
set>
where id = #{id}
update>
<delete id="deleteAssocication" parameterType="int" >
delete from t_checkgroup_checkitem where checkgroup_id=#{id}
delete>
<select id="findAll" resultType="com.fightting.pojo.CheckGroup">
select * from t_checkgroup
select>
mapper>