Spring3与Hibernate进行集成(使用注解)

 

首先写POM.xml

 


	4.0.0

	com.gc
	spring
	0.0.1-SNAPSHOT
	jar

	spring
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			3.8.1
			test
		
		
			org.springframework
			spring-core
			3.2.7.RELEASE
		
		
			org.springframework
			spring-context
			3.2.7.RELEASE
		
		
			org.springframework
			spring-beans
			3.2.7.RELEASE
		
		
			org.springframework
			spring-orm
			3.2.7.RELEASE
		
		
			org.aspectj
			aspectjweaver
			1.6.8
		

		
			org.hibernate
			hibernate-core
			3.6.1.Final
		
		
		
	javassist
	javassist
	3.12.0.GA

            

		
			mysql
			mysql-connector-java
			5.1.26
		

		
			commons-dbcp
			commons-dbcp
			1.4
		
	

 

applicationContext.xml 配置SessionFactory

 



	
	

	
		
		
		
		
	

	
		
		
		
			
				org.hibernate.dialect.MySQL5Dialect
				true
				false
				update
			
		
		
	
	

 

附:hibernate.cfg.xml




	
		
		
		org.hibernate.dialect.MySQL5Dialect
		com.mysql.jdbc.Driver
		jdbc:mysql://localhost:3306/gc
		root
		root
		
		
		true 
		false 
		
		update
		
		thread
		
		
		
		
	

  

定义实体

将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"));
	}

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(框架@Hibernate,java,数据库)