数据库:MySQL8
JDK:13
<hibernate-mapping>
<class name="映射类的带包的全类名" table="与类对应的数据库中表的名字">
<id name="id" column="字段名">
<generator class="native">generator>
id>
<property name="普通属性" column="字段名">property>
class>
hibernate-mapping>
public class UserDaoImpl implements UserDao {
//使用HibernateTemplate模板
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User user) {
this.hibernateTemplate.save(user);
}
}
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save(User user) {
userDao.save(user);
}
}
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<mapping resource="spring_hibernate/User.hbm.xml">mapping>
session-factory>
hibernate-configuration>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver">property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC">property>
<property name="user" value="root">property>
<property name="password" value="123">property>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="classpath:spring_hibernate/hibernate.cfg.xml">property>
bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<bean id="userDao" class="spring_hibernate.dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate">property>
bean>
<bean id="userService" class="spring_hibernate.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* spring_hibernate.service..*.*(..))">aop:advisor>
aop:config>
beans>
//Spring整合JUnit
@RunWith(SpringJUnit4ClassRunner.class)
//加载applicationContext.xml
@ContextConfiguration(locations = "classpath:spring_hibernate/applicationContext.xml")
public class testApp {
@Autowired
private UserService userService;
@Test
public void test(){
User user = new User();
user.setUsername("Jack");
user.setPassword("123");
user.setAge(12);
userService.save(user);
}
}
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public void save(User user) {
//从父类HibernateDaoSupport中获取HibernateTemplate
this.getHibernateTemplate().save(user);
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver">property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC">property>
<property name="user" value="root">property>
<property name="password" value="123">property>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
props>
property>
<property name="mappingLocations" value="classpath:spring_hibernate/*/User.hbm.xml">property>
bean>
<bean id="userDao" class="spring_hibernate.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<bean id="userService" class="spring_hibernate.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* spring_hibernate.service..*.*(..))">aop:advisor>
aop:config>
beans>
public class UserAction extends ActionSupport implements ModelDriven<User> {
//模型驱动封装数据
User user = new User();
@Override
public User getModel() {
return user;
}
//Spring动态注入userService
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String save(){
userService.save(user);
return "success";
}
}
<bean id="userAction" class="spring_hibernate.action.UserAction">
<property name="userService" ref="userService">property>
bean>
<struts>
<package name="default" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*global-allowed-methods>
<action name="userAction_*" class="userAction" method="{1}">
<result>/success.jspresult>
action>
package>
struts>