After consolidating the DAO layer, the next step is the integration of the service layer. Here by the way, a framework for the construction, preferably from the bottom to start building, as for the benefits, please in the actual combat slowly experience it.
First, the preparation of service
First you need to create a service interface
Package com.sky.ssm.service;
Import java.util.List;
Import Com.sky.ssm.po.ItemsCustom;
Import Com.sky.ssm.po.QueryItemsCustomVo;
/**
* Merchandise Management Service
* @author SK
*
/public interface Itemsservice {
//Product Query list
public list <ItemsCustom> finditemslist (Queryitemscustomvo queryitemscustomvo) throws Exception;
Then write the implementation class for the service
Package Com.sky.ssm.service.impl;
Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import Com.sky.ssm.mapper.ItemsCustomMapper;
Import Com.sky.ssm.po.ItemsCustom;
Import Com.sky.ssm.po.QueryItemsCustomVo;
Import Com.sky.ssm.service.ItemsService; /** * Commodity Management Implementation class * @author SK * * */public class Itemsserviceimpl implements Itemsservice {//annotation mapper/** because in Applicati Mapper is scanned in Oncontext-dao.xml * (<!--configuration mapper scanner--> <bean class= "Org.mybatis.spring.mapper.MapperScan Nerconfigurer "> <!--the package path scanned, if you need to scan multiple packets, use a half-width comma to separate--> <property name=" basepackage "value=" COM.SKY.SSM.M Apper "/> <property name=" sqlsessionfactorybeanname "value=" Sqlsessionfactory "/> </bean>) * Therefore, M
Apper already exists in the spring container, where it only needs to be acquired/@Autowired private itemscustommapper itemscustommapper; Public list<itemscustom> finditemslist (Queryitemscustomvo queryitemscustomvo) throws Exception {return Itemscu Stommapper.qUeryitemslist (QUERYITEMSCUSTOMVO);
}
}
Second, the service configuration
Applicationcontext-service.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd ">;! --Commodity Management Service--> <bean id= "Itemsservice" class= "Com.sky.ssm.service.impl.ItemsServiceImpl"/> </beans >
III. Configuration of transaction control
Applicationcontext-transaction.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd ">;! --using spring's declarative transaction control method--> <!--One, declares the transaction control of a spring transaction manager for the MyBatis operations database, Spring uses the JDBC transaction control class--> <bean id= "tr Ansactionmanager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager > <!--configuration data source The DataSource datasource in the reference Applicationcontext-dao.xml is configured in Applicationcontext-dao.xml--> <property name= "DataS Ource "ref=" DataSource/> </bean> <!--Two, configuration notification is the propagation characteristics of the configuration transaction--> <tx:advice id= "Txadvice" transaction- Manager= "TransactionManager" > <tx:attributes> <!--propagation behavior--> <!--REQUIRED: Support current transactions, create a new one if there is no current transaction SUPPORTS: Supports the current transaction and executes--> <tx:method name= "save*" propagation= "REQUIRED"/> <tx If there are currently no transactions : Method Name= "delete*" propagation= "REQUIRED"/> <tx:method name= "insert*" propagation= "REQUIRED"/> <tx:m Ethod name= "update*" propagation= "REQUIRED"/> <tx:method name= "find*" propagation= "SUPPORTS" read-only= "true"/ > <tx:method name= "get*" PRopagation= "SUPPORTS" read-only= "true"/> <tx:method name= "select*" propagation= "SUPPORTS" read-only= "true"/ > </tx:attributes> </tx:advice> <!--iii. configuring AOP--> <!--There are several concepts in AOP:-Aspects (Aspect): A modular focus, This attention-point implementation may otherwise crosscutting multiple objects. Transaction management is a good example of crosscutting concerns in Java application.
Aspect is implemented with spring's advisor or interceptor.
-Connection point (Joinpoint): an explicit point in the execution of a program, such as a call to a method or a specific exception being thrown. -Notification (Advice): The actions that the AOP framework performs at a particular connection point.
Various types of notifications include "around", "before", and "throws" notifications. -Pointcut (Pointcut): Specifies a collection of connection points that the notification will be thrown.
An AOP framework must allow developers to specify pointcuts, for example, using regular expressions.
So "<aop:aspect>" is actually defining the crosscutting logic, which is what to do at the connection point, and "<aop:advisor>" defines which connection points to apply <aop:aspect>.
The advantage of spring is that it allows multiple crosscutting logic (that is, <aop:aspect> definition) to be used multiple times to provide reusability. --> <aop:config> <!--(* com.sky.ssm.service.impl.*.* (..)) The meaning of several wildcard characters: the first *--the arbitrary return value type the second *--pass package Com.sky.ssm.service.impl The third *--pass packet Com.sky.ssm.service. Any method of any class under the Impl fourth ... --The wildcard method can have 0 or more parameters--> <aop:advisor advice-ref= "Txadvice pointcut=" Execution (* COM.SKY.SSM.SErvice.impl.*.* (..)) " /> </aop:config> </beans>
Project Structure Directory: