一.搭建项目骨架
整合项目采用父子工程,由三个子模块构成,分别是,表现层maven_web,业务层maven_service,dao层maven_dao,三个子模块继承一个父项目maven_ssh,下面展示的是项目的基本构成,可以一开始就搭建好整体的骨架,再完善填充细节,也可以一个一个模块的来,一般先从dao层开始,dao层属于底层构成,service ,web都需要依赖dao
二.构建父项目,并配置pom文件
配置父类pom.xml依赖,锁定ssh的版本号,配置tomcat7插件
-
<?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.itheima</groupId>
-
<artifactId>maven_ssh</artifactId>
-
<packaging>pom</packaging>
-
<version>1.0-SNAPSHOT</version>
-
<modules>
-
<module>maven_dao</module>
-
<module>maven_service</module>
-
<module>maven_web</module>
-
</modules>
-
<!-- 属性 -->
-
<properties>
-
<spring.version>4.2.4.RELEASE</spring.version>
-
<hibernate.version>5.0.7.Final</hibernate.version>
-
<struts.version>2.3.24</struts.version>
-
</properties>
-
<!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
-
<dependencyManagement>
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-aspects</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-orm</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-test</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-web</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.hibernate</groupId>
-
<artifactId>hibernate-core</artifactId>
-
<version>${hibernate.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.struts</groupId>
-
<artifactId>struts2-core</artifactId>
-
<version>${struts.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.struts</groupId>
-
<artifactId>struts2-spring-plugin</artifactId>
-
<version>${struts.version}</version>
-
</dependency>
-
</dependencies>
-
</dependencyManagement>
-
<!-- 依赖管理 -->
-
<dependencies>
-
<!-- spring -->
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-aspects</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-orm</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-test</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-web</artifactId>
-
</dependency>
-
<!-- hibernate -->
-
<dependency>
-
<groupId>org.hibernate</groupId>
-
<artifactId>hibernate-core</artifactId>
-
</dependency>
-
<!-- 数据库驱动 -->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
<version>5.1.6</version>
-
<scope>runtime</scope>
-
</dependency>
-
<!-- c3p0 -->
-
<dependency>
-
<groupId>c3p0</groupId>
-
<artifactId>c3p0</artifactId>
-
<version>0.9.1.2</version>
-
</dependency>
-
<!-- 导入 struts2 -->
-
<dependency>
-
<groupId>org.apache.struts</groupId>
-
<artifactId>struts2-core</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.struts</groupId>
-
<artifactId>struts2-spring-plugin</artifactId>
-
</dependency>
-
<!-- servlet jsp -->
-
<dependency>
-
<groupId>javax.servlet</groupId>
-
<artifactId>servlet-api</artifactId>
-
<version>2.5</version>
-
<scope>provided</scope>
-
</dependency>
-
<dependency>
-
<groupId>javax.servlet</groupId>
-
<artifactId>jsp-api</artifactId>
-
<version>2.0</version>
-
<scope>provided</scope>
-
</dependency>
-
<!-- 日志 -->
-
<dependency>
-
<groupId>org.slf4j</groupId>
-
<artifactId>slf4j-log4j12</artifactId>
-
<version>1.7.2</version>
-
</dependency>
-
<!-- jstl -->
-
<dependency>
-
<groupId>javax.servlet</groupId>
-
<artifactId>jstl</artifactId>
-
<version>1.2</version>
-
</dependency>
-
</dependencies>
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.apache.tomcat.maven</groupId>
-
<artifactId>tomcat7-maven-plugin</artifactId>
-
<version>2.2</version>
-
</plugin>
-
</plugins>
-
</build>
-
</project>
提示导入,直接选auto,自动导入
三、构建dao层子模块,mave_dao
选择父类工程,鼠标右键,选择new --model,dao层,service层的构建不需要勾选create form achetype(通过骨架生成),web层可以勾选
生成maven_dao,可以从maven_dao的pom看到继承关系
接下来完善相关的类和配置文件,类放在java文件夹,配置文件放在resources文件夹,
包:com.it.dao---接口:CustomerDao
包:com.it.dao.impl--实现类:CustomerDaoImpl
包:com.it.daomian--Customer实体类
-
package com.it.dao;
-
import com.it.daomain.Customer;
-
public interface CustomerDao {
-
Customer findById(Long id);
-
}
-
package com.it.dao.impl;
-
import com.it.dao.CustomerDao;
-
import com.it.daomain.Customer;
-
import org.hibernate.SessionFactory;
-
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
-
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
-
public void setS(SessionFactory sessionFactory){
-
this.setSessionFactory(sessionFactory);
-
}
-
public Customer findById(Long id) {
-
Customer customer = this.getHibernateTemplate().get(Customer.class, id);
-
return customer;
-
}
-
}
package com.it.daomain; public class Customer { private Long id; private String name; private String password; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Customer{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
配置文件,因为将hibernate的配置文件整合到了spring配置文件中,只保留了实体类的映射文件
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.it.daomain.Customer" table="customer"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <property name="password"/> </class> </hibernate-mapping>
因为模块整合,dao ,service,web分别配置自己的spring配置文件,
applicationContext.dao.xml
注意:引入heibernate映射文件的格式,classpath不能丢,这里只配置和dao层相关的连接池,sessionfactory,hibernate模板
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/maven-ssh"></property> <property name="user" value="root"></property> <property name="password" value="123"></property> <property name="checkoutTimeout" value="3000"></property> <property name="idleConnectionTestPeriod" value="1000"></property> <property name="maxPoolSize" value="15"></property> <property name="minPoolSize" value="5"></property> </bean> <!--配饰session工厂--> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingLocations" value="classpath:Customer.hbm.xml"></property> </bean> <!--配置hibernate模板--> <bean id="HibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="customerDao" class="com.it.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
四、构建service层模块maven_service
service层需要依赖dao层,我们需要在1.service模块的pom中引入dao层模块,还需要在applicationContext.service.xml里面导入applicationContext.dao.xml,同时需要在spring配置文件里面配置事务管理,并开启事务注解,这个项目里面只涉及数据的查找,因为没有用到事务管理
<?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>maven_ssh</artifactId> <groupId>com.itheima</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>maven_service</artifactId> <dependencies> <dependency> <groupId>com.itheima</groupId> <artifactId>maven_dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
接下来完善service层的java类和spring配置文件
包:com.it.service--接口:CustomerService
包:com.it.service.impl--实现类:CustomerServiceImpl
package com.it.service; import com.it.daomain.Customer; public interface CustomerService { Customer findById(Long id); }
package com.it.service.impl; import com.it.dao.CustomerDao; import com.it.daomain.Customer; import com.it.service.CustomerService; public class CustomerServiceImpl implements CustomerService { private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; } public Customer findById(Long id) { Customer customer = customerDao.findById(id); return customer; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="applicationContext.dao.xml"></import> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> <bean id="customerService" class="com.it.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"></property> </bean> </beans>
五、构建web层子模块maven_web,这一层需要依赖service和dao层,但是service层已经导入了dao层依赖,通过依赖传递,web层只需要导入service的依赖就行了,构建web层,我们需要勾选web骨架
pom文件
<?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>maven_ssh</artifactId> <groupId>com.itheima</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>maven_web</artifactId> <packaging>war</packaging> <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>com.itheima</groupId> <artifactId>maven_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
这里需要注意一下,idea不会为你自动生成java,resources的文件夹,需要我们自己手动添加,并设置,像这样
新建一个java文件夹,然后选中右键--底部mark direrctory as --sources root
新建一个resources文件夹,然后鼠标右键----底部mark direrctory as --resources root
接下类完善java类和配置文件,并在pom.文件中导入service层,在applicationContext.web.xml里引入applicationContext.service.xml
包:com.it.action--struts2的acton类--CustomerAction
aplicationContext.web.xml
struts.xml
package com.it.action; import com.it.daomain.Customer; import com.it.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; public class CustomerAction extends ActionSupport { private Customer customer; private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } private CustomerService customerService; public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } public String find(){ customer = customerService.findById(id); return SUCCESS; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="applicationContext.service.xml"></import> <bean id="customerAction" class="com.it.action.CustomerAction" scope="prototype"> <property name="customerService" ref="customerService"></property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <package name="maven_ssh" extends="struts-default" namespace="/"> <action name="find" class="customerAction" method="find"> <result name="success">success.jsp</result> </action> </package> </struts>
到这里差不多大功告成了,但是我们还缺了一样非常重要的东西,web.xml还需要配置,前端的jsp显示页面也需要写一下
这里需要注意:需要配置参数,指明配置文件的加载路径,这里只需要
applicationContext.web.xml,这里面已经导入了dao层,service层的配置文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.web.xml</param-value> </context-param>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.web.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
jsp页面:这里用到了struts2的el表达式来获取stackValue值栈里的值
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Title</title> </head> <body> 欢迎:${customer.name} </body>