spring2.5.6+hibernae初级尝试【annotation】

问题:最近找工作需要

解决方案:

1、spring配置内容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" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com"/>
	<!-- 配置Hibernate支持 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="org.postgresql.Driver">
		</property>
		<property name="url" value="jdbc:postgresql://localhost:5432/sh">
		</property>
		<property name="username" value="postgres"></property>
		<property name="password" value="123456"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.PostgreSQLDialect
                </prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.model.User</value>
			</list>
		</property>
	</bean>
</beans>

 2、实体类:

package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "\"User\"")
public class User {
	private int id;
	private String name;
	private String password;
	private int age;

	@Id
	@GeneratedValue
	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;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

 3、相关dao类:

package com.dao;

import com.model.User;

public interface UserDAO {
	/**
	 * 用户存储
	 * 
	 * @param user
	 */
	public void insert(User user);

	/**
	 * 用户删除
	 * 
	 * @param user
	 */
	public void delete(User user);

	/**
	 * 用户更新
	 * 
	 * @param user
	 */
	public void update(User user);
}

实现类:
package com.dao.impl;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.stereotype.Component;

import com.dao.UserDAO;
import com.model.User;

@Component("UserDAOImpl")
public class UserDAOImpl implements UserDAO {
	private static final Log log = LogFactory.getLog(PlainHibernateDAO.class);
	private SessionFactory sessionFactory;

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void insert(User user) {
		log.debug("insert user instance");
		try {
			Session s = sessionFactory.openSession();
			s.beginTransaction();
			s.save(user);
			s.getTransaction().commit();
			log.debug("persist successful");
			log.info("persist successful===");
		} catch (RuntimeException re) {
			log.error("insert failed", re);
			log.info("insert failed===", re);
			throw re;
		}
	}

	@Override
	public void delete(User user) {
		log.debug("delete user instance");
		log.info("delete user instance===");
		try {
			sessionFactory.getCurrentSession().delete(user);
			log.debug("delete user success");
			log.info("delete user success===");
		} catch (RuntimeException e) {
			log.error("delete error", e);
			log.info("delete error===", e);
			throw e;
		}
	}

	@Override
	public void update(User user) {
		log.debug("update user instance");
		log.info("update user instance");
		try {
			sessionFactory.getCurrentSession().update(user);
			log.debug("update user success");
			log.info("update user success===");
		} catch (RuntimeException e) {
			log.debug("update user instance", e);
			log.info("update user instance", e);
			throw e;
		}
	}
}

 4、测试类

package com.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dao.impl.UserDAOImpl;
import com.model.User;

public class UserTest {
	ApplicationContext ctx = new ClassPathXmlApplicationContext(
			"beans.xml");

	@Test
	public void insertTest() {
		UserDAOImpl daoImpl = (UserDAOImpl) ctx.getBean("UserDAOImpl");
		User user = new User();
		user.setId(1);
		user.setName("zzz");
		user.setPassword("123456");
		user.setAge(25);
		
		daoImpl.insert(user);
	}
}

 其中可能有不必要的操作,望不要吐槽,小弟刚刚学习!

猜你喜欢

转载自wyzws.iteye.com/blog/1734533
今日推荐