采用编程式事务
1、getCurrentSession()与openSession()的区别?
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()
创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()
创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
1、创建包
├─client
├─manager
│ └─impl
├─model
└─util
2、编写po和hbm映射文件
User.java
package com.strongit.usermgr.model;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Log.java
package com.strongit.usermgr.model;
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;
}
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.strongit.usermgr.model">
<class name="User" table="t_user">
<id name="id">
<generator class="native" />
</id>
<property generated="never" lazy="false" name="name" />
</class>
</hibernate-mapping>
Log.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.strongit.usermgr.model">
<class name="Log" table="t_log">
<id name="id">
<generator class="native"/>
</id>
<property generated="never" lazy="false" name="type"/>
<property generated="never" lazy="false" name="detail"/>
<property generated="never" lazy="false" name="time"/>
</class>
</hibernate-mapping>
3、配置hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/spring_hibernate_1</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--
<property name="hibernate.current_session_context_class">jta</property>
-->
<mapping resource="com/strongit/usermgr/model/User.hbm.xml"/>
<mapping resource="com/strongit/usermgr/model/Log.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、创建数据库
启动mysql
net start mysql
执行以下代码
package com.strongit.usermgr.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
5、编写服务层代码
用户接口 UserManager.java
package com.strongit.usermgr.manager;
import com.strongit.usermgr.model.User;
public interface UserManager {
public void addUser(User user);
}
用户接口实现 UserManagerImpl.java
package com.strongit.usermgr.manager.impl;
import java.util.Date;
import org.hibernate.Session;
import com.strongit.usermgr.manager.LogManager;
import com.strongit.usermgr.manager.UserManager;
import com.strongit.usermgr.model.Log;
import com.strongit.usermgr.model.User;
import com.strongit.usermgr.util.HibernateUtils;
public class UserManagerImpl implements UserManager {
public void addUser(User user) {
Session session = null;
try {
// session = HibernateUtils.getSession();
session = HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(user);
Log log = new Log();
log.setType("安全日志");
log.setDetail("xxx进入系统");
log.setTime(new Date());
LogManager logManager = new LogManagerImpl();
logManager.addLog(log);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
// }finally {
// HibernateUtils.closeSession(session);
}
}
}
日志接口 LogManager.java
package com.strongit.usermgr.manager;
import com.strongit.usermgr.model.Log;
public interface LogManager {
public void addLog(Log log);
}
日志接口实现 LogManagerImpl.java
package com.strongit.usermgr.manager.impl;
import com.strongit.usermgr.manager.LogManager;
import com.strongit.usermgr.model.Log;
import com.strongit.usermgr.util.HibernateUtils;
public class LogManagerImpl implements LogManager {
public void addLog(Log log) {
HibernateUtils.getSessionFactory().getCurrentSession().save(log);
}
}
hibernate帮助类 HibernateUtils.java
package com.strongit.usermgr.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
6、测试类
package com.strongit.usermgr.client;
import com.strongit.usermgr.manager.UserManager;
import com.strongit.usermgr.manager.impl.UserManagerImpl;
import com.strongit.usermgr.model.User;
public class Client {
/**
* @Description 方法实现功能描述
* @param args
* @author lanjh 9:02:58 PM
* @return void
* @throws 抛出异常说明
*/
public static void main(String[] args) {
User user = new User();
user.setName("lanjh");
UserManager userManager = new UserManagerImpl();
userManager.addUser(user);
}
}