create table `h3`.`stock` ( `stock_id` int(10) unsigned not null auto_increment, `stock_code` varchar(10) not null, `stock_name` varchar(20) not null, primary key (`stock_id`) using btree, unique key `uni_stock_name` (`stock_name`), unique key `uni_stock_id` (`stock_code`) using btree ) engine=innodb auto_increment=1 default charset=utf8;
package com.h3.stock.model; import java.io.serializable; public class stock implements serializable { private static final long serialversionuid = 1l; private long stockid; private string stockcode; private string stockname; //getter and setter methods... }
package com.h3.stock.bo; import com.h3.stock.model.stock; public interface stockbo { void save(stock stock); void update(stock stock); void delete(stock stock); stock findbystockcode(string stockcode); }
package com.h3.stock.bo.impl; import com.h3.stock.bo.stockbo; import com.h3.stock.dao.stockdao; import com.h3.stock.model.stock; public class stockboimpl implements stockbo{ stockdao stockdao; public void setstockdao(stockdao stockdao) { this.stockdao = stockdao; } public void save(stock stock){ stockdao.save(stock); } public void update(stock stock){ stockdao.update(stock); } public void delete(stock stock){ stockdao.delete(stock); } public stock findbystockcode(string stockcode){ return stockdao.findbystockcode(stockcode); } }
stock dao接口和实现,dao实现类扩展了 spring 的“hibernatedaosupport”,以使spring框架支持hibernate。 现在,你可以通过gethibernatetemplate()执行 hibernate 功能。
package com.h3.stock.dao; import com.h3.stock.model.stock; public interface stockdao { void save(stock stock); void update(stock stock); void delete(stock stock); stock findbystockcode(string stockcode); }
package com.h3.stock.dao.impl; import java.util.list; import org.springframework.orm.hibernate3.support.hibernatedaosupport; import com.h3.stock.dao.stockdao; import com.h3.stock.model.stock; public class stockdaoimpl extends hibernatedaosupport implements stockdao{ public void save(stock stock){ gethibernatetemplate().save(stock); } public void update(stock stock){ gethibernatetemplate().update(stock); } public void delete(stock stock){ gethibernatetemplate().delete(stock); } public stock findbystockcode(string stockcode){ list list = gethibernatetemplate().find( "from stock where stockcode=?",stockcode ); return (stock)list.get(0); } }
<?xml version="1.0"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.h3.stock.model.stock" table="stock" catalog="h3"> <id name="stockid" type="java.lang.long"> <column name="stock_id" /> <generator class="identity" /> </id> <property name="stockcode" type="string"> <column name="stock_code" length="10" not-null="true" unique="true" /> </property> <property name="stockname" type="string"> <column name="stock_name" length="20" not-null="true" unique="true" /> </property> </class> </hibernate-mapping>
database related….
创建一个属性文件(database.properties)数据库的详细信息,把它放在“resources/properties” 文件夹中。这是很好的做法,不同于数据库细节并将 spring bean 配置成不同的文件。
database.properties
jdbc.driverclassname=com.mysql.jdbc.driver jdbc.url=jdbc:mysql://localhost:3306/h3 jdbc.username=root jdbc.password=password
datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="location"> <value>properties/database.properties</value> </property> </bean> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="${jdbc.driverclassname}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> </beans>
hibernate 关联….
创建一个会话工厂 bean 配置文件(hibernate.xml),把它放入 “resources/database” 文件夹中。这个 localsessionfactorybean 中设置一个共享的 hibernate sessionfactory 在一个 spring 应用程序上下文。
hibernate.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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- hibernate session factory --> <bean id="sessionfactory" 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="hibernate.show_sql">true</prop> </props> </property> <property name="mappingresources"> <list> <value>/hibernate/stock.hbm.xml</value> </list> </property> </bean> </beans>
spring beans related….
创建一个bean配置文件(stock.xml)的bo和dao类,把它放入 “resources/spring” 文件夹中。依赖的 dao(stockdao)bean 注入到 bo(stockbo)bean; sessionfactory 的 bean 成stockdao。
stock.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- stock business object --> <bean id="stockbo" class="com.h3.stock.bo.impl.stockboimpl" > <property name="stockdao" ref="stockdao" /> </bean> <!-- stock data access object --> <bean id="stockdao" class="com.h3.stock.dao.impl.stockdaoimpl" > <property name="sessionfactory" ref="sessionfactory"></property> </bean> </beans>
beanlocations.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- database configuration --> <import resource="../database/datasource.xml"/> <import resource="../database/hibernate.xml"/> <!-- beans declaration --> <import resource="../beans/stock.xml"/> </beans>
package com.h3.common; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.h3.stock.bo.stockbo; import com.h3.stock.model.stock; public class app { public static void main( string[] args ) { applicationcontext appcontext = new classpathxmlapplicationcontext("spring/config/beanlocations.xml"); stockbo stockbo = (stockbo)appcontext.getbean("stockbo"); /** insert **/ stock stock = new stock(); stock.setstockcode("7668"); stock.setstockname("haio"); stockbo.save(stock); /** select **/ stock stock2 = stockbo.findbystockcode("7668"); system.out.println(stock2); /** update **/ stock2.setstockname("haio-1"); stockbo.update(stock2); /** delete **/ stockbo.delete(stock2); system.out.println("done"); } }
输出结果:
hibernate: insert into h3.stock (stock_code, stock_name) values (?, ?) hibernate: select stock0_.stock_id as stock1_0_, stock0_.stock_code as stock2_0_, stock0_.stock_name as stock3_0_ from h3.stock stock0_ where stock0_.stock_code=? stock [stockcode=7667, stockid=1, stockname=haio] hibernate: update h3.stock set stock_code=?, stock_name=? where stock_id=? done