Hello everyone, I am Zhang Jiangnan from Shanghai. The IT atmosphere is very good. I have been living here for half a year. I feel very good, haha.
The following is the SSH2 annotation I have summarized. I feel that it is okay to get started, so I have come up with my hand. Please feel free to contact us. QQ: 4218380 thank you.
Simply add the spring. xml configuration file information.
<? XML version = "1.0" encoding = "UTF-8"?> <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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-con Text-2.5.xsd "> <! -- Annotation configuration, scan information under the com. niclo package --> <context: annotation-config/> <context: component-scan base-package = "com. niclo"/> <! -- Database attribute file --> <beanclass = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locations "> <value> classpath: JDBC. properties </value> </property> </bean> <! -- Database configuration information --> <bean id = "datasource" Destroy-method = "close" class = "org. apache. commons. DBCP. basicdatasource "> <property name =" driverclassname "value =" $ {JDBC. driver} "/> <property name =" url "value =" $ {JDBC. URL} "/> <property name =" username "value =" $ {JDBC. username} "/> <property name =" password "value =" $ {JDBC. password} "/> </bean> <! -- Sessionfactory configuration information, which must be dropped --> <bean id = "sessionfactory" Destroy-method = "Destroy" class = "org. springframework. orm. hibernate3.annotation. annotationsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" packagestoscan "value =" com. niclo. VO "> </property> <property name =" hibernateproperties "> <props> <prop key =" hibernate. dialect ">$ {JDBC. dialect} </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hibernate. hbm2ddl. auto "> none </prop> </props> </property> </bean> </beans>
A vo class can also be said to be po or DTO. A encapsulation class is just like this.
package com.niclo.vo;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="testVo")public class TestVo {public static final String ID = "id";public static final String TITLE = "title";public static final String TYPE = "type";public static final String NAME = "name";public static final String DESCRIPTION = "description";public static final String URL = "url";private Integer id;private String title;private String type;private String name;private String description; private String url;@Id@GeneratedValue(strategy=GenerationType.AUTO)public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}}
A Dao interface, user-defined weak and weak, is a bit garbled, but I believe it will be sharp.
Package COM. niclo. base; import Java. io. serializable; import Java. util. list; import Java. util. map; import Org. springframework. dao. dataaccessexception;/*** basedao interface, which can be used by any ssh/s2sh architecture <br> * @ author Northern Lights? * @ Qq 4218380 * @ version 0.8 $ dec 07,201 0 $19:40:40 $ **/public interface basedao {// void setconnection (string dbname ); /*** number of statistics rows ** @ Param clazz? The class parameter must be more efficient than the object query? �� Statistical instance */INT count (class clazz);/*** query ��? �� Object * persistently specified instance * @ Param clazz * loaded? * @ Param ID * serialized class ID */<t> T findbyid (class clazz, serializable ID);/*** save? �� Object * persistently specified instance * @ Param entity?? */Void save (Object entity);/*** save? �� Object. The difference between this method and the Save method is: Assume that hibernate has a savewithgeneratorid () method, * Is this method responsible for inserting data into the database? No? �� When a transaction occurs, the SAVE method will call this party. ��?? * The persist () method will not be called by the database for rollback? * Entity * specifies the persistent instance * @ Param entity?? * @ */Void persist (Object entity);/*** delete an object decisively without explanation * Delete? �� Object * @ Param entity * object to be deleted */void Delete (Object entity);/*** use namedquery to delete... ** @ Param namedquery * hql statement name * @ Param values * value column? * @ Return * @ throws dataaccessexception */Integer deletebynamedquery (string namedquery, object... values) throws dataaccessexception; /*** use hql to delete data * @ Param hql * hql statement * @ Param valuemap * map of stored values * @ return * Number of Deleted Values * @ throws dataaccessexception */Integer deletebyhqlquery (string hql, map <string, Object> valuemap) throws dataaccessexception;/*** this is called modifying an object without explanation * modify? �� Object * @ Param entity * object to be deleted */void Update (Object entity);/*** execute update using namedquery... ** @ Param namedquery * hql statement name * @ Param values * value column? * @ Return * @ throws dataaccessexception */Integer updatebynamedquery (string namedquery, object... values) throws dataaccessexception;/*** use hql to update data * @ Param hql * hql statement * @ Param valuemap * map of stored values * @ return * Number of updated values? * @ Throws dataaccessexception */Integer updatebyhqlquery (string hql, Map <string, Object> valuemap) throws dataaccessexception;/*** save or? Update? �� Object * persistently specified instance * @ Param entity?? */Void saveorupdate (Object entity);/*** use the get method to load the object * @ Param <t> * type information T * @ Param entityclass * to load? * @ Param ID * ID to be loaded * @ return * if not found? Returns null. * @ throws dataaccessexception */<t> t get (class <t> entityclass, serializable ID);/*** use namedquery to query all data, paging query * @ Param <t> * @ Param namedquery * @ Param beginresult * @ Param maxresult * @ Param values * @ return * @ throws dataaccessexception */<t> List <t> findbynamedquery (string namedquery, int beginresult, int maxresult, object... values) throws dataaccessexception;/*** query using namedquery Query all data by page * @ Param <t> * @ Param namedquery * @ Param cachename * if no cache name is specified? * @ Param values * @ return * @ throws dataaccessexception */<t> List <t> findbynamedquery (string namedquery, string cachename, object... values) throws dataaccessexception;/*** use namedquery to query a single object ** @ Param <t> * @ Param namedquery supports XXX. HBM. find the hql statement in the XML file, * but I put it in the namedqueryconstants constant class, so I directly use namedqueryconstants. XXXX is enough * @ Param values * @ return * @ throws dataaccessexception */<t> T getby Namedquery (string namedquery, object... values) throws dataaccessexception;/*** search for the object based on the condition and whether it is slow? * @ Param <t> * @ Param hql * @ Param valuemap * @ Param cachename * if no cache name is specified? * @ Return * @ throws dataaccessexception */<t> List <t> findbyhqlquery (string hql, Map <string, Object> valuemap, string cachename) throws dataaccessexception; /*** use hql to search for data from the database. It supports querying by page * @ Param hql * hql statement, hql statement?? * @ Param valuemap? �� Map, key is named parameter ��? * @ Param firstresult? �� Number of rows * @ Param maxresult? �� Number of rows * @ return * @ throws dataaccessexception */<t> List <t> findbyhqlquery (string hql, Map <string, Object> valuemap, int firstresult, int maxresult) throws dataaccessexception;/*** this method is from the database? �� Single object, supporting motion? Instantiate knot? * @ Param <t> * @ Param hql * @ Param valuemap * @ return * @ throws dataaccessexception */<t> T getbyhqlquery (string hql, Map <string, Object> valuemap) throws dataaccessexception;/*** this method is from the database? �� Single object, supporting motion? Instantiate knot? * @ Param <t> * @ Param hql * @ Param valuemap * @ return * @ throws dataaccessexception */<t> List <t> findbyexample (string examplename, object exampleentity, int firstresult, int maxresults );}
Daoimpl implementation class, sessionfactory is also in it, uses hibernatetemplate, only write one implementation method, because there are too many.
/*** The basedao class is the DaO base class, which can be inherited by the daoimpl class of any module? BR> * @ author Yu Xiao? * @ Qq 4218380 * @ version 0.1 $ may 11,201 1 $13:40:40 $ **/@ component ("Dao") public class basedaoimpl implements basedao {public hibernatetemplate; @ Resource (name = "sessionfactory") Public void setsessionfactory (sessionfactory) {hibernatetemplate = new hibernatetemplate (sessionfactory);} Public <t> T findbyid (class entityclass, serializable ID) {return (t) hibernatetemplate. get (entityclass, ID );}}
Service Interface. Define only one method.
Public interface baseservice {/*** number of statistics rows ** @ Param clazz * The parameter class must be more efficient than object query? �� Statistical instance */INT count (class clazz);/*** query ��? �� Object * persistently specified instance * @ Param clazz * loaded? * @ Param ID * serialized class ID */<t> T findbyid (class entityclass, serializable ID);/*** save? �� Object * persistently specified instance * @ Param entity? ? */Boolean save (Object entity );}
Baseserviceimpl implementation class. It is very simple to call Dao here, that is, @ resource, but Dao is the same name as Dao in the component ("Dao") of basedaoimpl,
Similarly, if other classes want to reference this baseserviceimpl, you can use private @ resource baseserviceimpl service. This is fine. As for why is it configured in spring. XML, thank you.
@Component("service")public class BaseServiceImpl implements BaseService{private @Resource BaseDao dao;public int count(Class entity) {returndao.count(entity);}public <T> T findById(Class entityClass, Serializable id) {return dao.findById(entityClass, id);}public boolean delete(Object entity) {boolean flag = false;try {dao.delete(entity);flag = true;} catch (DataAccessException e) {throw e;}return flag;}}
Next, how does the struts2 action reference the service and how to use the struts2 annotation.
Package COM. niclo. action; import Java. util. list; import javax. annotation. resource; import Org. apache. struts2.convention. annotation. action; import Org. apache. struts2.convention. annotation. result; import COM. niclo. base. baseservice; import COM. niclo. VO. testvo; public class useraction {Private Static final string success = "(@. @)..? "; Private Static final string input =" (^. ^ )..! "; Private @ resource baseservice service; private list <testvo> list; @ action (value =" test ") Public void test () {testvo Vo = service. findbyid (testvo. class, 1); system. out. println (vo. getname () + "test .. ") ;}@ action (value =" searchaction ", Results = {@ result (name = success, location ="/result. JSP "), @ result (name = input, location ="/Lucene. JSP ")}) Public String searchaction () {// here, the service method should be written by yourself. // List = service. call the query page. For details, see the dome source code return success;} public list <testvo> getlist () {return list;} public void setlist (list <testvo> List) {This. list = List ;}}
Good Kun, today I went to Pujiang to blow a day of wind, the last fillmap help class, this class is dedicated to parsing hql drops.
Package COM. niclo. util; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Java. util. regEx. matcher; import Java. util. regEx. pattern; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; public final class fillmap {Private Static final log = logfactory. getlog (fillmap. class); Private fillmap () {}/ *** Based on the specified hql query conditions and? List filling map * @ Param factory * query hql condition * @ Param OBJ * value column? * @ Return * What is encapsulated into map? */Public static Map <string, Object> fillvalue (string hql, object... OBJ) {Map <string, Object> valuemap = new hashmap <string, Object> (4); pattern = pattern. compile ("[:] [a-zA-Z0-9] +"); matcher = pattern. matcher (hql); string findgroup = NULL; For (INT I = 0; I <obj. length; I ++) {If (matcher. find () {findgroup = matcher. group (); valuemap. put (findgroup. substring (1, findgroup. length (), OBJ [I]);} Pattern = NULL; matcher = NULL; If (log. isinfoenabled () {log.info ("〓 �? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? Get the hql is? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? 〓 ��? \ N "+ hql +" \ n fill the named parameter key/value is \ n "+ valuemap);} return valuemap;} @ suppresswarnings (" unchecked ") public static void main (string [] ARGs) {list = new arraylist (); list. add (0); list. add (-99); list. add (1); list. add (2); system. out. println (fillvalue ("abc =: ANC, dy =: DD, xx =: XX, YY =: YY", list. toarray ()));}}
I have uploaded my own examples to csdn and can download them directly. Examples include full-text Lucene search and WebService with SSH2 annotations.
Technology is the happiest thing to share and discuss, because only people communicate with each other is the happiest, and computers are just to make life better.