Spring_hibernate整合初步 based in annotation

首先配置xml

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.chpn.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>

</property>
</bean>

建立实体类,加上annotation

@Entity
public class User {
int id;
String name;

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

}

在UserDaoImpl.java中写有关hibernate逻辑

@Component("user")
public class UserDAOImpl implements UserDAO {

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

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

@Override
public void save(User u) {
Session s = sessionFactory.openSession();
s.beginTransaction();
s.save(u);
s.getTransaction().commit();

// TODO Auto-generated catch block

System.out.println("user save");
}

}

引入hibernate相关jar包




    		    Spring_hibernate整合初步 based in annotation

至此Spring整合hibernate初步完成

更多相关文章
  • 置頂 struts2+hibernate+spring整合(annotation版)
    本博文使用struts2,hibernate,spring技术整合Web项目,同时分层封装代码,包含model层,DAO层,Service层,Action层. 在整合hibernate时使用annotation注释进行数据库的映射,整合spring时使用annotation进行IOC注入. 最后在D
  • Paper about Event Detection. [email protected]: gr [email protected]: 2014-03-15 [email protected]: [email protected] 看一些相关的论文. 1. <Efficient Visual Event Detection using Volum ...
  • Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both appro ...
  • spring與hibernate整合配置基于Annotation注解方式管理實務
     1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 1 <!-- jdbc连接信息 --> 2 <context:property-placeholder 3 location="classpath:io/shuqi/ ...
  • Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
    1.加入DataSourceTransactionManager的命名空间修改applicationContext.xml文件,增加如下内容:1234567891011121314<?xml version="1.0" encoding="UTF-8"? ...
  • Spring整合Hibernate:1、annotation方式管理SessionFactory
    1.在applicationContext.xml文件中初始化SessionFactory(annotation方式)12345678910111213141516171819202122232425262728<bean    class="org.springframework. ...
  • Annotation的方式是通过注解的方式把Struts2中的Action.Dao层的实现类.Service层的实现类交由Spring管理,不需要在配置文件中进行配置.但为了方便,事务的管理依然使用的是Schema的方式.如果有需要,可以参照4.3.2中的方式,[email protected] ...
  • http://www.cnblogs.com/fragranting/archive/2012/03/25/xml--jaxb.html http://exceptioneye.iteye.com/blog/1325
一周排行