具体工程会上传文件sshpro
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>hello.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- 配置struts2核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <!-- 配置spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
<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"> <!-- 数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/maven" /> <property name="user" value="root" /> <property name="password" value="123456" /> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 依赖dataSource --> <property name="dataSource" ref="dataSource" /> <!-- 创建工厂需要加载hibernate映射文件 --> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean> <bean id="customerDao" class="customerDaoImpl.CustomerImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="customerService" class="customerServiceImpl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"></property> </bean> <bean id="customerAction" class="customerAction.CustomerAction"> <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.i18n.encoding" value="UTF-8"></constant> <!-- 开发模式 --> <constant name="struts.devMode" value="true"></constant> <!-- 主题 --> <constant name="struts.ui.theme" value="simple"></constant> <!-- 扩展名 --> <constant name="struts.action.extension" value="action,,"></constant> <!-- 通用package --> <package name="customer" namespace="/" extends="struts-default"> <action name="CustomerAction_*" class="customerAction" method="{1}"> <result name="success">/success.jsp</result> </action> </package> </struts>
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- 会话工厂 --> <session-factory> <!-- 数据库方言,根据数据库选择 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--为了方便调试是否在运行hibernate时在日志中输出sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 是否对日志中输出的sql语句进行格式化 --> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="current_session_context_class">thread</property> <!-- 加载映射文件 --> <mapping resource="domain/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
文件结构图:
package domain;
public class Customer {
private Long custId;
private String custName;
private Long custUserId;
private Long custCreateId;
private String custIndustry;
private String custLevel;
private String custLinkman;
private String custPhone;
private String custMobile;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public Long getCustUserId() {
return custUserId;
}
public void setCustUserId(Long custUserId) {
this.custUserId = custUserId;
}
public Long getCustCreateId() {
return custCreateId;
}
public void setCustCreateId(Long custCreateId) {
this.custCreateId = custCreateId;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustLinkman() {
return custLinkman;
}
public void setCustLinkman(String custLinkman) {
this.custLinkman = custLinkman;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustMobile() {
return custMobile;
}
public void setCustMobile(String custMobile) {
this.custMobile = custMobile;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Customer [custId=" + custId + ", custName=" + custName
+ ", custUserId=" + custUserId + ", custCreateId="
+ custCreateId + ", custIndustry=" + custIndustry
+ ", custLevel=" + custLevel + ", custLinkman=" + custLinkman
+ ", custPhone=" + custPhone + ", custMobile=" + custMobile
+ "]";
}
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-2-26 16:58:40 by Hibernate Tools 4.3.1.Final --> <hibernate-mapping> <class name="domain.Customer" table="cst_customer" optimistic-lock="version"> <id name="custId" type="java.lang.Long"> <column name="cust_id" /> <generator class="identity" /> </id> <property name="custName" type="string"> <column name="cust_name" length="32" not-null="true"></column> </property> <property name="custUserId" type="java.lang.Long"> <column name="cust_user_id"></column> </property> <property name="custCreateId" type="java.lang.Long"> <column name="cust_create_id"></column> </property> <property name="custIndustry" type="string"> <column name="cust_industry" length="32"></column> </property> <property name="custLevel" type="string"> <column name="cust_level" length="32"></column> </property> <property name="custLinkman" type="string"> <column name="cust_linkman" length="64"></column> </property> <property name="custPhone" type="string"> <column name="cust_phone" length="64"></column> </property> <property name="custMobile" type="string"> <column name="cust_mobile" length="16"></column> </property> </class> </hibernate-mapping>
package customerAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import customerService.CustomerService;
import domain.Customer;
public class CustomerAction extends ActionSupport{
private String id;
private CustomerService customerService;
private Customer customer;
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String findCustomerByid(){
System.out.println("查询用户的id:"+id);
customer = customerService.findById(Long.parseLong(id));
ActionContext.getContext().getValueStack().push(customer);
return "success";
}
}
package customerServiceImpl;
import customerDao.CustomerDao;
import customerService.CustomerService;
import domain.Customer;
public class CustomerServiceImpl implements CustomerService{
private CustomerDao customerDao;
public CustomerDao getCustomerDao() {
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
@Override
public Customer findById(Long id) {
// TODO Auto-generated method stub
Customer c = customerDao.findById(id);
return c;
}
}
package customerDaoImpl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import customerDao.CustomerDao;
import domain.Customer;
public class CustomerImpl implements CustomerDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Customer findById(Long id) {
// TODO Auto-generated method stub
Session session = this.getSessionFactory().openSession();
Customer customer = session.get(Customer.class, id);
session.close();
return customer;
}
}
<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>cn.xw.com</groupId> <artifactId>sshpro</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>sshpro</name> <description>maven整合ssh</description> <!-- 属性 --> <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> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <!-- 设置编译版本为1.7 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- maven内置 的tomcat6插件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <!-- 可以灵活配置工程路径 --> <path>/ssh</path> <!-- 可以灵活配置端口号 --> <port>8080</port> </configuration> </plugin> </plugins> </build> </project>