Hibernate核心配置文件
Hibernate 映射文件
Hibernate的操作
Action创建方式
Hibernate Dao层 DB
两两整合
Struts2 和 Spring
Action 创建有Spring管理
<bean id="" class="" scope="prototype">bean>
Spring 和 Hibernate
数据库信息由Spring配置
Step1: 导入jar包
Step2: 创建Action
@Controller("userAction") //spring通过注解方式创建对象
@Scope("prototype") //action是多实例
public class UserAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("创建Action......");
return NONE;
}
}
Step3: 配置文件
Struts2的核心配置struts.xml
<struts>
<constant name="struts.i18n.encoding" value="UTF-8">constant>
<package name="demo" extends="struts-default" namespace="/">
<action name="userAction" class="userAction">action>
package>
struts>
Spring核心配置spring_bean.xml
<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">
<context:component-scan base-package="com.jeff">context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver">property>
<property name="jdbcUrl" value="jdbc:mysql:///jeff">property>
<property name="user" value="root">property>
<property name="password" value="123456">property>
bean>
beans>
web.xml配置
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring_bean.xmlparam-value>
context-param>
把Hibernate里面的SessionFactory的创建交由Spring管理
Step1: 导入jar包
Step2: 创建实体类
Step3: 配置文件
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<mapping resource="com/jeff/mapping/Book.hbm.xml"/>
session-factory>
hibernate-configuration>
Book.hbm.xml
<hibernate-mapping>
<class name="com.jeff.entity.Book" table="book">
<id name="bookId" column="book_id">
<generator class="native">generator>
id>
<property name="bookName" column="book_name">property>
<property name="author" column="author">property>
class>
hibernate-mapping>
spring_bean.xml
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver">property>
<property name="jdbcUrl" value="jdbc:mysql:///jeff">property>
<property name="user" value="root">property>
<property name="password" value="123456">property>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocations" value="classpath:hibernate.cfg.xml">property>
bean>
注意HibernateTemplate进行crud操作时,必须手动配置事务,否则出项以下错误
Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
<context:component-scan base-package="com.jeff">context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver">property>
<property name="jdbcUrl" value="jdbc:mysql:///jeff">property>
<property name="user" value="root">property>
<property name="password" value="123456">property>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocations" value="classpath:hibernate.cfg.xml">property>
bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
//接口
package com.jeff.dao;
public interface BaseDao<T> {
/**
* 添加单个对象
* @param obj 需要持久化的对象
* @return
*/
public int add(T obj);
/**
* 删除单个对象
* @param obj 根据ID删除对象
* @return
*/
public int delete(Integer id);
}
// 实现
package com.jeff.dao.impl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
import com.jeff.dao.BaseDao;
@Transactional
public class BaseDaoImpl<T> implements BaseDao<T>{
private static Logger logger = Logger.getLogger(BaseDaoImpl.class);
//属性
@Autowired
private HibernateTemplate hibernateTemplate;
/**********------------CRUD操作--------******************/
@Override
public int add(T obj) {
logger.info("----向数据库添加实体类-------");
hibernateTemplate.save(obj);
return 0;
}
@Override
public int delete(Integer id) {
return 0;
}
}
//接口
package com.jeff.service;
import com.jeff.dao.BaseDao;
import com.jeff.entity.Book;
public interface BookService extends BaseDao<Book>{
}
//实现
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jeff.dao.impl.BaseDaoImpl;
import com.jeff.entity.Book;
import com.jeff.service.BookService;
@Service("bookService")
public class BookServiceImpl extends BaseDaoImpl<Book>
implements BookService {
}
package com.jeff.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.jeff.entity.Book;
import com.jeff.service.BookService;
import com.opensymphony.xwork2.ActionSupport;
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {
@Autowired
private BookService bookService;
@Override
public String execute() throws Exception {
System.out.println("创建Action......");
Book book = new Book("Spring 实战", "Craig Walls");
bookService.add(book);
return NONE;
}
}
可以将hibernate核心配置去掉,将所有的信息都在spring配置文件中配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
props>
property>
<property name="mappingResources">
<list>
<value>com/jeff/mapping/Book.hbm.xmlvalue>
list>
property>
bean>