人事管理系统
配置环境
-
spring-mvc.xml
<!-- 组件扫描,扫描controller中的@Controller--><context:component-scan base-package="com.ssp.**.controller"/><!-- 注解驱动--><mvc:annotation-driven/><!-- 处理静态资源--><mvc:default-servlet-handler/>
-
spring-service.xml
<!--组件扫描器--><context:component-scan base-package="com.ssp.**.service"/>
-
spring-mybatis.xml
<bean id="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="myDataSource"/><property name="configLocation" value="classpath:mybatis.xml"/></bean><!-- 生成dao的代理对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.ssp.**.dao"/><property name="sqlSessionFactoryBeanName" value="SqlSessionFactoryBean"/></bean>
-
spring-db.xml
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/> </bean>
-
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><typeAliases><package name="com.ssp.common.beans"/></typeAliases><!-- 注册映射文件--><mappers><package name="com.ssp.**.dao"/></mappers> </configuration>
-
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///hrm jdbc.username=root jdbc.password=root
-
spring-tx.xml
<!-- 注册平台事务管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="myDataSource"/></bean><!-- 注册事务通知--><tx:advice id="txAdvice" transaction-manager="txManager"><!-- 指定连接点--><tx:attributes><tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/></tx:attributes></tx:advice><!-- AOP配置--><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* *..service.*.*(..))"/></aop:config>
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ssp</groupId><artifactId>hrm</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>hrm Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-jcl</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.2.9.RELEASE</version></dependency><!-- 事务 --><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.2.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.2.9.RELEASE</version></dependency><!--aspectj--><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.5</version><scope>runtime</scope></dependency><!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-servlet-api --><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-servlet-api</artifactId><version>8.5.13</version></dependency><!-- mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version></dependency><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.2</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.49</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.22</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2.1-b03</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.2</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.12.2</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.12.2</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version><scope>compile</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version><scope>compile</scope></dependency><!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency></dependencies><build><finalName>hrm</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource></resources></build> </project>
自定义标签的使用
-
自定义标签的使用
-
首先应该定义一个自定义标签的Java类(PageTag),继承SimpleTagSupport(简单标签)和传统标签,
-
编写标签库描述符(tld)文件(应该放在web下)
-
引入自定义标签
<%@taglib uri="" prefix=""%/>
-
User
User中可能存在的问题
-
用户登录:
首先进入登录页面,获取登录信息,判断登录信息是否能被查到(是否为空),不为空,就把信息放在session中,session.setAttribute(“login_user”,login_user)
-
为了防止不登录直接进入,需要添加Filter过滤器。
-
1.创建一个接口实现Filter类的方法。
-
2.定义需要放行的页面,样式,接口。
-
3.强制类型转换获取request和response
-
-
4.获取本次请求的URI
-
5.用for循环判断后缀名是否和URI一致。一致就用**filterChain.doFilter(request,response)**放行
-
6.判断当前用户是否登录。
-
7.登录也需要在web.xml中配置
-
<!-- 登录的过滤器--><filter><filter-name>loginFilter</filter-name><filter-class>com.ssp.Filter.LoginFilter</filter-class></filter><filter-mapping><filter-name>loginFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
-
-
-
用户退出。session.removeAttribute(“login_user”);
-
删除用户的时候不能把当前用户删除,不能删除存在外键的用户,否则会报DataIntegrityViolationException错误,将异常抛出就可以。
-
判断添加用户信息是否重复。
-
findUser:
@RequestMapping("/findUser")public String findUser(@RequestParam(defaultValue = "1") int pageIndex, User user,Model model){//定义page变量,用于分页Page page=new Page();//获取页面的起始索引,并放在page中page.setPageIndex(pageIndex);//查询用户的数量,传入user,是为了按条件查询时的使用int recordCount=userService.findUserCount(user);//将用户数放在page中page.setRecordCount(recordCount);//查询数据库中的用户信息List<User> users = userService.findUser(user,page);//传给前台,用户数据的显示model.addAttribute("users",users);//用于查询数据回显model.addAttribute("user",user);//用于分页使用model.addAttribute("page",page);return "/jsp/user/user.jsp";}
<select id="findUserLogin" resultType="User">select * from user_inf whereloginname=#{loginname} and password=#{password} </select> <select id="findUser" resultType="User">select * from user_inf<where><if test="user.username != null and user.username!=''">username like "%" #{user.username} "%"</if><if test="user.status != null and user.status!=''">and status = #{user.status}</if></where>limit #{page.firstLimitParam},#{page.pageSize} </select> <select id="findUserCount" resultType="int">select count(*) from user_inf<where><if test="username != null and username!=''">username like "%" #{username} "%"</if><if test="status != null and status!=''">and status = #{status}</if></where> </select>
-
updateUser
@RequestMapping("/updateUser")public String updateUser(int pageIndex,User user,Model model,String flag) {//flag为空的时候进行查询信息if (flag == null) {//获取要查询的用户信息User user1 = userService.findUserById(user.getId());//传给前台页面,用于显示model.addAttribute("user1", user1);//传pageIndex的值是为了再更改之后跳到更改的那一页,pageIndex还需要放在前台的隐藏域中model.addAttribute("pageIndex",pageIndex);return "/jsp/user/showUpdateUser.jsp";}else {//用rows判断是否插入成功int rows=userService.updateUser(user);if (rows>0) {//成功就返回页面+pageIndex用于查询所有用户return "redirect:/user/findUser?pageIndex="+pageIndex;}else {model.addAttribute("fail","用户信息修改失败!");return "/jsp/fail.jsp";}}}
<update id="updateUser">update user_inf setloginname=#{loginname},username=#{username},status=#{status},password=#{password}where id=#{id} </update>
-
addUser
@RequestMapping("/addUser")public String addUser(User user,Model model){int rows=userService.addUser(user);if(rows>0){//为了跳转到最新添加的一页//得到记录总数int recordCount = userService.findUserCount(null);Page page = new Page();//放进recordCount中page.setRecordCount(recordCount);//getTotalSize中有计算方法int totalSize = page.getTotalSize();return "redirect:/user/findUser?pageIndex="+totalSize;}else {model.addAttribute("fail","用户信息添加失败!");return "/jsp/fail.jsp";}}
<insert id="addUser">insert into user_inf (loginname,password,status,username) values (#{loginname},#{password},#{status},#{username}) </insert>
-
removeUser
@RequestMapping("removeUser")public String remove(Integer [] ids, Model model,HttpSession session){//用于判断是否删除当前用户//因为user放在了session域中User user = (User) session.getAttribute("login_user");//当前登录id和循环中id是否一样for (Integer id:ids){if (user.getId()==id){model.addAttribute("fail","不能删除当前登录用户!");return "/jsp/fail.jsp";}}//因为存在外键,所以不能删除存在外键的用户,所以会有异常,所以trytry {int rows = userService.removeUser(ids);//因为是要删除所选中的所有用户,所以rows==ids.lengthif(rows==ids.length){return "/user/findUser";}else {model.addAttribute("fail","用户信息删除失败!");return "/jsp/fail.jsp";}}catch (DataIntegrityViolationException e){model.addAttribute("fail","不能删除当前用户,因为存在公告信息和文档");return "/jsp/fail.jsp";}}
<delete id="removeUser">delete from user_inf where id in<!--传入数组时,用array,传入字符数组类型时,用list接收,或者在dao层方法的参数中加@Param注解--> <foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach> </delete>
-
checkLoginname
function checkLoginname() {/**获取登录的名字,判断是否和数据库中的信息一致。loginname="+$("#loginname").val()用于获取输入的登录名*/$.get("${pageContext.request.contextPath}/user/checkLoginname?loginname="+$("#loginname").val(),function (data) {if (data == "EXIST"){alert("登录名已存在,请重新输入!")/*** 设置登录名输入框的值为空*/$("#loginname").val("")}})}
Dept
和上边的一致,不用考虑User中的问题
Job
和上边的一致,不用考虑User中的问题
Employee
-
这个应该考虑下拉框的问题。下拉框信息的显示
-
findEmployee
-
前台接受显示
职位: <select name="job_id" style="width:143px;"><option value="">--请选择职位--</option> <c:forEach items="${requestScope.jobs }" var="job"> <option value="${job.id }" <c:if test="${job.id==employee.job.id}">selected</c:if>>${job.name }</option> </c:forEach> </select>
@RequestMapping("/findEmployee")public String findEmployee(@RequestParam(defaultValue = "1") int pageIndex, Integer job_id,Integer dept_id,Employee employee, Model model) {//点击查询时,job和dept传不到后台 //将job_id和dept_id传入employee中的job对象和dept对象,这样就不用当做条件传入if(job_id!=null){Job job = new Job();job.setId(job_id);employee.setJob(job);}if(dept_id!=null){Dept dept = new Dept();dept.setId(dept_id);employee.setDept(dept);}//System.out.println(employee);//查出所有的信息并传给前台,用于数据在下拉框的显示List<Job> jobs = employeeService.findAllJob();List<Dept> depts = employeeService.findAllDept();model.addAttribute("jobs",jobs);model.addAttribute("depts",depts);int recordCount = employeeService.findEmployeeCount(employee);Page page = new Page();page.setPageIndex(pageIndex);page.setPageSize(2);page.setRecordCount(recordCount);List<Employee> employees = employeeService.findEmployee(employee,page);model.addAttribute("employees",employees);model.addAttribute("page",page);model.addAttribute("employee",employee);// for (Employee employee1:employees) // System.out.println(employee1);return "/jsp/employee/employee.jsp";}
-
Notice
-
//因为添加的时候需要获取添加人的信息 User login_user = (User)session.getAttribute("login_user"); notice.setUser(login_user);
Ducument
文件上传需要满足的条件文件上传
-
前台:
- 有文件上传的输入框
- form表单的请求方式必须是post
- form表单的enctype=“multipart/form-data
-
后台:
-
需要文件上传的jar包
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version> </dependency>
-
接受文件的类型必须是MultipartFile
-
配置Multipart解析器(在spring-mvc中配置)
<!-- Multipart解析器--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
-
-
findDocument
@RequestMapping("/findDocument")public String findDocument(String title,@RequestParam(defaultValue = "1") int pageIndex,Model model){Page page = new Page();page.setPageIndex(pageIndex);int recordCount = documentService.findDocumentCount(title);page.setRecordCount(recordCount);model.addAttribute("page",page);List<Document> documents = documentService.findDocument(title,page);model.addAttribute("documents",documents);model.addAttribute("title",title);return "/jsp/document/document.jsp";}
因为牵扯到外键所以用resultMap
<select id="findUserById" resultType="User">select * from user_inf where id=#{user_id}</select><resultMap id="DocumentMap" type="Document"><id column="id" property="id"/><association property="user"javaType="User"select="findUserById"column="user_id"/></resultMap><select id="findDocument" resultMap="DocumentMap">select * from document_inf<where><if test="title != null and title != ''">title like '%' #{title} '%'</if></where>limit #{page.firstLimitParam},#{page.pageSize}</select>
-
updateDocument
需要考虑更改信息后删除原始文件
@RequestMapping("/updateDocument")public String updateDocument(Document document,String flag,Model model,int pageIndex) throws IOException {if (flag == null){Document document1 = documentService.findDocumentById(document);model.addAttribute("document1",document1);model.addAttribute("pageIndex",pageIndex);return "/jsp/document/showUpdateDocument.jsp";}else {//更改文件可能存在的问题//2. 判断更改文件时有没有上传新的文档if(!document.getFile().isEmpty()) {// 1.更改信息后文件没有被删除//String path = "A:\\upload";//获取原始文件信息Document document1 = documentService.findDocumentById(document);//判断原始文件是否存在File targetFile = new File(path, document1.getFilename());//如果原始文件存在就删除原始文件if (targetFile.exists()) {targetFile.delete();}//获取文件的原始名字String filename = UUID.randomUUID() + document.getFile().getOriginalFilename();//document.getFile().transferTo(new File(path, filename));document.setFilename(filename);}int rows = documentService.updateDocument(document);if (rows>0){return "redirect:/document/findDocument?pageIndex="+pageIndex;}else {model.addAttribute("fail","文档修改失败!");return "/jsp/fail.jsp";}}
-
addDocument
上传文件时,可能有的问题
- 目录不存在
- 文件名重复
@RequestMapping("/addDocument")public String addDocument(Document document, HttpSession session,Model model) throws IOException {//上传文件时,可能有的问题//1.目录不存在//2.文件名重复//1.保存上传文件到服务器// String path = "A:\\upload";//判断目录存在不File file = new File(path);if (!file.exists()){file.mkdirs();}//获取原始文件的名称String filename = UUID.randomUUID()+document.getFile().getOriginalFilename();//定义保存的文件就是把MultipartFile转换为File,保存到服务器对应地址document.getFile().transferTo(new File(path,filename));//2.数据库中插入数据,给document的filename赋值document.setFilename(filename);//获取当前登录用户User login_user = (User) session.getAttribute("login_user");document.setUser(login_user);int rows = documentService.addDocument(document);if (rows>0){int recordCount = documentService.findDocumentCount(null);Page page = new Page();page.setRecordCount(recordCount);int totalSize = page.getTotalSize();return "redirect:/document/findDocument?pageIndex="+totalSize;}else {model.addAttribute("fail","用户信息添加失败!");return "/jsp/fail.jsp";}}
-
removeDocument
@RequestMapping("/removeDocument")public String removeDocument(Integer [] ids,Model model){// String path = "A:\\upload";int rows = 0;for (Integer id:ids) {Document document = documentService.findDocumentById1(id);File targetFile = new File(path,document.getFilename());if (targetFile.exists()){targetFile.delete();}//定义for循环一个一个按id删除int i = documentService.removeDocument(id);//计算删除的总数if (i>0){rows++;}}if (rows==ids.length){return "/document/findDocument";}else {model.addAttribute("fail","文档删除失败!");return "/jsp/fail.jsp";}} }