Flex + J2EE instance (cairngorm + blazeds + hibernate + spring) Part3---- Add Spring & hibernate
1. Add spring, Hibernate common jar under WEB-INF \ Lib, including MySQL database connection driver
2. Create a database in the MySQL database and add Test Data
The statement is as follows:
Drop table if exists 'user ';
Create Table 'user '(
'Id' int (11) not null auto_increment,
'Username' varchar (11) not null default '0 ',
'Password' varchar (11) not null default '0 ',
'Age' int (11) default '0 ',
'Email 'varchar (50) default null,
Primary Key ('id ')
) Engine = InnoDB default charset = utf8 row_format = redundant;
Insert into 'user' values (1, 'lww ', '000000', 22, 'lww @ QQ.com ');
Insert into 'user' values (2, 'xyp', '000000', 22, 'xyp @ 136.com ');
3. Create the following files in SRC
Hibernatedao. javapackage COM. core. hibernate; import Java. io. serializable; import Java. util. list; import Org. hibernate. criteria; import Org. hibernate. query; import Org. hibernate. sqlquery; import Org. hibernate. criterion. criterion; import Org. springframework. orm. hibernate3.support. hibernatedaosupport; import Org. springframework. stereotype. repository; import COM. core. utils. reflectutils;/*** extended hibernateda Osupport generic base class * @ Param <t> entity type */@ repositorypublic class hibernatedao <t> extends hibernatedaosupport {protected class <t> entityclass; /*** use the reflection mechanism in the constructor to obtain the specific class of parameter T */Public hibernatedao () {entityclass = reflectutils. getclassgenrictype (getclass ());} /*** get Object Based on Object Class and ID * @ Param clazz object class * @ Param id Primary Key ID */Public <x> X get (final class <x> clazz, final serializable ID) {return (x) getsession (). get (Clazz, ID);}/*** get object by ID * @ Param id Primary Key ID */Public t get (serializable ID) {return get (entityclass, ID );} /*** Delete object ** @ Param entity class */Public void Delete (final object entity) {getsession (). delete (entity);}/*** Delete object by ID * @ Param id Primary Key ID */Public void Delete (final serializable ID) {Delete (get (ID ));} /*** Delete Object Based on Object Class and ID * @ Param clazz object class * @ Param id Primary Key ID */Public void Delete (final C Lass clazz, final serializable ID) {Delete (get (clazz, ID ));} /*** save object ** @ Param entity the object saved */Public void save (final object entity) {getsession (). saveorupdate (entity );} /*** get all data * @ Param entityclass parameter t reflection type */Public <x> List <x> getall (final class <x> entityclass) {return createcriteria (entityclass ). list ();}/***** retrieve all data */public list <t> getall () {return query () ;}/ ***** retrieve data based on conditions * @ Param criterions: criterion */public list <t> query (final criterion... criterions) {return createcriteria (criterions ). list ();}/*** hql query statement * @ Param hql query statement that complies with the hql syntax * @ Param values: variable number of condition values, bind */public query createquery (final string hql, final object... values) {query = getsession (). createquery (hql); Int J = values. length; For (INT I = 0; I <j; I ++) query. setparameter (I, values [I ]); Return query;}/*** SQL query * @ Param SQL: a query statement that complies with the SQL syntax * @ Param values: a variable number of condition values, bind */Public sqlquery createsqlquery (final string SQL, final object... values) {sqlquery query = getsession (). createsqlquery (SQL); If (values! = NULL) {for (INT I = 0; I <values. length; I ++) {query. setparameter (I, values [I]) ;}} return query ;}/ *** create a query object based on the Type * @ Param clazz type */Public criteria createcriteria (finalclass clazz) {return getsession (). createcriteria (clazz);}/*** objectization query * @ Param entityclass parameter t reflection type * @ Param criterions a variable number of criterion */Public criteria createcriteria (finalclass clazz, final criterion... criterions) {C Riteria criteria = getsession (). createcriteria (clazz); For (criterion C: criterions) {criteria. add (c);} return criteria;}/*** objectization query * @ Param criterions criterion with variable quantity */Public criteria createcriteria (final criterion... criterions) {return createcriteria (entityclass, criterions) ;}####################### reflectutils. java package COM. core. utils; import Java. lang. reflect. parameterizedtype; I Mport Java. lang. reflect. type;/*** reflection tool class */public class reflectutils {/*** obtain the parameter type of the super class, take the first parameter type * @ Param <t> type parameter * @ Param clazz super class */public static <t> class <t> getclassgenrictype (finalclass clazz) {return getclassgenrictype (clazz, 0 );} /*** obtain the super class parameter type based on the Index * @ Param clazz superclass type * @ Param Index */public static class getclassgenrictype (finalclass clazz, final int index) {type gentype = clazz. Getgenericsuperclass (); If (! (Gentype instanceof parameterizedtype) {return object. class;} type [] Params = (parameterizedtype) gentype ). getactualtypearguments (); If (index> = Params. length | index <0) {return object. class;} If (! (Params [Index] instanceofclass) {return object. class ;}return (class) Params [Index] ;}} ######################## administrator. java package COM. PKMs. entity; 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;/*** administrator en Tity. @ author myeclipse persistence tools */@ entity @ table (name = "Administrator", catalog = "PKMs") public class administrator implements Java. io. serializable {// fields private integer ID; private string loginname; private string password; // Constructors/** default constructor */Public Administrator () {}/** full constructor */Public Administrator (string loginname, string password) {This. lo Ginname = loginname; this. password = password;} // property accessors @ ID @ generatedvalue (Strategy = identity) @ column (name = "ID", unique = true, nullable = false) Public integer GETID () {return this. ID;} public void setid (integer ID) {This. id = ID;} @ column (name = "loginname", length = 11) Public String getloginname () {return this. loginname;} public void setloginname (string loginname) {th Is. loginname = loginname;} @ column (name = "password", length = 11) Public String GetPassword () {return this. password;} public void setpassword (string password) {This. password = PASSWORD ;}########### administratordao. java package COM. PKMs. dao; import Java. util. list; import Org. hibernate. query; import Org. hibernate. session; import Org. hibernate. sessionfactory; import Org. springframework. beans. fa Ctory. annotation. autowired; import Org. springframework. flex. remoting. remotingdestination; import Org. springframework. stereotype. repository; import COM. core. hibernate. hibernatedao; import COM. PKMs. entity. administrator; @ suppresswarnings ("unchecked") @ repository ("administratordao") public class administratordao extends hibernatedao <administrator >{@ autowired private sessionfactory; Public Administratordao () {super (); // system. out. println ("administratordao init" + sessionfactory = NULL);} public list getbyname (string loginname) {try {session = super. getsession (); string SQL = "select U. username, U. password, U. age, U. email from user u where u. username = '"+ loginname +"' "; query = session. createsqlquery (SQL); List list = query. list (); If (list! = NULL & list. size ()> 0) {system. out. println (list. get (0);} return list;} catch (exception e) {e. printstacktrace (); return NULL ;}}} ############################ administratorservice. java package COM. PKMs. service; import Java. util. list; import Org. springframework. beans. factory. annotation. autowired; import Org. springframework. flex. remoting. remotingdestination; import Org. springframework. flex. remoting. remotinginclude; import Org. springframework. stereotype. service; import COM. PKMs. dao. administratordao; @ remotingdestination (channels = {"My-AMF", "My-secure-AMF"}) @ Service ("administratorservice") public class administratorservice {@ autowired private administratordao; @ remotinginclude public list firstapp (string info) {list = administratordao. getbyname (Info); system. out. println (Info + "" + list); return list; // return to flex }}
:
Http://download.csdn.net/detail/kkkloveyou/4079410