Actual combat 3--Design Management module, INTEGRATED!!!

Source: Internet
Author: User

1. All DAO and Daoimpl modules are not used, plus @Deprecated

2. Establishment of Daosupport class and Daosupportimpl class

Daosupport.java

Package Cn.itcast.oa.base;import Java.util.list;public interface daosupport<t> {void save (T entity); void Delete ( Long ID); void update (T entity); T getById (Long ID); List<t> getbyids (long[] IDs); List<t> findAll ();}

Daosupportimpl.java

Package Cn.itcast.oa.base;import Java.lang.reflect.parameterizedtype;import Java.util.list;import Javax.annotation.resource;import Org.hibernate.session;import org.hibernate.SessionFactory;
@SuppressWarnings ("Unchecked") public class Daosupportimpl<t> implements daosupport<t> {@Resourceprivate Sessionfactory sessionfactory;private class<t> clazz = Null;public Daosupportimpl () {// Use the reflection technique to get the real type of t parameterizedtype pt = (parameterizedtype) this.getclass (). Getgenericsuperclass ();// Gets the parent class type of the current new object's generic This.clazz = (class<t>) pt.getactualtypearguments () [0]; System.out.println ("clazz===>" +clazz.getname ()); System.out.println ("clazz===>" +clazz.getsimplename ());} Protected Session getsession () {return sessionfactory.getcurrentsession ();} public void Save (T entity) {getsession (). Save (entity); public void update (T entity) {getsession (). Update (entity);} public void Delete (Long id) {Object obj = getById (ID); if (obj! = null) {getsession (). Delete (obj);}} Public T getById (Long ID) {if (id = = NULL) return Null;elsereturn (T) getsession (). Get (Clazz, id);} Public list<t> getbyids (long[] IDs) {return getsession (). CreateQuery (//"from" + clazz.getsimplename () + "WHERE ID In (:IDS)//.setparameterlist ("IDs", IDs). List ();} Public list<t> FindAll () {return getsession (). CreateQuery (//"from" + Clazz.getsimplename ()).//list ();}}

3. Extract the baseaction so that each action can focus on writing its own methods    

package Cn.itcast.oa.base;import Java.lang.reflect.parameterizedtype;import Javax.annotation.resource;import Cn.itcast.oa.service.departmentservice;import Cn.itcast.oa.service.RoleService; Import Cn.itcast.oa.service.userservice;import Com.opensymphony.xwork2.actionsupport;import Com.opensymphony.xwork2.modeldriven;public abstract class Baseaction<t> extends Actionsupport implements modeldriven<t>{protected T model;p ublic baseaction () {try {//by reflecting the real type of the live model Parameterizedtype pt = ( Parameterizedtype) This.getclass (). Getgenericsuperclass (); Class<t> clazz = (class<t>) pt.getactualtypearguments () [0];//creates an instance of model by Reflection model = Clazz.newinstance ();} catch (Exception e) {throw new RuntimeException ();}} Public T Getmodel () {return model;} The Declaration of the service instance ****************** @Resourceprotected roleservice roleservice; @Resourceprotected Departmentservice departmentservice; @Resourceprotected userservice userservice;} 

Integration Complete!!!

Steps to add a new module:

I. Preparing to create an action

1. Write a new action, Useraction.java,

1) extends Baseaction

2) @Controller @Scope ("prototype")

2. Define the method of action, write out the method name, function, return value

/** */public String list () throws Exception {return "list";} /** Delete */public String Delete () throws Exception {return "toList";} /** Add page */public String Addui () throws Exception {return "Saveui";} /** Add */public String Add () throws Exception {return "toList";} /** Modify Page */public String Editui () throws Exception {return "Saveui";} /** Modify */public String edit () throws Exception {return "toList";}        /** Initialize password 1234 **/public String Initpassword () throws Exception {return "toList";}    

3. Create the JSP page used (list.jsp, saveui.jsp)

4. struts.xml Configuration

<!--user management--><action name= "user_*" class= "useraction" method= "{1}" ><result name= "list" >/web-inf/ Jsp/useraction/list.jsp</result><result name= "Saveui" >/web-inf/jsp/useraction/saveui.jsp</result ><result name= "toList" type= "Redirectaction" >user_list?parentid=${parentid}</result></action >

  

Two. Prepare the service

1. Create interface Userservice.java, extends daosupport<user>

Package Cn.itcast.oa.service;import Cn.itcast.oa.base.daosupport;import Cn.itcast.oa.domain.user;public interface UserService  extends daosupport<user>{}

2. Create the implementation class Userserviceimpl.java, extends Daosupportimpl<user> implements UserService

3. Configuration: Write annotations @Service on Userserviceimpl @Transactional

Package Cn.itcast.oa.service.impl;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import Cn.itcast.oa.base.daosupportimpl;import Cn.itcast.oa.domain.user;import Cn.itcast.oa.service.UserService; @Service @transactionalpublic class Userserviceimpl extends daosupportimpl<user> implements userservice{}

4. Statement of service written in Baseaction

package Cn.itcast.oa.base;import Java.lang.reflect.parameterizedtype;import Javax.annotation.resource;import Cn.itcast.oa.service.departmentservice;import Cn.itcast.oa.service.RoleService; Import Cn.itcast.oa.service.userservice;import Com.opensymphony.xwork2.actionsupport;import Com.opensymphony.xwork2.modeldriven;public abstract class Baseaction<t> extends Actionsupport implements modeldriven<t>{protected T model;p ublic baseaction () {try {//by reflecting the real type of the live model Parameterizedtype pt = ( Parameterizedtype) This.getclass (). Getgenericsuperclass (); Class<t> clazz = (class<t>) pt.getactualtypearguments () [0];//creates an instance of model by Reflection model = Clazz.newinstance ();} catch (Exception e) {throw new RuntimeException ();}} Public T Getmodel () {return model;} The Declaration of the service instance ****************** @Resourceprotected roleservice roleservice; @Resourceprotected Departmentservice departmentservice; @Resourceprotected userservice userservice;} 

  

  

  

Actual combat 3--Design Management module, INTEGRATED!!!

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.