- 新建一个maven项目
- 打开pom.xml文件配置基础依赖
<!-- 引用junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 引用servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- 配置hibernate依赖包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- 配置mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
- 创建所需要的包
- 创建数据库表
在MySQL中创建数据库表:
Create table t_user( user_id int primary key auto_increment, user_name varchar(50), pass_word varchar(50) ); |
添加一条数据:
insert into t_user(user_name,pass_word) values('admin',MD5('123')); |
- 创建对应实体类和其对应的实体类
- 配置hibernate.cfg.xml核心配置文件
- 进行测试,成功进行下一步
整合Spring
- 引入依赖spring
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
2.配置Spring核心配置文件:applicationContext.xml
对蓝色的部分进行修改
替换代码
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx " >http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
- 在rescources中创建数据库连接池配置文件:db.properties
配置文件代码
driver_Class=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssh?useUnicode=true&characterEncoding=UTF-8
uname=root
upwd=root
initPoolSize=3
maxPoolSize=20
- 在pom.xml中引入数据库连接池技术C3PO依赖:
<!-- 引入C3P0数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
- 配置applicationContext.xml(Spring整合重点)
先导入所要用的依赖
<!-- spring对orm的支持依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- 引入Spring 的AspectJ依赖,解析事务切点 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.13</version>
</dependency>
<!-- spring管理数据源 -->
<!-- 1.先加载db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.再配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver_Class}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${uname}"></property>
<property name="password" value="${upwd}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<!-- 3.配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/lzx/pojo/*.hbm.xml"></property>
</bean>
<!-- 4. 配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务(增强) -->
<tx:advice id="myAdivce" transaction-manager="transactionManager">
<!-- 配置增强属性 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 5.扫描com.zking.ssh.dao包下面所有的类,类中所有的方法 -->
<aop:pointcut expression="execution(* com.lzx.dao.*.*(..))" id="myCut"/>
<aop:advisor advice-ref="myAdivce" pointcut-ref="myCut"/>
</aop:config>
- 配置Dao方法
编写接口:IUserDao
编写实现类:UserDaoImpl,实现接口IUserDao
在applicationContext-dao.xml中配置对象
<!--配置UserDaoImpl-->
<bean id="userDaoImpl" class="com.lzx.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
测试
整合Struts2
- 在pom.xml中导入struts依赖
<!-- 引用struts2框架核心依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
- 配置web.xml文件
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- Web层页面
- 配置biz包
同时需要在applicationContext-biz.xml中配置UserBizImpl,为iUserDao注入一个对象。
<bean id="UserBizImpl" class="com.lzx.biz.UserBizImpl">
<!-- ref="UserDaoImpl"为applicationContext-dao.xml中的对象-->
<property name="iUserDao" ref="UserDaoImpl"></property>
</bean>
编写Action
配置applictaionContext-action.xml
<bean id="addAction" class="com.lzx.action.AddAction" scope="prototype">
<property name="iuserDao" ref="userDaoImpl"></property>
</bean>
配置struts.xml
在web.xml中加载Spring
<!--加载Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
如果你只有一个文件
就把applicationContext-*.xml
改为applicationContext.xml
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!--添加监听-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 引入web层-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>