Spring+Hibernate小程序



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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值