SH2011-1-22
/SH2011-1-22/src
com.zyl.dao
---------------------------//com/zyl/dao/ILogDao.java
package com.zyl.dao;
import com.zyl.usermsg.po.Log;
public interface ILogDao {
public void addLog(Log log);
}
---------------------------//src/com/zyl/dao/IUserDao.java
package com.zyl.dao;
import com.zyl.usermsg.po.User;
public interface IUserDao {
public void addUser(User user);
}
---------------------------//src/com/zyl/dao/LogDaoImpl.java
package com.zyl.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.zyl.usermsg.po.Log;
public class LogDaoImpl extends HibernateDaoSupport implements ILogDao {
public void addLog(Log log) {
this.getHibernateTemplate().save(log);
}
}
---------------------------//src/com/zyl/dao/UserDaoImpl.java
package com.zyl.dao;
import java.util.Date;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.zyl.usermsg.po.Log;
import com.zyl.usermsg.po.User;
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {
private ILogDao logDao;
public void setLogDao(ILogDao logDao) {
this.logDao = logDao;
}
public void addUser(User user) {
this.getHibernateTemplate().save(user);
Log log=new Log();
log.setDetail(user.getName()+"已进入");
log.setType("安全日志");
log.setTime(new Date());
logDao.addLog(log);
}
}
com.zyl.dao.Test
---------------------------//src/com/zyl/dao/Test/daoTest.java
package com.zyl.dao.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zyl.dao.IUserDao;
import com.zyl.dao.UserDaoImpl;
import com.zyl.usermsg.po.User;
public class daoTest {
public static void main(String[] args) {
User user = new User("zyl");
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"application*");
IUserDao userDao = (IUserDao) beanFactory.getBean("userDao");
userDao.addUser(user);
}
}
com.zyl.hiber.sessionfactory
---------------------------/HibernateSessionFactory.java
package com.zyl.hiber.sessionfactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
com.zyl.usermsg.po
---------------------------/com/zyl/usermsg/po/Log.java
package com.zyl.usermsg.po;
import java.util.Date;
public class Log {
private int id;
//操作日志、安全日志、事件日志
private String type;
private String detail;
private Date time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
---------------------------/com/zyl/usermsg/po/User.java
package com.zyl.usermsg.po;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public User() {
super();
}
public void setName(String name) {
this.name = name;
}
public User(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "User [name=" + name + "]";
}
}
---------------------------/com/zyl/usermsg/po/Log.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.zyl.usermsg.po">
<class name="Log" table="sh_log">
<id name="id">
<generator class="native"/>
</id>
<property name="type"></property>
<property name="detail"></property>
<property name="time"></property>
</class>
</hibernate-mapping>
---------------------------//com/zyl/usermsg/po/User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.zyl.usermsg.po">
<class name="User" table="sh_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
---------------------------//src/applicationContext-beans.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: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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="logDao" class="com.zyl.dao.LogDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userDao" class="com.zyl.dao.UserDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="logDao">
<ref bean="logDao" />
</property>
</bean>
</beans>
---------------------------//src/applicationContext-common.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: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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置事务管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="txAdvice" 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="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置哪些类参与事务 -->
<aop:config>
<aop:pointcut id="allDaoMethod" expression="execution(* com.zyl.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod"/>
</aop:config>
</beans>
---------------------------//src/hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="connection.url">jdbc:db2://192.168.25.230:50000/JSAMPLE</property>
<property name="connection.username">zyl</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="myeclipse.connection.profile">zyl</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/zyl/usermsg/po/Log.hbm.xml"/>
<mapping resource="com/zyl/usermsg/po/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
---------------------------//src/log4j.properties
log4j.rootLogger=ERROR,A1
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.TTCCLayout
---------------------------------------------
SH2011-1-22
/SH2011-1-22/src
com.zyl.dao
/SH2011-1-22/src/com/zyl/dao/ILogDao.java
/SH2011-1-22/src/com/zyl/dao/IUserDao.java
/SH2011-1-22/src/com/zyl/dao/LogDaoImpl.java
/SH2011-1-22/src/com/zyl/dao/UserDaoImpl.java
com.zyl.dao.Test
/SH2011-1-22/src/com/zyl/dao/Test/daoTest.java
com.zyl.hiber.sessionfactory
/SH2011-1-22/src/com/zyl/hiber/sessionfactory/HibernateSessionFactory.java
com.zyl.usermsg.po
/SH2011-1-22/src/com/zyl/usermsg/po/Log.java
/SH2011-1-22/src/com/zyl/usermsg/po/User.java
/SH2011-1-22/src/com/zyl/usermsg/po/Log.hbm.xml
/SH2011-1-22/src/com/zyl/usermsg/po/User.hbm.xml
/SH2011-1-22/src/applicationContext-beans.xml
/SH2011-1-22/src/applicationContext-common.xml
/SH2011-1-22/src/hibernate.cfg.xml
/SH2011-1-22/src/log4j.properties
/SH2011-1-22/WebRoot
---------------------------------------------
sh_log
pk ID INTEGER 4 否
"TYPE" VARCHAR 255 是
DETAIL VARCHAR 255 是
TIME TIMESTAMP 10 是
sh_user
pk ID INTEGER 4 否
"NAME" VARCHAR 255 是
---------------------------------------------纯hibernate
-----------------------com\zyl\hibernate\SessionFactory
-----------------------com.zyl.usermgr.client
package com.zyl.usermgr.client;
import com.zyl.usermgr.manager.UserManager;
import com.zyl.usermgr.manager.UserManagerImpl;
import com.zyl.usermgr.model.User;
public class Client {
public static void main(String[] args) {
User user=new User("123");
UserManager userManager=new UserManagerImpl();
userManager.addUser(user);
}
}
-----------------------/SH/src/com/zyl/usermgr/client/Client.java
com.zyl.usermgr.manager
-----------------------/SH/src/com/zyl/usermgr/manager/LogManager.java
-----------------------/com/zyl/usermgr/manager/LogManagerImpl.java
package com.zyl.usermgr.manager;
import org.hibernate.Session;
import com.zyl.hibernate.SessionFactory.HibernateSessionFactory;
import com.zyl.usermgr.model.Log;
public class LogManagerImpl implements LogManager {
public void addLog(Log log) {
Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
// Transaction trans =session.beginTransaction();
session.save(log);
// trans.commit();
//session.close();
}
}
-----------------------/com/zyl/usermgr/manager/UserManager.java
-----------------------/com/zyl/usermgr/manager/UserManagerImpl.java
package com.zyl.usermgr.manager;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zyl.hibernate.SessionFactory.HibernateSessionFactory;
import com.zyl.usermgr.model.Log;
import com.zyl.usermgr.model.User;
public class UserManagerImpl implements UserManager {
public void addUser(User user) {
Session session=null;
//Session session=HibernateSessionFactory.getSession();
try {
session=HibernateSessionFactory.getSessionFactory().getCurrentSession();
Transaction trans =session.beginTransaction();
session.save(user);
Log log =new Log();
log.setType("安全日志");
log.setDetail("zyl进入");
log.setTime(new Date());
LogManager logManager=new LogManagerImpl();
logManager.addLog(log);
trans.commit();
// session.close();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
}
}
com.zyl.usermgr.model模型和上边一样
-----------------------/com/zyl/usermgr/model/Log.java
-----------------------/com/zyl/usermgr/model/User.java
-----------------------/com/zyl/usermgr/model/Log.hbm.xml
-----------------------/com/zyl/usermgr/model/User.hbm.xml
-----------------------util
package util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ToDDL {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration configure = new Configuration();
configure.configure("hibernate.cfg.xml");
SchemaExport sc = new SchemaExport(configure);
sc.create(true,true);
}
}
-----------------------/SH/src/hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="connection.url">
jdbc:db2://192.168.25.230:50000/JSAMPLE
</property>
<property name="connection.username">zyl</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
com.ibm.db2.jcc.DB2Driver
</property>
<property name="myeclipse.connection.profile">zyl</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/zyl/usermgr/model/User.hbm.xml" />
<mapping resource="com/zyl/usermgr/model/Log.hbm.xml" />
</session-factory>
</hibernate-configuration>
分享到:
相关推荐
Struts、Spring 和 Hibernate 是Java Web开发中的三大框架,它们结合使用可以构建高效、模块化的应用程序,特别是对于处理用户登录和文件上传等常见功能。在这个项目中,"struts+spring+hibernate(mysql)用户登录及...
SSH(Struts+Spring+Hibernate)是一个经典的Java Web开发框架,用于构建高效、可扩展的企业级应用程序。这个教务管理信息系统是基于SSH框架实现的,提供了完整的源代码,可以直接运行,便于开发者学习和参考。 **...
Flex+Spring+Hibernate示例是一种常见的企业级应用架构,它结合了Adobe Flex前端技术、Spring后端框架和Hibernate持久层框架,以构建高效、灵活且易于维护的Web应用程序。在这个示例中,开发者可能已经展示了如何...
【纯净的Spring+Hibernate+MySQL】项目是一个典型的Java Web应用示例,它将Spring MVC、Hibernate ORM框架与MySQL数据库相结合,以实现用户登录、注册等基础功能。在本项目中,Spring MVC作为控制层,负责处理HTTP...
项目中已经搭建好了SSH框架,没有实现太得杂的功能,大家可以看一下struts-config.xml,现程序只实现了图书的新增功能!!大家可以在这个基础上进行完善,希望能给大家带来帮助--另外整个源程序超过了15M,不能够上传...
12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...
Struts、Spring 和 Hibernate 是Java Web开发中的三大框架,它们的组合通常被称为SSH(Struts、Spring、Hibernate)。这个“struts+spring+hibernate开发 注册小实例”是为初学者设计的一个教程,目的是帮助他们快速...
SSH(Struts+Spring+Hibernate)是Java Web开发中一种常见的技术栈,它将MVC设计模式、依赖注入和持久层框架集成为一个整体,大大提高了开发效率和代码的可维护性。下面我们将深入探讨SSH框架的各个组成部分及其结合...
整合Spring和Hibernate可以使应用程序更易于管理,因为Spring可以控制Hibernate的生命周期,并提供事务管理。 描述中提到的是一个"hello world"级别的示例,这意味着这个项目可能包含了一个基本的配置和简单的数据...
Spring 是一个全面的后端应用程序框架,而 Hibernate 是一个对象关系映射(ORM)工具,用于简化数据库操作。这个"spring+hibernate框架demo"提供了一个整合这两个框架的小项目,便于初学者理解和学习。 Spring 框架...
根据标题、描述和部分内容,知识点覆盖范围主要集中在Java Web开发领域,并且包括了JSP、Servlet、Ajax以及SSH框架(Struts 2、Spring、Hibernate)的技术整合和实战案例分析。 JSP(Java Server Pages)是一种动态...
SSH(Struts+Spring+Hibernate)是Java Web开发中常用的一种三层架构模式,它将应用程序分为表现层(Presentation Layer)、业务逻辑层(Business Logic Layer)和数据访问层(Data Access Layer),以实现清晰的...
在IT行业中,Web开发是至关重要的领域,而Spring、Struts2和Hibernate是Java Web开发中的三大主流框架,它们各自负责不同的职责,协同工作可以大大提高开发效率和代码质量。本项目是一个基于这三个框架的实例,旨在...
Spring MVC、Spring 和 Hibernate 是Java Web开发中的三大框架,它们分别负责不同的职责,共同构建了一个高效、灵活的后端架构。这个"springmvc+spring+hibernate集成jar包"包含了这三个框架集成所需的所有依赖,...
Struts、Spring 和 Hibernate 是Java开发中非常著名的三个开源框架,它们组合在一起通常被称为SSH框架。这个框架集合为开发者提供了一种高效、可扩展且结构化的解决方案,用于构建企业级的Web应用程序。下面将详细...
12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...
在本项目"spring4+springmvc4+hibernate4 整合"中,我们将探讨这三者的集成过程,以及它们如何协同工作来构建高效、可维护的Web应用程序。 **Spring Framework 4** Spring 是一个全面的Java应用框架,提供依赖注入...