ssm整合开发(两张表)

==========pojo类========

public class Department {
private int id;
private String dname;
private String location;
private List<Employee> list;

}

public class Employee {
private Integer eid;
private String username;
private String password;
private String gender;
private Date birthday;
private Integer salary;
private String email;
private Department dd;

}

================mapper接口===========================

<?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.XXX.dao.EmployeeMapper">




<select id="selectEmployeeById" parameterType="int" resultMap="a">
select * from employee e,department d where e.d_id=d.id and
e.eid=#{eid};
</select>


<select id="selectByName" parameterType="String" resultMap="a">
select * from employee e,department d where e.d_id=d.id and
e.username=#{username}
</select>


<resultMap type="employee" id="a">
<id column="eid" property="eid"></id>
<result column="username" property="username" />
<result column="password" property="password" />
<result column="gender" property="gender" />
<result column="birthday" property="birthday" />
<result column="salary" property="salary" />
<result column="email" property="email" />
<association property="dd" javaType="department">
<id column="id" property="id" />
<result property="dname" column="dname" />
<result column="location" property="location" />
</association>
</resultMap>




<select id="selectEmployees" resultMap="a">
select * from employee e,department d where e.d_id=d.id
</select>


<select id="selectLike" parameterType="employee" resultMap="a">
select * from employee e,department d
<where>
<if test="username!=null and username!=''  ">
e.username like '%${username}%'
</if>
<if test="salary!=0 and salary!=''  ">
and e.salary >=#{salary}
</if>
</where>
</select>


<insert id="insert" parameterType="Employee">
insert into employee (username,password,gender,birthday,salary,email,d_id)
values(#{username},#{password},#{gender},#{birthday},#{salary},#{email},#{dd.id});
</insert>


<delete id="delete" parameterType="int">
delete from employee where eid=#{eid}
</delete>


<delete id="deleteEmployees" parameterType="String">
delete from employee where eid in ( ${value})
</delete>


<update id="update" parameterType="employee"  >
update employee set username=#{username},password=
#{password},gender=#{gender},birthday=#{birthday},salary =#{salary},email=#{email},d_id=#{dd.id}
where eid=#{eid};
</update>


<select id="daochu" resultMap="a" parameterType="String">
select * from employee e,department d where e.d_id=d.id
and e.eid in ( ${value} );
</select>




</mapper>

------------------------------------------------

public interface EmployeeMapper {


public void insert(Employee e);
public void update(Employee e);
public List<Employee> selectEmployees();
public Employee selectEmployeeById(int eid);
public void delete(int eid);
public List<Employee> selectLike(Employee e);
public List<Employee> daochu(String eids);
public void deleteEmployees(String eids);
public Employee selectByName(String username);


}

---------------------service-----------------------------------------------------

public interface EmployeeService {
public void insert(Employee e);
public void update(Employee e);
public List<Employee> selectEmployees();
public Employee selectEmployeeById(int eid);
public void delete(int eid);
public List<Employee> selectLike(Employee e);
public List<Employee> daochu(String eids);
public void deleteEmployees(String eids);
public boolean login(Employee e);
public boolean isExist(String username);
public Employee  selectByName(String username);

}

================================

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService{


@Autowired
private EmployeeMapper employeeMapper;


@Override
public void insert(Employee e) {
employeeMapper.insert(e);
}


/* 

*/
@Override
public void update(Employee e) {
employeeMapper.update(e);
}
@Override
public List<Employee> selectEmployees() {
List<Employee> list = employeeMapper.selectEmployees();
return list;
}

@Override
public Employee selectEmployeeById(int eid) {
Employee employee = employeeMapper.selectEmployeeById(eid);
return employee;
}
@Override
public void delete(int eid) {
      employeeMapper.delete(eid);
}

@Override
public List<Employee> selectLike(Employee e) {
List<Employee> list = employeeMapper.selectLike(e);
return list;
}

*/
@Override
public List<Employee> daochu(String eids) {
List<Employee> list = employeeMapper.daochu(eids);
return list;
}


/* 

*/
@Override
public void deleteEmployees(String eids) {
employeeMapper.deleteEmployees(eids);
}


@Override
public boolean login(Employee e) {
Employee employee = employeeMapper.selectByName(e.getUsername());
if (employee!=null && e.getPassword().equals(employee.getPassword())) {
return true;
}
return false;
}


/* 

*/
@Override
public boolean isExist(String username) {
Employee selectByName = employeeMapper.selectByName(username);
if(selectByName!=null){
return true;
}
return false;
}

==========controller类=====================

@Controller
@RequestMapping("employee")
public class TestEmployee {


@Autowired
private EmployeeService service;


@Autowired
private DepartmentService departmentService;


@RequestMapping("selectEmployeeById")
public String selectEmployeeById(int eid, Model model) {
Employee employee = service.selectEmployeeById(eid);
model.addAttribute("user", employee);
System.out.println(employee);
List<Department> selectDepartments = departmentService
.selectDepartments();
model.addAttribute("list", selectDepartments);
return "Employeeupdate";
};


@RequestMapping("update")
public String update(Employee e, Model model) {
service.update(e);
return "redirect:selectEmployees.do";
}


@RequestMapping("delete")
public String delete(int eid, Model model) {
service.delete(eid);
return "redirect:selectEmployees.do";
}


@RequestMapping("deleteEmployees")
public String deleteEmployees(String eids) {
service.deleteEmployees(eids);
return "redirect:selectEmployees.do";
};


@RequestMapping("selectDepartments")
public String selectDepartments(Model model) {
List<Department> selectDepartments = departmentService
.selectDepartments();
model.addAttribute("list", selectDepartments);
return "Employeereg";
};


@RequestMapping("selectEmployees")
public String selectEmployees(Model model) {
List<Employee> list = service.selectEmployees();
model.addAttribute("list", list);
return "EmployeeList";
}
//导出操作
@RequestMapping("daochu")
public String daochu(String eids) throws Exception {
File file = new File("D://employee.txt");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
List<Employee> list = service.daochu(eids);
for (Employee employee : list) {
bufferedWriter.write(employee.getEid() + employee.getUsername()
+ employee.getPassword() + employee.getBirthday()
+ employee.getGender() + employee.getSalary()
+ employee.getDd());
bufferedWriter.newLine();
}
bufferedWriter.close();
writer.close();
fileOutputStream.close();
return "userok";
}


@RequestMapping("insert")
public String insert(Employee e) {
service.insert(e);


return "Employeelogin";
}


@ResponseBody
@RequestMapping("isExist")
public String isExist(String username) {
boolean b = service.isExist(username);
if (b) {
return "ok";
}
return "error";
}


@RequestMapping("selectLike")
public String selectLike(Employee e, Model model) {


List<Employee> list = service.selectLike(e);
model.addAttribute("list", list);
return "EmployeeList";
};


@RequestMapping("login")
public String login(Employee e) {
service.login(e);


return "redirect:selectEmployees.do";


};


}

==============update.jsp=====================

<form action="${pageContext.request.contextPath }/employee/update.do">
             <input type="hidden" name="eid"  value="${user.eid }" />
   username:<input type="text" name="username" id="username" value="${user.username }" ><br>
    password:<input type="password" id="password" name="password"  value="${user.password }"><br>
    birthday:<input type="date"  name="birthday" value="<f:formatDate value="${user.birthday }"  pattern="yyyy-MM-dd"  />"><br>
      salary:<input type="number"  name="salary" value="${user.salary }"><br>
      email:<input type="text"  name="email" value="${user.email }"><br>
     gender:<input type="radio"  name="gender" <c:if test="${user.gender=='男' }"> checked="checked"</c:if>  value="男">男
     <input type="radio"  name="gender" <c:if test="${user.gender=='女' }"> checked="checked"</c:if> value="女">女
       <br>
dd.dname:<select name="dd.id">
   <c:forEach items="${list }" var="o">
       <option value="${o.id }" <c:if test="${o.id==user.dd.id }">selected="selected"</c:if>>${o.dname }</option>
   </c:forEach>
</select>
<br>

<input type="button" value="update"  />


</form>

====================reg.jsp====

<form action="${pageContext.request.contextPath }/employee/insert.do">
username:<input type="text" name="username" id="username" ><br>
password:<input type="password" id="password" name="password" ><br>
birthday:<input type="date"  name="birthday" ><br>
salary:<input type="number"  name="salary" ><br>
email:<input type="text"  name="email" ><br>
gender:<input type="radio"  name="gender"  value="男">男
<input type="radio"  name="gender"  value="女">女
<br>
dd.dname:<select name="dd.id">
<c:forEach items="${list }" var="one">
<option value="${one.id }" >${one.dname }</option>
</c:forEach>
</select><br>


<input type="button" value="reg"  />


</form>
========list.jsp===========================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
  <script type="text/javascript">
  $(function(){
   
  $("#a").click(function(){
  $(":checkbox[name='items']").attr("checked","checked");
  });
  $(":checkbox[name='items']").click(function(){
  $("#a").attr("checked",$(":checkbox[name='items']").length==$(":checkbox[name='items']:checked").length);
  });
  $("#b").click(function(){
  var aa=new Array();
  $(":checkbox[name='items']:checked").each(function(){
  aa.push($(this).val());
  });
  location.href="${pageContext.request.contextPath }/employee/daochu.do?eids="+aa;
  });
  
  $("#c").click(function(){
  var aa=new Array();
  $(":checkbox[name='items']:checked").each(function(){
  aa.push($(this).val());
  });
  location.href="${pageContext.request.contextPath }/employee/deleteEmployees.do?eids="+aa;
  });
  
  });
  </script>
  
  </head>
  
  <body>


<input type="button" value="导出" id="b"  />
<input type="button" value="删除" id="c"  />
<form action="${pageContext.request.contextPath }/employee/selectLike.do">
username:<input type="text" name="username"  ><br>
salary:<input type="text"  name="salary" ><br>
<input type="submit" value="搜索"  />


</form>
<table border="2">
<tr>
<td><input  type="checkbox" id="a"  /></td>
<td>id</td>
<td>username</td>
<td>password</td>
<td>gender</td>
<td>salary</td>
<td>birthday</td>
<td>email</td>
<td>dd.id</td>
<td>dd.dname</td>
<td>dd.location</td>
<td>操作</td>
</tr>
<c:forEach items="${list }" var="one">
<tr>
<td><input  type="checkbox" name="items" value="${one.eid }" /></td>
<td>${one.eid }</td>
<td>${one.username }</td>
<td>${one.password }</td>
<td>${one.gender }</td>
<td>${one.salary}</td>
<td><f:formatDate value="${one.birthday }"  pattern="yyyy-MM-dd"  /></td>
<td>${one.email }</td>
<td>${one.dd.id }</td>
<td>${one.dd.dname }</td>
<td>${one.dd.location }</td>
<td><a href="${pageContext.request.contextPath }/employee/delete.do?eid=${one.eid}">删除</a>  | <a href="${pageContext.request.contextPath }/employee/selectEmployeeById.do?eid=${one.eid}">修改</a> </td>
</tr>




</c:forEach>
</table>
  </body>
</html>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值