首先写POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gc</groupId> <artifactId>spring</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.1.Final</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.0.GA</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> </dependencies> </project>
applicationContext.xml 配置SessionFactory
<?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: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/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"> <context:component-scan base-package="com.gc.spring"/> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/gc" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 引入外部的hibernate配置 <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> --> <property name="dataSource" ref="myDataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="packagesToScan" value="com.gc.spring" /> </bean> </beans>
附:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接相关 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/gc</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- hibernate特性配置 --> <property name="show_sql">true</property> <!-- 打印SQL --> <property name="format_sql">false</property> <!-- 格式化SQL --> <!-- <property name=""></property> create-drop:运行时,先创建表,运行结束,删除表 create:每次运行都先删除已有的表,再创建。测试时,可使用create模式 update:映射文件和表,如果不对应,则更新 validate:检查映射文件与数据库中的表是否对应,如果不对应,会报错。经常用这个配置,可提高安全性。 --> <property name="hbm2ddl.auto">update</property> <property name="hibernate.current_session_context_class">thread</property> <!-- 配置需要让hibernate进行管理的实体类 --> <mapping class="com.gc.spring.transaction.Log"/> <mapping class="com.gc.spring.transaction.User"/> </session-factory> </hibernate-configuration>
定义实体
将id属性提取出来,抽取到父类中,然后子类进行继承
package com.gc.spring.transaction; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; @MappedSuperclass public class BaseEntity { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } }
package com.gc.spring.transaction; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name="user") public class User extends BaseEntity { @Column(name="name",length=50) private String name; public User() { super(); } public User(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.gc.spring.transaction; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; @Entity public class Log extends BaseEntity { /** * 日志类别 * 操作日志: 调用哪些方法 * 安全日志: 登录 * 事件日志: 发送消息 */ @Column private String type; @Column private String detail; @Column private Date time; public Log() { } public Log(String type, String detail, Date time) { this.type = type; this.detail = detail; this.time = time; } 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; } }
定义DAO层
抽取一个父类,用来处理sessionFactory的注入问题
package com.gc.spring.transaction; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Component; @Component public class BaseDao extends HibernateDaoSupport { /** * 将sessionFactory设置到HibernateDaoSupport中 * @param sessionFactory */ @Autowired public void setSessionFactory0(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } }
package com.gc.spring.transaction; public interface UserManager{ public void addUser(User user); }
package com.gc.spring.transaction; import java.util.Date; import javax.annotation.Resource; import org.springframework.stereotype.Repository; @Repository("userManager") public class UserManagerImpl extends BaseDao implements UserManager { @Resource private LogManager logManager; public void addUser(User user) { this.getHibernateTemplate().save(user); logManager.addLog(new Log("操作日志","添加用户",new Date())); } }
package com.gc.spring.transaction; public interface LogManager { public void addLog(Log log); }
package com.gc.spring.transaction; import org.springframework.stereotype.Repository; @Repository("logManager") public class LogManagerImpl extends BaseDao implements LogManager { public void addLog(Log log) { this.getHibernateTemplate().save(log); } }
打完收工
public void testHib() { UserManager manager = context.getBean("userManager",UserManager.class); manager.addUser(new com.gc.spring.transaction.User("dsfa")); }