Baseaction,basedao,baseservice

Source: Internet
Author: User

SSH's

Baseaction

A brief description of the role

1. General methods such as adding and deleting, getting tables, getting trees, etc.

2. Other action inheritance baseaction to achieve basic non-write code or write less code, so that other actions have baseaction methods, but also their own unique method

On the Code

@ParentPackage ("Default") @Namespace ("/") Public classBaseaction<t>extendsActionsupport {Private Static FinalLogger Logger = Logger.getlogger (baseaction.class); protected intpage = 1;//Current Page    protected introws = 10;//shows the number of records per page    protectedString sort;//Sort Fields    protectedString order = "ASC";//Asc/desc    protectedString Q;//Easyui Combo and its subclasses are used when filtering    protectedString ID;//PRIMARY Key    protectedString IDs;//primary key set, comma split    protectedT data;//data Model (same as foreground form name, name= "Data.xxx")    protectedBaseservicei<t> Service;//Business logic
/**
* GET Request
*
* @return
*/
Public HttpServletRequest Getrequest () {
return Servletactioncontext.getrequest ();
}

/**
* Get response
*
* @return
*/
Public HttpServletResponse GetResponse () {
return Servletactioncontext.getresponse ();
}

/**
* Get session
*
* @return
*/
Public HttpSession getsession () {
Return Servletactioncontext.getrequest (). GetSession ();
}
/**
* Save an object
*/
public void Save () {
JSON JSON = new JSON ();
if (data! = NULL) {
Service.save (data);
Json.setsuccess (TRUE);
Json.setmsg ("New success! ");
}
Writejson (JSON);
}

/**
* Update an Object
*/
public void Update () {

JSON JSON = new JSON ();
String reflectid = null;

try {
if (data! = NULL) {
Reflectid = (String) Fieldutils.readfield (data, "id", true);
}
}
catch (Illegalaccessexception e) {
E.printstacktrace ();
}
if (! Stringutils.isblank (Reflectid)) {
try {
T t = Service.getbyid (Reflectid);
Beanutils.copyproperties (data,t);
Service.update (t);
Json.setsuccess (TRUE);
Json.setmsg ("Update succeeded");

} catch (Exception e) {
Json.setsuccess (FALSE);
Json.setmsg ("update failed");
Logger.info (E.tostring ());
}
}
Writejson (JSON);
}

/**
* Delete an Object
*/
public void Delete () {
JSON JSON = new JSON ();
if (! Stringutils.isblank (ID)) {
T t = Service.getbyid (ID);
Service.delete (t);
Json.setsuccess (TRUE);
Json.setmsg ("Delete succeeded! ");
}
Writejson (JSON);
}
}

Write Baseactiong as generic so the foreground is written as data.xxx and can be automatically converted to data by the struts type

This is convenient for each operation to automatically match different bean classes.

Service Layer

Servicei interface defines different methods and methods

 Public Interface Baseservicei<t> {    /**     * Save an object     *      @param  o     *            Object      @return the ID     of the object */     Public Serializable Save (T o);}

There are too many ways to write an example.

Serviceimpl Implementation Class

@Service ("Baseservice") @Transactionalpublicclassimplements baseservicei< T> {    @Autowired    private basedaoi<t> Basedao;    @Override    public  Serializable Save (T o) {        return  basedao.save ( o);}    }

Concrete implementation method of injection Basedao

Here's Basedao.

Basedaoi interface Definition method of Basedao

 Public Interface Basedaoi<t> {    /**     * Save an object     *      @param  o     *       ID     ofobject @return Object*/public      Serializable Save (T o);}

Too many ways not to write

Basedaoimpl

@Repository ("Basedao") Public classBasedaoimpl<t>ImplementsBasedaoi<t>{@AutowiredPrivatesessionfactory sessionfactory; /*** Get the session of the current thing * *@returnorg.hibernate.Session*/     PublicSession getcurrentsession () {returnsessionfactory.getcurrentsession (); } @Override PublicSerializable Save (T o) {if(O! =NULL) {            returngetcurrentsession (). Save (O); }        return NULL; }}

Basedaoimpl
Need to inject sessionfactory (method too much no longer written)

In this way, all generic methods are written in generics, so that each inheriting class colleague has the base method of the parent class and has its own unique method. If a method is to be used repeatedly, it is written as a common method. A lot of reduced code

Baseaction,basedao,baseservice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.