结构目录
- beans
- BeanUtils.java
package com.hrs.common.beans; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.BeansException; import org.springframework.beans.FatalBeanException; import org.springframework.util.Assert; public abstract class BeanUtils extends org.springframework.beans.BeanUtils{ @SuppressWarnings({ "rawtypes"}) public static void copyProperties(Object source, Object target) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); // 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等 if (value != null) { boolean isEmpty = false; if (value instanceof Set) { Set s = (Set) value; if (s == null || s.isEmpty()) { isEmpty = true; } } else if (value instanceof Map) { Map m = (Map) value; if (m == null || m.isEmpty()) { isEmpty = true; } } else if (value instanceof List) { List l = (List) value; if (l == null || l.size() < 1) { isEmpty = true; } } else if (value instanceof Collection) { Collection c = (Collection) value; if (c == null || c.size() < 1) { isEmpty = true; } } if (!isEmpty) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } } }
- NavigationTree.java
package com.hrs.common.beans; import java.util.ArrayList; import java.util.List; public class NavigationTree { private boolean expended; private String text; //文本信息 private String iconCls; //图标 private String viewType;//视图 private boolean leaf; //是否叶子节点 private boolean selectable; //是否可选 private List<NavigationTree> children = new ArrayList<>(); //子节点 public NavigationTree(boolean expended, String text, String iconCls,String viewType, boolean leaf,boolean selectable, List<NavigationTree> childrens) { this.expended = expended; this.text = text; this.iconCls = iconCls; this.viewType = viewType; this.leaf = leaf; this.selectable = selectable; this.children = childrens; } public boolean isExpended() { return expended; } public void setExpended(boolean expended) { this.expended = expended; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getIconCls() { return iconCls; } public void setIconCls(String iconCls) { this.iconCls = iconCls; } public boolean isLeaf() { return leaf; } public void setLeaf(boolean leaf) { this.leaf = leaf; } public boolean isSelectable() { return selectable; } public void setSelectable(boolean selectable) { this.selectable = selectable; } public List<NavigationTree> getChildren() { return children; } public void setChildren(List<NavigationTree> children) { this.children = children; } public String getViewType() { return viewType; } public void setViewType(String viewType) { this.viewType = viewType; } }
SessionUtil.java
package com.hrs.common.beans;
import javax.servlet.http.HttpSession;
import com.hrs.zhanshiyang.log.domain.Log;
public class SessionUtil {
public static String USER="log";
public static String EMPLOYEEID="employeeId";
public static String EMPLOYEENAME="employeeName";
/*设置用户到session*/
public static void setUser(HttpSession session,Log log){
session.setAttribute(USER,log);
setEmployeeId(session,log.getEmployeeId());
setEmployeeName(session,log.getEmployeeName());
}
/*从session中取登录信息*/
public static Log getUser(HttpSession session){
Object log=session.getAttribute(USER);
return log==null?null:(Log) log;
}
/*设置employeeId到session*/
public static void setEmployeeId(HttpSession session,String employeeId){
session.setAttribute(EMPLOYEEID,employeeId);
}
/*获取employeeId*/
public static String getEmployeeId(HttpSession session){
Object employeeId=session.getAttribute(EMPLOYEEID);
return employeeId==null?null:(String) employeeId;
}
/*设置employeeName到session*/
public static void setEmployeeName(HttpSession session,String employeeName){
session.setAttribute(EMPLOYEENAME,employeeName);
}
/*获取employeeName*/
public static String getEmployeeName(HttpSession session){
Object employeeName=session.getAttribute(EMPLOYEENAME);
return employeeName==null?null:(String) employeeName;
}
/*移除session*/
public static void removeAttribute(HttpSession session){
session.removeAttribute(USER);
session.removeAttribute(EMPLOYEEID);
session.removeAttribute(EMPLOYEENAME);
}
}
2.config
- WebMvcConfig
package com.hrs.common.config; import java.nio.charset.Charset; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer{ public void addResourceHandlers(ResourceHandlerRegistry registry){ //将所有/static/** 访问都映射到classpath:/static/ 目录下 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } @Bean public HttpMessageConverter<String> responseBodyConverter() { return new StringHttpMessageConverter(Charset.forName("UTF-8")); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(responseBodyConverter()); } @Override public void addCorsMappings(CorsRegistry registry){ registry.addMapping("/**") //.allowedOrigins("http://192.168.1.97") .allowedMethods("GET", "POST","DELETE","PUT") .allowCredentials(false).maxAge(3600); } }
3.web
-
ExtAjaxResponse.java
package com.hrs.common.web; import java.util.Map; public class ExtAjaxResponse { private boolean success= true; private String msg= ""; private Map<String,String> map; public ExtAjaxResponse() {} public ExtAjaxResponse(boolean success) { this.success = success; } public ExtAjaxResponse(boolean success,String msg) { this.success = success; this.msg = msg; } public ExtAjaxResponse(boolean success,Map<String,String> map) { this.success = success; this.map = map; } public boolean isSuccess() { return success; } public String getMsg() { return msg; } public Map<String, String> getMap() { return map; } }
-
ExtjsPageRequest.java
package com.hrs.common.web; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; public class ExtjsPageRequest { //分页条件 private int page= 1; private int limit= 20; //排序条件 private String sort= "id"; private String dir= "ASC"; public void setPage(int page) {this.page = page;} public void setLimit(int limit) {this.limit = limit;} public void setSort(String sort) {this.sort = sort;} public void setDir(String dir) {this.dir = dir;} public Pageable getPageable() { Pageable pageable = null; if(!sort.trim().equals("") && !dir.trim().equals("")){ Sort pageSort = new Sort(Direction.DESC,sort); if(!dir.equals("DESC")){ pageSort = new Sort(Direction.ASC,sort); } pageable=PageRequest.of(page-1, limit,pageSort); }else{pageable = PageRequest.of(page-1, limit);} return pageable; } }
-
TreeController.java
package com.hrs.common.web; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.hrs.common.beans.NavigationTree; import com.hrs.zhanshiyang.log.service.ILogService; @RestController @RequestMapping("/tree") public class TreeController { @Autowired private ILogService logService; @RequestMapping(value="/gettree") public NavigationTree getTree(@RequestParam(name="employeeId") String employeeId) { NavigationTree root = new NavigationTree(true,null,null,null,false,true,null); NavigationTree dashboard = new NavigationTree(false,"Dashboard","x-fa fa-desktop","admindashboard",true,true,null); /*NavigationTree login = new NavigationTree(false,"login","x-fa fa-check","login",true,true,null);*/ //员工信息 NavigationTree emp = new NavigationTree(false,"员工基本信息","x-fa fa-cubes",null,false,true,null); NavigationTree staffSearch=new NavigationTree(false,"查看个人信息", "x-fa fa-cube","staffSearchOne",true, true, null); NavigationTree fileSearch=new NavigationTree(false,"查看档案信息", "x-fa fa-cube","fileSearchOne", true, true, null); NavigationTree overtimeSearch=new NavigationTree(false,"查看加班信息", "x-fa fa-cube","overtimeSearch", true, true, null); NavigationTree leaveSearch=new NavigationTree(false,"查看请假信息", "x-fa fa-cube","leaveSearch", true, true, null); NavigationTree attendanceSearch=new NavigationTree(false,"查看考勤信息", "x-fa fa-cube","attendanceSearch", true, true, null); //档案信息 NavigationTree file = new NavigationTree(false,"员工档案信息","x-fa fa-book","file",true,true,null); List<NavigationTree> empChildren = new ArrayList<>(); empChildren.add(staffSearch); empChildren.add(fileSearch); empChildren.add(overtimeSearch); empChildren.add(leaveSearch); empChildren.add(attendanceSearch); emp.setChildren(empChildren); if(logService.findByEmployeeId(employeeId).getLogPermission().equals("1")) { List<NavigationTree> rootChildren = new ArrayList<>(); rootChildren.add(dashboard); rootChildren.add(emp); rootChildren.add(file); root.setChildren(rootChildren); } else { List<NavigationTree> rootChildren = new ArrayList<>(); rootChildren.add(dashboard); rootChildren.add(emp); root.setChildren(rootChildren); } return root; } @RequestMapping(value="/gettree1") public String getTree1(@RequestParam(name="employeeId") String employeeId) { if(logService.findByEmployeeId(employeeId).getLogPermission().equals("1")) { System.out.println("111"); return "1"; } else return"0"; } }
4.BaseDomain.java
-
package com.hrs.common; import java.io.Serializable; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; @MappedSuperclass public class BaseDomain <PK extends Serializable>{ private PK id; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public PK getId() { return id; } public void setId(PK id) { this.id = id; } }
-
ServletInitializer.java
-
package com.hrs; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(HrsApplication.class); } }
6.Test文件
-
package com.hrs.youzhenjieTest.employee; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Optional; import javax.transaction.Transactional; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.hrs.youzhenjie.contract.domain.Contract; import com.hrs.youzhenjie.contract.service.IContractService; import com.hrs.youzhenjie.employee.domain.Employee; import com.hrs.youzhenjie.employee.service.IEmployeeService; @RunWith(SpringRunner.class) @SpringBootTest public class HrsApplicationTests { @Autowired private IEmployeeService employeeService; @Autowired private IContractService contraService; @Test public void saveEmployee() { Employee employee = new Employee(); employee.setEmployeeId(new SimpleDateFormat("yyMMdd").format(new Date()) + "CC52302"); employee.setEmployeeDepartment("renshibu"); employee.setEmployeeEntryTime(new Date()); employee.setEmployeeLeaveTime(new Date()); employeeService.saveOrUpdate(employee); } @Test public void saveContract() { for (int i = 0; i < 5; i++) { Contract contract=new Contract(); contract.setEmployeeId("2530"+i); contract.setContractStartTime(new Date()); contract.setContractEndTime(new Date()); contract.setContractState("未签订"); contract.setContractType("劳动合同"); contract.setContractProperties("文本合同"); //contraService.saveOrUpdate(contract); } } @Test public void find() { Optional<Employee> employee = employeeService.searchById(3L); System.out.println(employee.get().getEmployeeName()); } @Test public void findAll() { List<Employee> lists = employeeService.searchAll(); for (Employee employee : lists) { System.out.println(employee.getEmployeeId()); } } @Test public void testCash(){ Employee employee = new Employee(); employee.setEmployeeId(new SimpleDateFormat("yyMMdd").format(new Date()) + "2530"); employee.setEmployeeDepartment("renshibu"); employee.setEmployeeEntryTime(new Date()); employee.setEmployeeLeaveTime(new Date()); employee.setEmployeeName("James"); employee.setEmployeeSex("男"); Contract contract=new Contract(); contract.setEmployeeId(new SimpleDateFormat("yyMMdd").format(new Date()) +"2530"); contract.setContractStartTime(new Date()); contract.setContractEndTime(new Date()); contract.setContractState("未签订"); contract.setContractType("劳动合同"); contract.setContractProperties("文本合同"); contract.setEmployee(employee); contraService.save(contract); // employee.setContract(contract); // employeeService.saveOrUpdate(employee); } @Test public void TestName(){ Employee employee=employeeService.findByEmployeeId("1812212530"); System.out.println(employee.getEmployeeName()); System.out.println(employee.getId()); } @Test public void sqlit(){ String fileName="xiaohuanren.gif"; System.out.println(fileName); String[] fileParts=fileName.split("\\."); for (String string : fileParts) { System.out.println(string); } } }