bootscrap+angular+ssm整合(中)



Mapper接口
public interface EmpDaoMapper {

//添加
public void addEmp(Emp emp);
//批量刪除
public void batchDel(String eids);

//根據id刪除
public void deleteByEid(Integer emp_id);

//查詢所有
public List<Emp> selectAllEmps();


//根據id查詢員工
public Emp selectById(Integer emp_id);

//修改
public void updateEmp(Emp emp);

//查詢所有部門
public List<Dept> selectAllDepts();
}


EmpDaoMapper.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.frank.dao.EmpDaoMapper">
<!-- 添加員工 -->
<insert id="addEmp" parameterType="emp">
insert into t_emp(emp_name,gender,email,d_id) 
values(#{emp_name},#{gender},#{email},#{dept.dept_id})
</insert>

<!-- 修改員工 -->
<update id="updateEmp" parameterType="emp">
update t_emp set 
emp_name=#{emp_name},
gender=#{gender},
email=#{email},
d_id=#{dept.dept_id} 
where emp_id = #{emp_id}
</update>

<!-- 根據員工id刪除員工 -->
<delete id="deleteByEid" parameterType="java.lang.Integer">
delete from t_emp where emp_id = #{emp_id}
</delete>

<!-- 查詢所有部門 -->
<select id="selectAllDepts" resultType="dept">
select dept_id,dept_name from t_dept
</select>

<delete id="batchDel" parameterType="java.lang.String">
delete from t_emp where emp_id in (${value})
</delete>
<!-- 根據id查詢員工 -->
<select id="selectById" parameterType="java.lang.Integer" 
resultMap="empMap">

select e.*,d.* from t_emp e,t_dept d 
where e.d_id = d.dept_id and e.emp_id = #{emp_id}
</select>

<!-- 查詢所有員工 -->
<select id="selectAllEmps" resultMap="empMap">
select e.*,d.* from t_emp e,t_dept d 
where e.d_id = d.dept_id order by e.emp_id
</select>



<resultMap type="emp" id="empMap">
<id column="emp_id" property="emp_id"/>
<result column="emp_name" property="emp_name"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<association property="dept" javaType="dept">
<id column="dept_id" property="dept_id"/>
<result column="dept_name" property="dept_name"/>
</association>

</resultMap>

</mapper>



Msg工具類

import java.util.HashMap;
import java.util.Map;


public class Msg {


private int code;//狀態碼:100表示成功,200表示失敗
private String result;//操作成功、操作失敗
private Map<String, Object> data = new HashMap<String, Object>();


//返回成功結果的方法
public static Msg success(){
Msg msg = new Msg();
msg.setCode(100);
msg.setResult("操作成功");
return msg;
}
//返回失敗結果的方法
public static Msg fail(){
Msg msg = new Msg();
msg.setCode(200);
msg.setResult("操作失敗");
return msg;
}

//存放數據,支持鏈式操作
public Msg add(String key,Object value){
this.getData().put(key, value);
return this;
}


public Msg() {
super();
}
public Msg(int code, String result, Map<String, Object> data) {
super();
this.code = code;
this.result = result;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public Map<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> data) {
this.data = data;
}



}



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.frank.bean.Dept;
import com.frank.bean.Emp;
import com.frank.service.EmpService;
import com.frank.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Controller
@RequestMapping("/emp")
public class EmpController {


@Autowired
private EmpService empService;


@RequestMapping("/selectPage")
@ResponseBody
public Msg selectEmpsWithJson(
@RequestParam(value="pageno",defaultValue="1")Integer pageno
,@RequestParam(value="pagesize",defaultValue="2")Integer pagesize){
//指定查詢的頁碼和每頁的條數
PageHelper.startPage(pageno, pagesize);
//查詢所有的員工數據
List<Emp> empList = empService.selectAllEmps();
//將員工數據封裝在pageInfo中
PageInfo pageInfo = new PageInfo(empList);
return Msg.success().add("pageInfo", pageInfo);
}



@RequestMapping(value="/selectDepts",method=RequestMethod.POST)
@ResponseBody
public Msg selectDepts(){
List<Dept> depts = empService.selectAllDepts();
for (Dept dept : depts) {
System.out.println(dept);
}
return Msg.success().add("deptList", depts);
}



@RequestMapping("/save")
@ResponseBody
public Msg saveEmp(Emp emp){
empService.addEmp(emp);
return Msg.success();
}

@RequestMapping("/update")
@ResponseBody
public Msg updateEmp(Emp emp){
empService.updateEmp(emp);
return Msg.success();
}

@RequestMapping("delEmp")
@ResponseBody
public Msg delete(String eids){
if(eids.contains(",")){
empService.batchDel(eids);
}else{
empService.deleteByEid(Integer.parseInt(eids));
}
return Msg.success();
}

@RequestMapping("/selectEmp")
@ResponseBody
public Msg selectEmp(@RequestParam(value="emp_id")Integer emp_id){
Emp emp = empService.selectById(emp_id);
return Msg.success().add("emp", emp);
}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章