xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
二、项目设计思路及关键代码
1、dao层:
定义了一个接口,一个接口实现方法当做模板,其余的dao需继承模板dao
daoTemplate.java
package dao;
import java.util.List;
public interface daoTemplate {
public int save(T t)throws Exception;
public int delete(T t)throws Exception;
public int delete(Integer id)throws Exception;
public int update(T t)throws Exception;
public List select()throws Exception;
public T get(Integer id)throws Exception;
}
daoTemplateImpl.java
package dao;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
//HibernateDaoSupport继承HibernateTemplate(该类中存在一个方法 setSessionFactory(SessionFactory))
//HibernateDaoSupport需要外部注入hibernate的Sessionfactory
//每个pojo类对应的单笔业务Dao都要注入sessionfactory
public class daoTemplateImpl extends HibernateDaoSupport implements daoTemplate{
protected Class claz;
public daoTemplateImpl(){
//实例化claz
ParameterizedType pt =(ParameterizedType) this.getClass().getGenericSuperclass();
this.claz = (Class) pt.getActualTypeArguments()[0];
}
@Autowired
public void setSessionFactory1(SessionFactory sessionFactory) {
// TODO Auto-generated method stub
super.setSessionFactory(sessionFactory);
}
@Override
public int save(T t) throws Exception {
getSession().save(t);
return 1;
}
@Override
public int delete(T t) throws Exception {
getSession().delete(t);
return 1;
}
@Override
public int delete(Integer id) throws Exception {
T temp=(T)getSession().load(claz, id);
return delete(temp);
}
@Override
public int update(T t) throws Exception {
getSession().update(t);
return 1;
}
@Override
public List select() throws Exception {
String hql="from "+claz.getName();
Query query=getSession().createQuery(hql);
return query.list();
}
@Override
public T get(Integer id) throws Exception {
return (T)getSession().get(claz, id);
}
}
登录的代码:
@Repository
public class employeeDao extends daoTemplateImpl{
/**
* 模糊查询
* @param condition eg:"张三"
* @return List
*/
public List getBasic(String condition) {
String hql="from "+claz.getName()+" where empname like :name ";
Query query=getSession().createQuery(hql);
query.setString("name", "%"+condition+"%");
return query.list();
}
}
模糊查询的代码:
public List getBasic(String condition) {
//注意这里的empname是pojo类(实体类)的属性名
String hql="from "+claz.getName()+" where empname like :name ";
Query query=getSession().createQuery(hql);
query.setString("name", "%"+condition+"%");
return query.list();
}
注意:每个dao需要加上
@Repository注解
2、servicesDao:
注意:(1)要加上@Service注解
(2)将employeeDao做为属性,加上 @Autowired,并且并且get、set方法
employeeDao empDao;
package servicesDao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.employeeDao;
import entity.Employee;
@Service
public class employeeServiceDao {
@Autowired
employeeDao empDao;
public Employee save(Employee e)throws Exception{
empDao.save(e);
return e;
}
public boolean delete(Employee e)throws Exception{
return empDao.delete(e)>0;
}
public boolean update(Employee e)throws Exception{
return empDao.update(e)>0;
}
public List query()throws Exception{
return empDao.select();
}
public List getBasic(String condition)throws Exception{
return empDao.getBasic(condition);
}
public Employee get(Integer id)throws Exception{
return empDao.get(id);
}
public employeeDao getEmpDao() {
return empDao;
}
public void setEmpDao(employeeDao empDao) {
this.empDao = empDao;
}
}
其实
servicesDao 也应该写一个模板dao,这里省略了
3、controller层
userController.java:
package controller;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import entity.User;
import servicesDao.userServiceDao;
import utils.ImageUtil;
@Controller
public class userController {
@Autowired
userServiceDao useDao;
@RequestMapping("/user_login")
public String login(User u,String number,HttpSession session) throws Exception{
String imageCode=(String)session.getAttribute("imageCode");
if(imageCode.equals(number.toUpperCase())){
User user=useDao.login(u);
if(user!=null){
session.setAttribute("user", user);
return "redirect:/emp_list.action";
}
}
return "login";
}
@RequestMapping("/createValidCode")
public OutputStream createImg(HttpServletResponse response,HttpSession session)throws Exception{
// 1.调用工具类,生成验证码及图片
Map imageMap = ImageUtil.createImage();
// 2.从imageMap中取到验证码,并放入session
String imageCode = imageMap.keySet().iterator().next();
session.setAttribute("imageCode", imageCode.toUpperCase());
// 3.从imageMap中取到图片,转为输入流
BufferedImage image = imageMap.get(imageCode);
OutputStream output=response.getOutputStream();
ImageUtil.WriteOutputStream(image,output);
return output;
}
public userServiceDao getUseDao() {
return useDao;
}
public void setUseDao(userServiceDao useDao) {
this.useDao = useDao;
}
}
employeeController.java:
package controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import dao.employeeDao;
import entity.Employee;
import servicesDao.employeeServiceDao;
import utils.StringUtil;
@Controller
public class employeeController {
@Autowired
employeeServiceDao employDao;
@RequestMapping("/toAddPage")
public String toAddPage(Employee emp) throws Exception{
return "redirect:/emp_add.action";
}
@RequestMapping("/emp_add")
public String emp_add(Employee emp) throws Exception{
employDao.save(emp);
return "redirect:/emp_list.action";
}
@RequestMapping("/emp_list")
public String dispEmployee(Map models,String sqlname) throws Exception{
if(StringUtil.isNotEmpty(sqlname)){
models.put("employees", employDao.getBasic(sqlname));}
else{
models.put("employees", employDao.query());}
return "empList";
}
@RequestMapping("/emp_del")
public String deleteEmp(Integer id)throws Exception{
if(id!=null){
Employee e=new Employee();
e.setT_id(id);
employDao.delete(e);
}
return "redirect:/emp_list.action";
}
@RequestMapping("/emp_update")
public String updateEmpBofer(Integer id,Map models)throws Exception{
Employee e=employDao.get(id);
models.put("employee",e);
return "updateEmp";
}
@RequestMapping("/emp_updateEmp")
public String updateEmp(Employee emp)throws Exception{
employDao.update(emp);
return "redirect:/emp_list.action";
}
public employeeServiceDao getEmployDao() {
return employDao;
}
public void setEmployDao(employeeServiceDao employDao) {
this.employDao = employDao;
}
}
注意:(1)加@Controller注解
(2)加
@Autowired
employeeServiceDao employDao;//get、set方法
(3)@RequestMapping("/toAddPage") 对应访问的地址为/toAddPage.action
(4)Map models,可将里面的值传到页面中去显示
(5)public String emp_add(Employee emp) throws Exception 可接收页面中 name为emp的对象
(6)请求转发:redirect:/emp_list.action
4、工具类
三、前端代码
empList.jsp
<%@ page language="java" import="java.util.*,entity.Employee"
pageEncoding="utf-8"%>
emplist
当前的用户:
${sessionScope.user.username}
<%-- 当前的用户:
<%=session.getAttribute("username") %>
${sessionScope.user.username} --%>
<%@include file="head.jsp"%>
placeholder="请输入你要查询 的姓名" />
value="查询" />
Welcome!
<%
List employees = (List) request
.getAttribute("employees");
for (Employee e : employees) {
%>
<%=e.getT_id()%>
<%=e.getEmpname()%>
<%=e.getEmpsalary()%>
<%=e.getEmpage()%>
<%
}
%>
οnclick="location='addEmp.jsp'" />
四、github传送门
https://github.com/tangfuping/SSH_emp.git