项目结构
oa——父module
全局定义
<?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.chen</groupId>
<artifactId>oa</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>oa_dao</module>
<module>oa_service</module>
<module>oa_web</module>
</modules>
<properties>
<spring.version>4.3.14.RELEASE</spring.version>
</properties>
</project>
oa_dao——持久层
<?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">
<parent>
<artifactId>oa</artifactId>
<groupId>com.chen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>oa_dao</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
spring-dao.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启自动扫描-->
<context:component-scan base-package="com.oa.dao"/>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/oa?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置session工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--映射数据源-->
<property name="dataSource" ref="dataSource"/>
<!--起个别名,在映射文件中使用的时候可以直接使用类 -->
<property name="typeAliasesPackage" value="com.oa.entity"/>
</bean>
<!--映射器接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<!--扫描basePackage属性包路径下的所有接口,并与映射的配置文件关联起来,生成持久化操作对象-->
<!--所以接口和映射文件都必须在这个包路径下-->
<property name="basePackage" value="com.oa.dao"/>
</bean>
</beans>
oa_service——业务层
<?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">
<parent>
<artifactId>oa</artifactId>
<groupId>com.chen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>oa_service</artifactId>
<dependencies>
<dependency>
<groupId>com.chen</groupId>
<artifactId>oa_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
</dependencies>
</project>
spring-service.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="spring-dao.xml"/>
<context:component-scan base-package="com.oa.service"/>
<!--申明事务需要打开aspectj自动代理-->
<aop:aspectj-autoproxy/>
<!--声明事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--声明通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--将通知和切入点关联起来-->
<aop:config>
<aop:pointcut id="txpc" expression="execution(* com.oa.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
</aop:config>
</beans>
oa_web——表现层
<?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">
<parent>
<artifactId>oa</artifactId>
<groupId>com.chen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>oa_web</artifactId>
<packaging>war</packaging>
<name>oa_web Maven Webapp</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.chen</groupId>
<artifactId>oa_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>oa_web</finalName>
</build>
</project>
spring-web.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="spring-service.xml"/>
<context:component-scan base-package="com.oa.controller"/>
<!--开启MVC注解驱动-->
<mvc:annotation-driven/>
<!--静态资源交由servlet处理-->
<mvc:default-servlet-handler/>
<!--配置视图解析-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
拦截器设置
自定义拦截器
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
/** 过滤有关键字login的请求 **/
String url = httpServletRequest.getRequestURI();
if (url.toLowerCase().indexOf("login")>=0){
return true;
}
/** 放行登陆用户的请求 **/
HttpSession session = httpServletRequest.getSession();
if (session.getAttribute("employee")!=null){
return true;
}
httpServletResponse.sendRedirect("/to_login");
return false;
}
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
配置拦截器,使拦截器生效
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.oa.global.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
登陆实现
@Service("loginService")
public class LoginServiceImpl implements LoginService {
@Autowired
private EmployeeDao employeeDao;
public Employee login(String num, String password) {
Employee employee = employeeDao.select(num);
if (employee!=null&&employee.getPassword().equals(password)){
return employee;
}
return null;
}
public void changePassword(Employee employee) {
employeeDao.update(employee);
}
}
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
@RequestMapping("/to_login")
public String toLogin(){
return "login";
}
@RequestMapping("/login")
public String login(HttpSession session, @RequestParam String num, @RequestParam String password){
Employee employee = loginService.login(num,password);
if (employee==null){
return "redirect:to_login";
}
session.setAttribute("employee",employee);
return "redirect:self";
}
@RequestMapping("/self")
public String self(){
return "self";
}
@RequestMapping("/quit")
public String quit(HttpSession session){
session.setAttribute("employee",null);
return "redirect:to_login";
}
@RequestMapping("/to_change_password")
public String toChangePassword(){
return "change_password";
}
@RequestMapping("/change_password")
public String changePassword(HttpSession session, @RequestParam String old, @RequestParam String new1,@RequestParam String new2){
Employee employee = (Employee) session.getAttribute("employee");
if (employee.getPassword().equals(old)){
if (new1.equals(new2)){
employee.setPassword(new1);
loginService.changePassword(employee);
return "redirect:self";
}
}
return "redirect:to_change_password";
}
}
报销单管理
报销单处理流程:填写——提交——审核——打款
项目部署
War包
War包一般是在进行Web开发时,通常是一个网站Project下的所有源码的集合,里面包含前台HTML/CSS/JS的代码,也包含Java的代码。 当开发人员在自己的开发机器上调试所有代码并通过后,为了交给测试人员测试和未来进行产品发布,都需要将开发人员的源码打包成War进行发布。War包可以放在Tomcat下的webapps或者word目录下,随着tomcat服务器的启动,它可以自动被解压。
Tomcat服务器
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选,最新的Servlet和JSP规范总是能在Tomcat中得到体现。
正准备打包部署的时候,发现oa_web的target目录下已经生成了oa_web.war,把他复制到tomcat的webapps下就可以运行
源码地址:https://github.com/WE666/oa
总结
在这个项目中需要注意的技术点
1.声明式事务封装在业务层。封装在持久层太细,封装在表现层太粗。
2.Spring和Mybatis整合配置映射器接口。
3.不定向表单的处理。用集合来接收不定向表单的值