login
/WEB-INF/views/main.jsp
/WEB-INF/views/{1}.jsp
/WEB-INF/login.jsp
/WEB-INF/views/user/list.jsp
/WEB-INF/views/user/input.jsp
user_pageList
/WEB-INF/views/dept/list.jsp
/WEB-INF/views/dept/input.jsp
dept_pageList
/WEB-INF/views/sys/list.jsp
org.hibernate.dialect.MySQL5InnoDBDialect
true
true
update
package gz.itcast.crm.dao.base;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
public abstract class BaseDaoImpl implements IBaseDao {
private HibernateTemplate ht;
public void setHt(HibernateTemplate ht) {
this.ht = ht;
}
Class targerClass;
public BaseDaoImpl(){
Class clazz = this.getClass();
Type type = clazz.getGenericSuperclass();
ParameterizedType pt = (ParameterizedType)type;
targerClass = (Class) pt.getActualTypeArguments()[0];
}
public List queryAll() {
return ht.loadAll(targerClass);
}
public void save(T t) {
ht.saveOrUpdate(t);
}
public List queryByHQL(String hql, Object... values) {
return (List) ht.find(hql, values);
}
public T get(int id) {
return (T) ht.get(targerClass, id);
}
public void delete(int id) {
T t = get(id);
ht.delete(t);
}
public List queryByPage(final String condition,final int curPage,final int pageSize,
final Object... values) {
return ht.execute(new HibernateCallback>() {
public List doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery("from "
+ targerClass.getSimpleName() + " o where 1=1 "
+ condition);
if(values !=null){
for(int i=0;i() {
public Long doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery("select count(o) from "+
targerClass.getSimpleName() + " o where 1=1 " + condition
);
// 赋值
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return (Long) query.uniqueResult();
}
}).intValue();
}
}
package gz.itcast.crm.service.impl;
import gz.itcast.crm.dao.IUserDao;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.UserQuery;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.StringUtils;
public class UserServiceImpl implements IUserService{
//注入UserDao
private IUserDao userDao;
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
public PageBean queryByPage(UserQuery userquery) {
PageBean pb = new PageBean();
//1.设置当前页码
pb.setCurPage(userquery.getCurPage());
//添加查询条件
String condition = "";
List param = new ArrayList();
if(userquery!=null){
//StringUtils: 既能判断null,也能判断空字符串
//关键词
if(!StringUtils.isEmpty(userquery.getKeyword())){
condition+=" and o.name like ? or o.realName like ? or o.phone like ? or o.email like ? ";
param.add("%"+userquery.getKeyword()+"%");
param.add("%"+userquery.getKeyword()+"%");
param.add("%"+userquery.getKeyword()+"%");
param.add("%"+userquery.getKeyword()+"%");
}
//录入时间
if(!StringUtils.isEmpty(userquery.getBeginTime())){
condition+=" and o.inputTime>=?";
param.add(userquery.getBeginTime());
}
if(!StringUtils.isEmpty(userquery.getEndTime())){
condition+=" and o.inputTime<=?";
param.add(userquery.getEndTime());
}
}
//2.设置当前页数
pb.setData(userDao.queryByPage( condition , pb.getCurPage(), pb.getPageSize(), param.toArray() ) );
//3.设置总记录数
pb.setTotalCount(userDao.queryByCount(condition,param.toArray()));
return pb;
}
public boolean checkName(String name,int id) {
//如果是添加,则全部数据进行检查;如果是修改,则排除当前修改的用户
List param = new ArrayList();
String hql = "from User where name=?";
param.add(name);
//修改操作
if(id!=0){
hql += " and id<>?";
param.add(id);
}
List list = userDao.queryByHQL(hql, param.toArray());
return list!=null&&list.size()>0;
}
public void save(User user) {
userDao.save(user);
}
public void delete(int id) {
if(id != 0){
userDao.delete(id);
}
}
public User get(int id) {
if(id !=0){
return userDao.get(id);
}
return null;
}
public List findAll() {
// TODO Auto-generated method stub
return userDao.queryAll();
}
}
package gz.itcast.crm.service.impl;
import gz.itcast.crm.dao.IDeptDao;
import gz.itcast.crm.entity.Dept;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IDeptService;
import gz.itcast.crm.web.query.DeptQuery;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.StringUtils;
public class DeptServiceImpl implements IDeptService {
private IDeptDao deptDao;
public void setDeptDao(IDeptDao deptDao) {
this.deptDao = deptDao;
}
public List queryAll() {
return deptDao.queryAll();
}
public void save(Dept dept) {
deptDao.save(dept);
}
public Dept get(int id) {
return deptDao.get(id);
}
public void delete(int id) {
deptDao.delete(id);
}
public PageBean queryByPage(DeptQuery query) {
//封装PageBean
PageBean pb = new PageBean();
//1.设置当前页码
pb.setCurPage(query.getCurPage());
String condition = "";
List param = new ArrayList();
if(query!=null){
if(!StringUtils.isEmpty(query.getKeyword())){
condition +=" and o.deptName like ? or o.descr like ? or o.leader.realName like ?";
param.add("%"+query.getKeyword()+"%");
param.add("%"+query.getKeyword()+"%");
param.add("%"+query.getKeyword()+"%");
}
//录入时间
if(!StringUtils.isEmpty(query.getBeginTime())){
condition+=" and o.inputTime>=?";
param.add(query.getBeginTime());
}
if(!StringUtils.isEmpty(query.getEndTime())){
condition+=" and o.inputTime<=?";
param.add(query.getEndTime());
}
if(query.getLeader()!=0){
condition +=" and o.leader.id=?";
param.add(query.getLeader());
}
}
//2.设置当前页数
pb.setData(deptDao.queryByPage( condition , pb.getCurPage(), pb.getPageSize(), param.toArray() ) );
//3.设置总记录数
pb.setTotalCount(deptDao.queryByCount(condition,param.toArray()));
return pb;
}
public boolean checkName(String deptName, int id) {
//如果是添加,则全部数据进行检查;如果是修改,则排除当前修改的用户
List param = new ArrayList();
String hql = "from Dept where deptName=?";
param.add(deptName);
//修改操作
if(id!=0){
hql += " and id<>?";
param.add(id);
}
List list = deptDao.queryByHQL(hql, param.toArray());
return list!=null&&list.size()>0;
}
}
package gz.itcast.crm.service.impl;
import gz.itcast.crm.dao.ISystemLogDao;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.SystemLog;
import gz.itcast.crm.service.ISystemLogService;
import gz.itcast.crm.web.query.SystemLogQuery;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.StringUtils;
public class SystemLogServiceImpl implements ISystemLogService {
private ISystemLogDao systemLogDao;
public void setSystemLogDao(ISystemLogDao systemLogDao) {
this.systemLogDao = systemLogDao;
}
public List queryAll() {
return systemLogDao.queryAll();
}
public void save(SystemLog SystemLog) {
systemLogDao.save(SystemLog);
}
public SystemLog get(int id) {
return systemLogDao.get(id);
}
public void delete(int id) {
systemLogDao.delete(id);
}
public PageBean queryByPage(SystemLogQuery query) {
//封装PageBean
PageBean pb = new PageBean();
//1.设置当前页码
pb.setCurPage(query.getCurPage());
//添加查询条件
String condition = "";
List param = new ArrayList();
if(query!=null){
if(!StringUtils.isEmpty(query.getKeyword())){
condition +=" and o.operator.realName like ? or o.func like ? or o.ip like ?";
param.add("%"+query.getKeyword()+"%");
param.add("%"+query.getKeyword()+"%");
param.add("%"+query.getKeyword()+"%");
}
//录入时间
if(!StringUtils.isEmpty(query.getBeginTime())){
condition+=" and o.inputTime>=?";
param.add(query.getBeginTime());
}
if(!StringUtils.isEmpty(query.getEndTime())){
condition+=" and o.inputTime<=?";
param.add(query.getEndTime());
}
if(query.getLeader()!=0){
condition +=" and o.operator.id=?";
param.add(query.getLeader());
}
}
//2.设置当前页数
pb.setData(systemLogDao.queryByPage( condition , pb.getCurPage(), pb.getPageSize(), param.toArray() ) );
//3.设置总记录数
pb.setTotalCount(systemLogDao.queryByCount(condition,param.toArray()));
return pb;
}
}
package gz.itcast.crm.web;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.UserQuery;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
public class UserAction extends BaseAction{
//注入userService
private IUserService userService;
public void setUserService(IUserService userService) {
this.userService = userService;
}
//接收User对象
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//接收UserQuery对象
UserQuery uquery = new UserQuery();
public UserQuery getUquery() {
return uquery;
}
public void setUquery(UserQuery uquery) {
this.uquery = uquery;
}
//跳转页面
public String input(){
if(user != null &&user.getId() !=0){
//修改操作
user = userService.get(user.getId());
}
return "input";
}
//查询所有用户
public String pageList(){
if(uquery==null ||uquery.getCurPage()==null || uquery.getCurPage().equals("")){
uquery.setCurPage(1);
ActionContext.getContext().getSession().put("Uquery", uquery);
}
else{
UserQuery uquery1 = (UserQuery) ActionContext.getContext().getSession().get("Uquery");
uquery1.setCurPage(uquery.getCurPage());
//System.out.println(uquery.getCurPage());
uquery = uquery1;
}
PageBean pb = userService.queryByPage(uquery);
putContext("pb", pb);
return SUCCESS;
}
//校验save
public void validateSave() {
boolean result = userService.checkName(user.getName(),user.getId());
if(result){
addFieldError("user.name", "用户名已重复");
}
}
//添加用户
public String save(){
user.setInputTime(new Date());
userService.save(user);
return "save";
}
//删除用户
public String del(){
userService.delete(user.getId());
return pageList();
}
}
package gz.itcast.crm.web;
import gz.itcast.crm.entity.Dept;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IDeptService;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.DeptQuery;
import java.util.Date;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
public class DeptAction extends BaseAction {
//注入deptService
private IDeptService deptService;
public void setDeptService(IDeptService deptService) {
this.deptService = deptService;
}
private IUserService userService;
public void setUserService(IUserService userService) {
this.userService = userService;
}
//设置对象
private Dept dept = new Dept();
private DeptQuery query = new DeptQuery();
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public DeptQuery getQuery() {
return query;
}
public void setQuery(DeptQuery query) {
this.query = query;
}
//跳转输入页面
public String input(){
if(dept != null &&dept.getId() !=0){
dept = deptService.get(dept.getId());
}
List list = userService.findAll();
ActionContext.getContext().getSession().put("list", list);
return "input";
}
//分页查询用户
public String pageList(){
//默认第一页
if(query==null || query.getCurPage()==null || query.getCurPage().equals("")){
query.setCurPage(1);
ActionContext.getContext().getSession().put("dquery", query);
}
else{
DeptQuery query1 = (DeptQuery) ActionContext.getContext().getSession().get("dquery");
query1.setCurPage(query.getCurPage());
query = query1;
}
//查询数据
PageBean pb = deptService.queryByPage(query);
//查询所有用户
List users = userService.findAll();
putContext("users", users);
putContext("pb", pb);
return SUCCESS;
}
//校验保存
public void validateSave() {
boolean result = deptService.checkName(dept.getDeptName(),dept.getId());
if(result){
addFieldError("dept.deptName", "用户名已重复");
}
}
//保存方法
public String save(){
//调用业务
if(dept==null || dept.getId()==0){
dept.setInputTime(new Date());
}
deptService.save(dept);
//设置时间
//回显列表页面
return "save";
}
//删除用户
public String delete(){
deptService.delete(dept.getId());
return pageList();
}
}
package gz.itcast.crm.web;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.SystemLog;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.ISystemLogService;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.SystemLogQuery;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
public class SystemLogAction extends BaseAction {
//注入IsystemLogService
private ISystemLogService systemLogService;
public void setSystemLogService(ISystemLogService systemLogService) {
this.systemLogService = systemLogService;
}
//注入IUserService
private IUserService userService;
public void setUserService(IUserService userService) {
this.userService = userService;
}
//设置query属性
private SystemLogQuery query = new SystemLogQuery();
public SystemLogQuery getQuery() {
return query;
}
public void setQuery(SystemLogQuery query) {
this.query = query;
}
//设置system属性
private SystemLog system = new SystemLog();
public SystemLog getSystem() {
return system;
}
public void setSystem(SystemLog system) {
this.system = system;
}
//分页查询用户
public String pageList(){
//默认第一页
if(query==null || query.getCurPage()==null || query.getCurPage().equals("")){
query.setCurPage(1);
ActionContext.getContext().getSession().put("squery", query);
}
else{
SystemLogQuery squery = (SystemLogQuery) ActionContext.getContext().getSession().get("squery");
squery.setCurPage(query.getCurPage());
query = squery;
}
//查询数据
PageBean pb = systemLogService.queryByPage(query);
List users = userService.findAll();
putContext("users", users);
putContext("pb", pb);
return SUCCESS;
}
//删除用户
public String delete(){
systemLogService.delete(system.getId());
return pageList();
}
}
package gz.itcast.crm.util;
import gz.itcast.crm.entity.SystemLog;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.ISystemLogService;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import com.opensymphony.xwork2.ActionContext;
public class SystemLogUtil {
private ISystemLogService systemLogService;
public void setSystemLogService(ISystemLogService systemLogService) {
this.systemLogService = systemLogService;
}
public void log(JoinPoint pt){
Object target = pt.getTarget();
if(target instanceof ISystemLogService){
return;
}
String clazzname = target.getClass().getSimpleName();
String method = pt.getSignature().getName();
String func = clazzname +":"+ method + "()";
SystemLog sl = new SystemLog();
sl.setInputTime(new Date());
String ip = ServletActionContext.getRequest().getRemoteHost();
sl.setIp(ip);
//sl.setIp("11111");
//获取登录用户
User u = new User();
u.setId(1);
sl.setOperator(u);
sl.setFunc(func);
//保存日志
systemLogService.save(sl);
}
}
/WEB-INF/login.jsp
osiv
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
sessionFactory
sessionFactory
osiv
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
ImgServlet
gz.itcast.crm.util.ImgServlet
ImgServlet
/ImgServlet.img
当整个项目都开发好了以后,在服务器中进行部署,在浏览器中打开的效果则为: