1. 导入jar包
antlr-2.7.7.jar | commons-lang3-3.2.jar | jstl.jar | spring-context-4.2.4.RELEASE.jar |
asm-3.3.jar | commons-logging-1.1.1.jar | log4j-1.2.16.jar | spring-core-4.2.4.RELEASE.jar |
asm-commons-3.3.jar | dom4j-1.6.1.jar | log4j-api-2.2.jar | spring-expression-4.2.4.RELEASE.jar |
asm-tree-3.3.jar | freemarker-2.3.22.jar | log4j-core-2.2.jar | spring-jdbc-4.2.4.RELEASE.jar |
c3p0-0.9.2.1.jar | geronimo-jta_1.1_spec-1.1.1.jar | mchange-commons-java-0.2.3.4.jar | spring-orm-4.2.4.RELEASE.jar |
com.springsource.org.aopalliance-1.0.0.jar | hibernate-c3p0-5.0.7.Final.jar | mysql-connector-java-5.1.7-bin.jar | spring-test-4.2.4.RELEASE.jar |
com.springsource.org.apache.commons.logging-1.1.1.jar | hibernate-commons-annotations-5.0.1.Final.jar | ognl-3.0.6.jar | spring-tx-4.2.4.RELEASE.jar |
com.springsource.org.apache.log4j-1.2.15.jar | hibernate-core-5.0.7.Final.jar | slf4j-api-1.6.1.jar | spring-web-4.2.4.RELEASE.jar |
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar | hibernate-jpa-2.1-api-1.0.0.Final.jar | slf4j-log4j12-1.7.2.jar | standard.jar |
commons-beanutils-1.8.3.jar | jandex-2.0.0.Final.jar | spring-aop-4.2.4.RELEASE.jar | struts2-core-2.3.24.jar |
commons-fileupload-1.3.1.jar | javassist-3.18.1-GA.jar | spring-aspects-4.2.4.RELEASE.jar | struts2-spring-plugin-2.3.24.jar |
commons-io-2.2.jar | jboss-logging-3.3.0.Final.jar | spring-beans-4.2.4.RELEASE.jar | xwork-core-2.3.24.jar |
2. 创建配置文件
2.1 Hibernate框架的配置文件
创建位置: 在实体类所在的包下创建配置映射文件,一般以[实体类名.hnm.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="实体类的全路径名" table="实体类对应的数据库表名"> <!-- 配置id属性 --> <id name="实体类中代表id的成员变量" column="数据库表中代表id的字段"> <!-- 主键生成策略 --> <generator class="native"/> </id> <!-- 配置其他普通属性 --> <property name="实体类成员变量1" column="数据库表中普通字段1"/> <property name="实体类成员变量2" column="数据库表中普通字段2"/> </class> </hibernate-mapping>
2.2 Spring框架的配置文件
创建位置: 在src下创建applicationContext.xml文件;
文件作用: 管理类的创建以及添加事务;
<?xml version="1.0" encoding="UTF-8"?> <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"> </beans>
2.3 Struts2框架的配置文件
创建位置: 在src目录下创建struts.xml文件,
文件作用: 用来配置Action;
<?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> </struts>
3. 编写代码
包结构;
创建User.java
package com.daben.entity;
public class User {
private int id;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + "]";
}
}
创建IUserDao.java
package com.daben.dao;
import com.daben.entity.User;
public interface IUserDao {
public void save(User user);
}
创建UserDaoImpl.java
package com.daben.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.daben.dao.IUserDao;
import com.daben.entity.User;
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {
@Override
public void save(User user) {
System.out.println("dao保存用户");
System.out.println(user);
// 把数据,保存到数据库中
this.getHibernateTemplate().save(user);
}
}
创建IUserService.java
package com.daben.service;
import com.daben.entity.User;
public interface IUserService {
public void save(User user);
}
创建UserServiceImpl.java
package com.daben.service.imple;
import org.springframework.transaction.annotation.Transactional;
import com.daben.dao.IUserDao;
import com.daben.entity.User;
import com.daben.service.IUserService;
public class UserServiceImpl implements IUserService {
private IUserDao UserDaoImpl;
public void setUserDaoImpl(IUserDao userDaoImpl) {
UserDaoImpl = userDaoImpl;
}
@Override
@Transactional
public void save(User user) {
System.out.println("service保存用户");
UserDaoImpl.save(user);
}
}
创建UserAction.java
package com.daben.action;
import com.daben.entity.User;
import com.daben.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = 6863073933314758008L;
private IUserService userServiceImpl;
public void setUserServiceImpl(IUserService userServiceImpl) {
this.userServiceImpl = userServiceImpl;
}
public void setUser(User user) {
this.user = user;
}
private User user = new User();
@Override
public User getModel() {
return user;
}
public String save() {
System.out.println("action保存用户");
userServiceImpl.save(user);
return NONE;
}
}
创建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/ssh/user_save" method="post">
姓名: <input type="text" name="username"/><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
4. 编写User.hbm.xml
位置: 在entity目录下
<?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.daben.entity.User" table="user"> <!-- 配置id属性 --> <id name="id" column="id"> <!-- 主键生成策略 --> <generator class="native"/> </id> <!-- 配置其他普通属性 --> <property name="username" column="username"/> </class> </hibernate-mapping>
5. 编写applicationContext.xml
位置: 在src目录下
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 先配置C3P0的连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///ssh"/> <property name="user" value="root"/> <property name="password" value="366464"/> </bean> <!-- LocalSessionFactoryBean加载配置文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 先加载连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 加载方言,加载可选 --> <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="mappingResources"> <list> <value>com/daben/entity/User.hbm.xml</value> </list> </property> </bean> <!-- 先配置平台事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 开启事务的注解 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置用户模块 --> <!-- 配置action类,必须是多例的 --> <bean id="userAction" class="com.daben.action.UserAction" scope="prototype"> <property name="userServiceImpl" ref="userServiceImpl"></property> </bean> <!-- 配置service类 --> <bean id="userServiceImpl" class="com.daben.service.imple.UserServiceImpl"> <property name="UserDaoImpl" ref="userDaoImpl"></property> </bean> <!-- 配置dao类 --> <bean id="userDaoImpl" class="com.daben.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
6. 编写struts.xml
<?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>
<!-- 先配置包结构 -->
<package name="crm" extends="struts-default" namespace="/">
<!-- 配置用户的Action,如果Action由Spring框架来管理,class标签上只需要编写ID值就OK -->
<action name="user_*" class="userAction" method="{1}">
</action>
</package>
</struts>
7. 编写web.xml
<?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"
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">
<display-name>ssh</display-name>
<!-- 配置Spring框架整合WEB的监听器 -->
<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>
<!-- 解决延迟加载的问题 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-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>
</filter-mapping>
<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>
8. 执行index.jsp
提交数据,完事
声明:
有一些博文是看的黑马程序员视频,然后跟着老师做的笔记
Spring是跟子路老师学的
特此感谢,写这些文章的目的是为了自己方便查阅