Spring_hibernate整合初步 based in annotation
原创
©著作权归作者所有:来自51CTO博客作者wwwwcpcp的原创作品,请联系作者获取转载授权,否则将追究法律责任
首先配置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初步完成