在myeclipse下整合spring和hibernate1
来源:百度文库 编辑:神马文学网 时间:2024/04/26 18:32:33
整合hibernate和spring这样的文章已经很多了,下面我们来看看如何利用myeclipse的功能为整合提速咯
1.首先,创建工程,可以直接选创建J2EE web工程
(这....就不用贴图了吧)
2.导入spring, 选择myeclipse的add spring capabilities,注意把copy .....打勾(注,如果想要在spring的配置文件中配置hibernate的话, 一定要先导入spring)
3. 导入hibernate, 选择myeclipse的add hibernatecapabilities,注意把copy .....打勾
这时,myeclipse检测到已有spring,会问如何处理hibernate配置信息, 这里, 我们选择把hibernate的配置信息写在spring的配置信息中
接着,既然选择把在spring配置文件中配置hibernate信息,就需要设置hibernate的sessionfactory在配置文件中的bean id, 这里, 就设置为sessionFactory
然后要配置sessionFactory对应的数据源,注,数据源对应的bean id也需要设置,可以简单设置为dataSource
就不贴图咯
最后,选择sessionfactory对于的实现类,可以就用spring提供的LocalSessionFactory
这样, 我们就在项目中添加了spring和hibernate并将他们给予整合咯
3.应用:
配置好了环境,我们当然还得应用咯.下面给出我的代码
首先创建pojo和对应的hbm.xml
package mapping;
public class Test {
public Test() {
super ();
// TODO Auto-generated constructor stub
}
private int id;
private String name;
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;
}
}
xml version="1.0" ?>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping default-lazy ="false" auto-import ="true" package ="mapping" >
< class table ="test" name ="Test" >
< id name ="id" column ="test_id" type ="int" >
< generator class ="native" > generator >
id >
< property name ="name" type ="string" column ="name" > property >
class >
hibernate-mapping >
然后开发对应的DAO操作pojo, 因为我比较懒,所以直接使用HibernateTemplate进行操作
package mapping;
import java.util.List;
import org.hibernate.Criteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class TestDAO {
private HibernateTemplate hibernateTemplate;
public TestDAO() {
super();
// TODO Auto-generated constructor stub
}
public Test getTest(String name) throws Exception {
Test t=new Test();
t.setName(name);
List list = this.getHibernateTemplate().findByExample(t);
if (list.isEmpty())
throw new Exception("No Such Record");
else
return (Test) list.get(0);
}
public void addTest(String name) {
Test test = new Test();
test.setName(name);
this.getHibernateTemplate().save(test);
}
public void updateTest(Test test){
this.getHibernateTemplate().update(test);
}
public void deleteTest(Test test){
this.getHibernateTemplate().delete(test);
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate ht) {
this.hibernateTemplate = ht;
}
}
相应的,还需要修改下spring的配置文件
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/nirvana?useUnicode=true
dyerac
org.hibernate.dialect.MySQLDialect
utf8
true
update
true
最后的最后,开发一个测试类:
import mapping.Test;
import mapping.TestDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Tester {
public static void main(String args[]) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"src/applicationContext.xml");
TestDAO test = (TestDAO) ctx.getBean("testDAO");
//test.addTest("dyerac");
try {
Test t = test.getTest("bsbs");
System.err.println(t.getName());
//t.setName("bsbs");
//test.updateTest(t);
//test.deleteTest(t);
} catch (Exception e) {
System.err.println(e);
}
}
}
1.首先,创建工程,可以直接选创建J2EE web工程
(这....就不用贴图了吧)
2.导入spring, 选择myeclipse的add spring capabilities,注意把copy .....打勾(注,如果想要在spring的配置文件中配置hibernate的话, 一定要先导入spring)
3. 导入hibernate, 选择myeclipse的add hibernatecapabilities,注意把copy .....打勾
这时,myeclipse检测到已有spring,会问如何处理hibernate配置信息, 这里, 我们选择把hibernate的配置信息写在spring的配置信息中
接着,既然选择把在spring配置文件中配置hibernate信息,就需要设置hibernate的sessionfactory在配置文件中的bean id, 这里, 就设置为sessionFactory
然后要配置sessionFactory对应的数据源,注,数据源对应的bean id也需要设置,可以简单设置为dataSource
就不贴图咯
最后,选择sessionfactory对于的实现类,可以就用spring提供的LocalSessionFactory
这样, 我们就在项目中添加了spring和hibernate并将他们给予整合咯
3.应用:
配置好了环境,我们当然还得应用咯.下面给出我的代码
首先创建pojo和对应的hbm.xml
package mapping;
public class Test {
public Test() {
super ();
// TODO Auto-generated constructor stub
}
private int id;
private String name;
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;
}
}
xml version="1.0" ?>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping default-lazy ="false" auto-import ="true" package ="mapping" >
< class table ="test" name ="Test" >
< id name ="id" column ="test_id" type ="int" >
< generator class ="native" > generator >
id >
< property name ="name" type ="string" column ="name" > property >
class >
hibernate-mapping >
然后开发对应的DAO操作pojo, 因为我比较懒,所以直接使用HibernateTemplate进行操作
package mapping;
import java.util.List;
import org.hibernate.Criteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class TestDAO {
private HibernateTemplate hibernateTemplate;
public TestDAO() {
super();
// TODO Auto-generated constructor stub
}
public Test getTest(String name) throws Exception {
Test t=new Test();
t.setName(name);
List list = this.getHibernateTemplate().findByExample(t);
if (list.isEmpty())
throw new Exception("No Such Record");
else
return (Test) list.get(0);
}
public void addTest(String name) {
Test test = new Test();
test.setName(name);
this.getHibernateTemplate().save(test);
}
public void updateTest(Test test){
this.getHibernateTemplate().update(test);
}
public void deleteTest(Test test){
this.getHibernateTemplate().delete(test);
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate ht) {
this.hibernateTemplate = ht;
}
}
相应的,还需要修改下spring的配置文件
最后的最后,开发一个测试类:
import mapping.Test;
import mapping.TestDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Tester {
public static void main(String args[]) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"src/applicationContext.xml");
TestDAO test = (TestDAO) ctx.getBean("testDAO");
//test.addTest("dyerac");
try {
Test t = test.getTest("bsbs");
System.err.println(t.getName());
//t.setName("bsbs");
//test.updateTest(t);
//test.deleteTest(t);
} catch (Exception e) {
System.err.println(e);
}
}
}
在myeclipse下整合spring和hibernate1
myeclipse整合spring和hibernate
myeclipse整合hibernate和spring
MyEclipse Spring Hibernate整合开发 - liuxinglanyue - JavaEye技术网站
spring和ibatis的整合
在Spring 2中整合DWR 2
spring和ibatis的整合1
spring和ibatis的整合2
MyEclipse 整合ssh 冲突包
在eclipse MyEclipse下配置建立J2EE工程-
转贴:在MyEclipse下安装properties edit插件
winxp环境下整合tomcat5和iis5
spring与Ajax的整合
Struts Spring Hibernate整合笔记
Struts Spring Hibernate整合笔记
MyEclipse下开发JSF (4)
Spring 和struts 整合的三种方式 - 曹映辉的专栏 - CSDN博客
在eclipse MyEclipse下配置建立J2EE工程--黄晔博客
在eclipse MyEclipse下配置建立J2EE工程--黄晔博客
MyEclipse开发SSH(Struts+Spring+Hibernate)入门
MyEclipse开发SSH(Struts+Spring+Hibernate)入门q
MyEclipse开发SSH(Struts+Spring+Hibernate)入门vvv
Weblogic在MyEclipse的配置
Log4j 的配置和在 Spring 中的使用