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=11 default charset=utf8;
package com.h3.stock.model; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import static javax.persistence.generationtype.identity; import javax.persistence.id; import javax.persistence.table; import javax.persistence.uniqueconstraint; @entity @table(name = "stock", catalog = "h3", uniqueconstraints = { @uniqueconstraint(columnnames = "stock_name"), @uniqueconstraint(columnnames = "stock_code") }) public class stock implements java.io.serializable { private integer stockid; private string stockcode; private string stockname; public stock() { } public stock(string stockcode, string stockname) { this.stockcode = stockcode; this.stockname = stockname; } @id @generatedvalue(strategy = identity) @column(name = "stock_id", unique = true, nullable = false) public integer getstockid() { return this.stockid; } public void setstockid(integer stockid) { this.stockid = stockid; } @column(name = "stock_code", unique = true, nullable = false, length = 10) public string getstockcode() { return this.stockcode; } public void setstockcode(string stockcode) { this.stockcode = stockcode; } @column(name = "stock_name", unique = true, nullable = false, length = 20) public string getstockname() { return this.stockname; } public void setstockname(string stockname) { this.stockname = stockname; } @override public string tostring() { return "stock [stockcode=" + stockcode + ", stockid=" + stockid + ", stockname=" + stockname + "]"; } }
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 org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.h3.stock.bo.stockbo; import com.h3.stock.dao.stockdao; import com.h3.stock.model.stock; @service("stockbo") public class stockboimpl implements stockbo{ @autowired 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类是直接扩展“hibernatedaosupport“。但注释方式不可能做到这一点,因为没有办法从dao类会话到工厂bean自动装配。解决方法是创建一个自定义类(customhibernatedaosupport),并扩展了“hibernatedaosupport”和自动装配会话工厂,dao类扩展了这个类。
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.util; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.orm.hibernate3.support.hibernatedaosupport; public abstract class customhibernatedaosupport extends hibernatedaosupport { @autowired public void anymethodname(sessionfactory sessionfactory) { setsessionfactory(sessionfactory); } }
package com.h3.stock.dao.impl; import java.util.list; import org.springframework.stereotype.repository; import com.h3.stock.dao.stockdao; import com.h3.stock.model.stock; import com.h3.util.customhibernatedaosupport; @repository("stockdao") public class stockdaoimpl extends customhibernatedaosupport 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); } }
数据库关联….
创建一个属性文件(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 related….
创建一个会话工厂bean的配置文件(hibernate.xml),把它放到“resources/database”文件夹中。在注释必须使用annotationsessionfactorybean 来代替 localsessionfactorybean,并注明您的注解模型类在“annotatedclasses”属性,而不是'mappingresources'属性。
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.annotation.annotationsessionfactorybean"> <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="annotatedclasses"> <list> <value>com.h3.stock.model.stock</value> </list> </property> </bean> </beans>
beanlocations.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- database configuration --> <import resource="../database/datasource.xml"/> <import resource="../database/hibernate.xml"/> <!-- auto scan the components --> <context:component-scan base-package="com.h3.stock" /> </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=3, stockname=haio] hibernate: update h3.stock set stock_code=?, stock_name=? where stock_id=? hibernate: delete from h3.stock where stock_id=? done
所有的spring,hibernate相关类和配置文件都进行注解,它只是保留在xml文件中的数据库的详细信息。就个人而言,我不使用太多的注释功能,因为在某种程度上可能需要一些解决方法,像上面的“customhibernatedaosupport'扩展'hibernatedaosupport”。 在spring和hibernate的xml文件的成熟是一个更优的选择。