一、Spring整合Hibernate
方式一:零障碍整合
需要使用spring中提供的一个LocalSessionFacotry来加载Hibernate的配置文件。
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
方式二:spring管理hibernate配置
不在需要hibernate.cfg.xml文件,所有关于hibernate.cfg.xml文件中的配置都在spring的配置文件中来配置。
1.首先要配置数据源
2.接下来引入properties文件,里面是MySQL相关配置名
3.创建LocalSessionFactoryBean来完成spring管理hibernate中的SessionFactory
4.加载hbm.xml配置文件
<context:property-placeholder location="classpath:db.properties" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<!-- 以下属性在书写时不能省略hibernate -->
<!-- <props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
</props> -->
<!-- 上述的配置可以简写成以下 -->
<value>
hibernate.show_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
</value>
</property>
<!-- 加载hibernate的Xxx.hbm.xml配置文件 -->
<property name="mappingResources">
<list>
<value>brooker/domain/User.hbm.xml</value>
</list>
</property>
<!--<property name="mappingLocations"> <list> <value>classpath:cn/ithiema/domain/User.hbm.xml</value>
</list> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:cn/itheima/domain</value>
</list> </property> -->
</bean>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
spring整合hibernate后的DAO
<!-- 声明Dao -->
<bean id="userDao" class="brooker.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
编写service及测试
<!-- 声明Service -->
<bean id="userService" class="brooker.service.UserServiceImpl">
<!-- 将userDao注入到userService -->
<property name="userDao" ref="userDao"></property>
</bean>
Test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class UserTest {
@Autowired
private UserService userService;
@Test
public void testAdd() {
User user = new User();
user.setName("maria");
user.setAge(22);
userService.add(user);
}
@Test
public void testUpdate() {
User user = userService.findById(1);
user.setAge(21);
userService.update(user);
}
@Test
public void testFindAll() {
List<User> list = userService.findAll();
System.out.println(list);
}
@Test
public void testDel() {
User user = userService.findById(3);
userService.del(user);
}
@Test
public void testFindAllByCriteria() {
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.add(Restrictions.gt("age", 20));//选出age>20的user
List<User> list = userService.findAllByCriteria(dc);
System.out.println(list);
}
@Test
public void testFindByNameQuery() {
List<User> list = userService.findByNameQuery();
System.out.println(list);
}
}
二、Spring整合struts2框架
(1)方式一
创建一个addUser.jsp页面
<body>
<form action="${pageContext.request.contextPath}/user_add" method="post">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="addUser">
</form>
</body>
创建UserAction类
public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
private UserService userService;
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
//将UserService注入UserAction
public void setUserService(UserService userService) {
this.userService = userService;
}
public String add() {
userService.add(user);
return SUCCESS;
}
}
Struts.xml文件中配置
<package name="default" namespace="/" extends="struts-default">
<!-- class直接填applicationContext中配置的id就行 -->
<action name="user_*" class="userAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
applicationContext.xml中:
<!-- 配置action -->
<bean id="userAction" class="brooker.action.UserAction" scope="prototype">
<!-- 将userService注入action -->
<property name="userService" ref="userService"></property>
</bean>
(2)Spring整合struts2方式二:
web.xml
<!-- 以下是配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 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>
Spring整合struts2框架总结
1.如果在struts.xml文件中如果写的是全类名,我们使用action自动装配service方案
2.如果在struts.xml文件中这时,在applicationContext.xml文件中要配置<bean id=”userAction” class=” cn.itheima.action.UserAction”>
3.以上操作的前提是必须导入struts2-spring-plugin.xml文件
在这个文件中它改变struts2的bean工厂
4.默认情况下如果采用action自动装配service方案,这时每一次请求都会新创建一个action,并且service的装配类型是by name
5.如果我们采用的是spring管理action这种方案我们必须在声明中添加scope=prototype”,原因是struts2框架的action每一次请求都应该是一个新的action