整合hibernate和spring这样的文章已经很多了,下面我们来看看如何利用myeclipse的功能为整合提速咯 1.首先,创建工程,可以直接选创建J2EE web工程 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" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate./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的配置文件 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/nirvana?useUnicode=true</value> </property> <property name="username"> <value>dyerac</value> </property> <property name="password"> <value></value> </property> </bean> <bean id="sessoinFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="connection.characterEncoding">utf8</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingDirectoryLocations"> <list > <value>src/mapping</value> </list> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessoinFactory"/> </property> <property name="allowCreate"> <value>true</value> </property> </bean> <bean id="testDAO" class="mapping.TestDAO"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean> </beans> 最后的最后,开发一个测试类: 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); } } } |
|