Inherit the hibernatedaosupport class from the business logic implementation class
Integrate using annotations (JSP file, Struts. XML is the same as the previous one)
JavaBean
@ Entity @ genericgenerator (name = "uid", strategy = "UUID") public class person {@ ID @ generatedvaluepublic int GETID () {return ID ;} public void setid (int id) {This. id = ID ;}@ columnpublic string getname () {return name;} public void setname (string name) {This. name = Name;} private int ID; private string name ;}
Business Logic
@ Component ("persondao") public class persondaoimpl extends hibernatedaosupport implements persondao {/* hibernatedaosupport class has two final Methods: setsessionfactory and sethibernatetemplate * which cannot be rewritten, but can be called, during the call, you can inject sessionfactory and hibernatetemplate into it. */@ Resource (name = "sessionfactory") Public void setsupersessionfactory (sessionfactory) {super. setsessionfactory (sessionfactory);} @ suppresswarnings ("Unchecked") public person checkuser (final int ID, final string username) {final string SQL = "from person P where P. ID =? And P. Name =? "; Return this.gethibernatetemplate(cmd.exe cute (New hibernatecallback <person> () {public person doinhibernate (Session arg0) throws hibernateexception, sqlexception {query = arg0.createquery (SQL ). setinteger (0, ID ). setstring (1, username); Return (person) query. list (). get (0 );}});}}
Service Layer
@ Component ("personservice") public class personserviceimpl implements personservice {private persondao; public person check (int id, string username) {return getpersondao (). checkuser (ID, username) ;}@ Resource (name = "persondao") Public void setpersondao (persondao) {This. persondao = persondao;} public persondao getpersondao () {return persondao ;}}
Action
@ Component ("loginaction") public class loginaction extends actionsupport implements modeldriven <person> {private personservice; private person; @ overridepublic string execute () throws exception {This. getpersonservice (). check (person. GETID (), person. getname (); Return success;} @ Resource (name = "personservice") Public void setpersonservice (personservice) {This. personservice = personservice;} public personservice getpersonservice () {return personservice;} public person GetModel () {If (person = NULL) person = new person (); Return person ;}}
Spring configuration file applicationcontext. xml
<! -- Automatically search for the com. zzia package and classes under the sub-package --> <context: component-scan base-package = "com. zzia"/> <! -- Configure the data source --> <bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource "> <property name =" user "value =" root "> </property> <property name =" password "value =" 123 "> </property> <property name = "driverclass" value = "com. mySQL. JDBC. driver "> </property> <property name =" jdbcurl "value =" JDBC: mysql: // localhost: 3306/test "> </property> </bean> <! -- Configure sessionfactory --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.annotation. annotationsessionfactorybean "> <property name =" datasource "ref =" datasource "> </property> <property name =" annotatedclasses "> <list> <value> COM. zzia. po. person </value> </List> </property> <property name = "hibernateproperties"> <props> <prop key = "hibernate. dialect "> Org. hibernate. dialect. mysqldialect </prop> <prop key = "current_session_context_class"> thread </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hibernate. format_ SQL "> true </prop> <prop key =" hibernate. hbm2ddl. auto "> Update </prop> </props> </property> </bean>