邢台网站改版定制,电子工程世界app下载,公司网站需要备案,就业网站建设在本演示中#xff0c;我们将使用spring / jpa2后端生成一个REST / CXF应用程序。 该演示演示了分钟项目的轨迹REST-CXF 。 演示2中的模型保持不变。 浓缩保持不变。 但是轨道改变了 添加的是2层#xff1a; 在JPA2之上具有弹簧集成的DAO层 具有JAX-RS批注的REST-CXF层… 在本演示中我们将使用spring / jpa2后端生成一个REST / CXF应用程序。 该演示演示了分钟项目的轨迹REST-CXF 。 演示2中的模型保持不变。 浓缩保持不变。 但是轨道改变了 添加的是2层 在JPA2之上具有弹簧集成的DAO层 具有JAX-RS批注的REST-CXF层 JPA2实体由JAXB注释注释。 所有这些都是为了在模型实体的顶部提供CRUD接口。 组态 这是配置TRANXY-JPA2-Spring-REST-CXF.xml !DOCTYPE root
generator-config xmlnshttp://minuteproject.sf.net/xsd/mp-config
xmlns:xshttp://www.w3.org/2001/XMLSchema-instance
xs:noNamespaceSchemaLocation../config/mp-config.xsdconfigurationconventionstarget-convention typeenable-updatable-code-feature //conventionsmodel nametranxy version1.0 package-rootnet.sf.mp.demodata-modeldriver namemysql version5.1.16 groupIdmysqlartifactIdmysql-connector-java/driverdataSourcedriverClassNameorg.gjt.mm.mysql.Driver/driverClassNameurljdbc:mysql://127.0.0.1:3306/tranxy/urlusernameroot/usernamepasswordmysql/password/dataSourceprimaryKeyPolicy oneGlobalfalse primaryKeyPolicyPattern nameautoincrementPattern/primaryKeyPolicyPattern/primaryKeyPolicy/data-modelbusiness-modelgeneration-conditioncondition typeexclude startsWithQUARTZ/condition/generation-conditionbusiness-package defaulttranxycondition typepackage startsWithtrans resulttranslation/condition/business-packageenrichmentconventions!-- manipulate the structure and entities BEFORE manipulating the entities --column-naming-convention typeapply-strip-column-name-suffixpattern-to-stripID /reference-naming-conventiontypeapply-referenced-alias-when-no-ambiguity is-to-plurializetrue //conventionsentity namelanguage_x_translatorfield namelanguage_id linkReferenceAliastranslating_language /field nameuser_id linkReferenceAliastranslator //entityentity nameLANGUAGE_X_SPEAKERfield nameLANGUAGE_ID linkToTargetEntityLANGUAGElinkToTargetFieldIDLANGUAGE linkReferenceAliasspoken_language /field nameuser_id linkReferenceAliasspeaker //entityentity nameAPPLICATIONfield nameTYPEproperty tagcheckconstraint aliasapplication_typeproperty nameOPENSOURCE/property nameCOPYRIGHT //property/field/entity/enrichment/business-model/modeltargetstarget refnameREST-CXF-BSLA namedefault fileNamemp-template-config-REST-CXF-Spring.xml outputdir-root../../DEV/latvianjug/tranxy/resttemplatedir-root../../template/framework/cxf/targettarget refnameBackendOnBsla namedefault fileNamemp-template-config-JPA2-bsla.xml outputdir-root../../DEV/latvianjug/tranxy/bslatemplatedir-root../../template/framework/bslaproperty nameadd-cache-implementation valueehcache/property/target target refnameJPA2 fileNamemp-template-config-JPA2.xmloutputdir-root../../DEV/latvianjug/tranxy/jpa templatedir-root../../template/framework/jpaproperty nameadd-querydsl value2.1.2/propertyproperty nameadd-jpa2-implementation valuehibernate/propertyproperty nameadd-cache-implementation valueehcache/propertyproperty nameadd-domain-specific-method valuetrue/propertyproperty nameadd-xmlbinding valuetrue/property property nameadd-xml-format valuelowercase-hyphen/property /targettarget refnameMavenMaster namemaven fileNamemp-template-config-maven.xml outputdir-root../../DEV/latvianjug/tranxytemplatedir-root../../template/framework/maven/targettarget refnameCACHE-LIB fileNamemp-template-config-CACHE-LIB.xmltemplatedir-root../../template/framework/cache/targettarget refnameLIB fileNamemp-template-config-bsla-LIB-features.xmltemplatedir-root../../template/framework/bsla
/targettarget refnameREST-LIB fileNamemp-template-config-REST-LIB.xml templatedir-root../../template/framework/rest/targettarget refnameSPRING-LIB fileNamemp-template-config-SPRING-LIB.xml templatedir-root../../template/framework/spring/target/targets/configuration
/generator-config 待办事项说明 代 在/ mywork / config中设置TRANXY-JPA2-Spring-REST-CXF.xml 跑 model-generation.cmd TRANXY-JPA2-Spring-REST-CXF.xml 输出进入/ dev / latvianjug / tranxy 产生的伪像 具有3个模块的Maven项目结构 JPA2层 Spring DAO层 CXF层 在演示1和演示2中已经访问了JPA2层。 Spring DAO层 它由每个实体一个的交易服务组成 JPA2之上的CRUD DAO层该层称为BSLA基本Spring层体系结构。 每个实体都会生成两个接口和实现 翻译实体示例 DAO接口 /*** Copyright (c) minuteproject, minuteprojectgmail.com* All rights reserved.* * Licensed under the Apache License, Version 2.0 (the License)* you may not use this file except in compliance with the License.* You may obtain a copy of the License at* * http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* * More information on minuteproject:* twitter minuteproject* wiki http://minuteproject.wikispaces.com * blog http://minuteproject.blogspot.net*
*/
/*** template reference : * - name : BslaDaoInterfaceUML* - file name : BslaDaoInterfaceUML.vm
*/
package net.sf.mp.demo.tranxy.dao.face.translation;import net.sf.mp.demo.tranxy.domain.translation.Translation;
import java.util.List;
import net.sf.minuteProject.architecture.bsla.bean.criteria.PaginationCriteria;
import net.sf.minuteProject.architecture.bsla.dao.face.DataAccessObject;/**** pTitle: TranslationDao/p** pDescription: Interface of a Data access object dealing with TranslationDao* persistence. It offers a set of methods which allow for saving,* deleting and searching translation objects/p**/
public interface TranslationDao extends DataAccessObject {/*** Inserts a Translation entity * param Translation translation*/public void insertTranslation(Translation translation) ;/*** Inserts a list of Translation entity * param ListTranslation translations*/public void insertTranslations(ListTranslation translations) ;/*** Updates a Translation entity * param Translation translation*/public Translation updateTranslation(Translation translation) ;/*** Updates a Translation entity with only the attributes set into Translation.* The primary keys are to be set for this method to operate.* This is a performance friendly feature, which remove the udibiquous full load and full update when an* update is to be done* Remark: The primary keys cannot be update by this methods, nor are the attributes that must be set to null.* param Translation translation*/public int updateNotNullOnlyTranslation(Translation translation) ;public int updateNotNullOnlyPrototypeTranslation(Translation translation, Translation prototypeCriteria);/*** Saves a Translation entity * param Translation translation*/public void saveTranslation(Translation translation);/*** Deletes a Translation entity * param Translation translation*/public void deleteTranslation(Translation translation) ;/*** Loads the Translation entity which is related to an instance of* Translation* param Long id* return Translation The Translation entitypublic Translation loadTranslation(Long id);
*//*** Loads the Translation entity which is related to an instance of* Translation* param java.lang.Long Id* return Translation The Translation entity*/public Translation loadTranslation(java.lang.Long id); /*** Loads a list of Translation entity * param Listjava.lang.Long ids* return ListTranslation The Translation entity*/public ListTranslation loadTranslationListByTranslation (ListTranslation translations);/*** Loads a list of Translation entity * param Listjava.lang.Long ids* return ListTranslation The Translation entity*/public ListTranslation loadTranslationListById(Listjava.lang.Long ids);/*** Loads the Translation entity which is related to an instance of* Translation and its dependent one to many objects* param Long id* return Translation The Translation entity*/public Translation loadFullFirstLevelTranslation(java.lang.Long id);/*** Loads the Translation entity which is related to an instance of* Translation* param Translation translation* return Translation The Translation entity*/public Translation loadFullFirstLevelTranslation(Translation translation); /*** Loads the Translation entity which is related to an instance of* Translation and its dependent objects one to many* param Long id* return Translation The Translation entity*/public Translation loadFullTranslation(Long id) ;/*** Searches a list of Translation entity based on a Translation containing Translation matching criteria* param Translation translation* return ListTranslation*/public ListTranslation searchPrototypeTranslation(Translation translation) ;/*** Searches a list of Translation entity based on a list of Translation containing Translation matching criteria* param ListTranslation translations* return ListTranslation*/public ListTranslation searchPrototypeTranslation(ListTranslation translations) ; /*** Searches a list of Translation entity * param Translation translation* return List*/public ListTranslation searchPrototypeTranslation(Translation translationPositive, Translation translationNegative) ;/*** Load a paginated list of Translation entity dependent of pagination criteria* param PaginationCriteria paginationCriteria* return List*/public ListTranslation loadPaginatedTranslation (Translation translation, PaginationCriteria paginationCriteria) ;}/*** Copyright (c) minuteproject, minuteprojectgmail.com* All rights reserved.* * Licensed under the Apache License, Version 2.0 (the License)* you may not use this file except in compliance with the License.* You may obtain a copy of the License at* * http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* * More information on minuteproject:* twitter minuteproject* wiki http://minuteproject.wikispaces.com * blog http://minuteproject.blogspot.net*
*/
/*** template reference : * - name : BslaDaoInterfaceExtendedUML* - file name : BslaDaoInterfaceKFUML.vm
*/
package net.sf.mp.demo.tranxy.dao.face.translation;import net.sf.mp.demo.tranxy.domain.translation.Translation;
import java.util.List;
import net.sf.minuteProject.architecture.filter.data.Criteria;
import net.sf.minuteProject.architecture.bsla.dao.face.DataAccessObject;/**** pTitle: TranslationExtDao/p** pDescription: Interface of a Data access object dealing with TranslationExtDao* persistence. It offers extended DAO functionalities/p**/
public interface TranslationExtDao extends DataAccessObject {/*** Inserts a Translation entity with cascade of its children* param Translation translation*/public void insertTranslationWithCascade(Translation translation) ;/*** Inserts a list of Translation entity with cascade of its children* param ListTranslation translations*/public void insertTranslationsWithCascade(ListTranslation translations) ; /*** lookup Translation entity Translation, criteria and max result number*/public ListTranslation lookupTranslation(Translation translation, Criteria criteria, Integer numberOfResult);public Integer updateNotNullOnlyTranslation (Translation translation, Criteria criteria);/*** Affect the first translation retrieved corresponding to the translation criteria.* Blank criteria are mapped to null.* If no criteria is found, null is returned.*/public Translation affectTranslation (Translation translation);public Translation affectTranslationUseCache (Translation translation);/*** Assign the first translation retrieved corresponding to the translation criteria.* Blank criteria are mapped to null.* If no criteria is found, null is returned.* If there is no translation corresponding in the database. Then translation is inserted and returned with its primary key(s). */public Translation assignTranslation (Translation translation);/*** Assign the first translation retrieved corresponding to the mask criteria.* Blank criteria are mapped to null.* If no criteria is found, null is returned.* If there is no translation corresponding in the database. * Then translation is inserted and returned with its primary key(s). * Mask servers usually to set unique keys or the semantic reference*/public Translation assignTranslation (Translation translation, Translation mask);public Translation assignTranslationUseCache (Translation translation);/*** return the first Translation entity found*/ public Translation getFirstTranslation (Translation translation);/*** checks if the Translation entity exists*/ public boolean existsTranslation (Translation translation); public boolean existsTranslationWhereConditionsAre (Translation translation);/*** partial load enables to specify the fields you want to load explicitly*/ public ListTranslation partialLoadTranslation(Translation translation, Translation positiveTranslation, Translation negativeTranslation);/*** partial load with parent entities* variation (list, first, distinct decorator)* variation2 (with cache)*/public ListTranslation partialLoadWithParentTranslation(Translation translation, Translation positiveTranslation, Translation negativeTranslation);public ListTranslation partialLoadWithParentTranslationUseCache(Translation translation, Translation positiveTranslation, Translation negativeTranslation, Boolean useCache);public ListTranslation partialLoadWithParentTranslationUseCacheOnResult(Translation translation, Translation positiveTranslation, Translation negativeTranslation, Boolean useCache);/*** variation first*/public Translation partialLoadWithParentFirstTranslation(Translation translationWhat, Translation positiveTranslation, Translation negativeTranslation);public Translation partialLoadWithParentFirstTranslationUseCache(Translation translationWhat, Translation positiveTranslation, Translation negativeTranslation, Boolean useCache);public Translation partialLoadWithParentFirstTranslationUseCacheOnResult(Translation translationWhat, Translation positiveTranslation, Translation negativeTranslation, Boolean useCache);/*** variation distinct*/public ListTranslation getDistinctTranslation(Translation translationWhat, Translation positiveTranslation, Translation negativeTranslation);//public List partialLoadWithParentForBean(Object bean, Translation translation, Translation positiveTranslation, Translation negativeTranslation);/*** search on prototype with cache*/public ListTranslation searchPrototypeWithCacheTranslation (Translation translation);/*** Searches a list of distinct Translation entity based on a Translation mask and a list of Translation containing Translation matching criteria* param Translation translation* param ListTranslation translations* return ListTranslation*/public ListTranslation searchDistinctPrototypeTranslation(Translation translationMask, ListTranslation translations) ; public ListTranslation countDistinct (Translation whatMask, Translation whereEqCriteria);public Long count (Translation whereEqCriteria);public ListTranslation loadGraph(Translation graphMaskWhat, ListTranslation whereMask); public ListTranslation loadGraphFromParentKey (Translation graphMaskWhat, ListTranslation parents); /*** generic to move after in superclass*/public ListObject[] getSQLQueryResult(String query); } DAO实施 TranslationJPAImpl和TranslationJPAExtImpl未复制代码。 将来通用DAO将用于跨实体冗余方面。 将执行对spring 3.x的适应即不再通过EntityManager注入进行JPASupport扩展 同时上面的代码在spring 2.5上可以正常工作 弹簧配置 spring-config-Tranxy-BE-main.xml ?xml version1.0 encodingUTF-8?
!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN//EN http://www.springframework.org/dtd/spring-beans.dtdbeans!-- Dao JPA --import resourceclasspath:net/sf/mp/demo/tranxy/factory/spring/spring-config-JPA-Tranxy-dao.xml/ !--MP-MANAGED-UPDATABLE-BEGINNING-DISABLE JPAtranxyFactory-tranxy--!-- hibernate config to put in an appart config file--bean idJPAtranxyFactory autowirebyNameclassorg.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean!-- all connection information are retrieve from the persistence file--!--property namedataSource ref.../property namepersistenceUnitName value.../--property namepersistenceXmlLocation valueclasspath:META-INF/persistence.xml //bean
!--MP-MANAGED-UPDATABLE-ENDING--!-- Database --import resourceclasspath:net/sf/mp/demo/tranxy/factory/spring/spring-config-Tranxy-database.xml/ /beans spring-config-Tranxy-database.xml ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:jndihttp://www.springframework.org/schema/jeexmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdbean idplaceHolderConfigclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationvalueclasspath:net/sf/mp/demo/tranxy/factory/spring/spring-config-Tranxy.properties/value/property/bean bean idtranxyTransactionManager classorg.springframework.orm.jpa.JpaTransactionManagerproperty nameentityManagerFactory refJPAtranxyFactory//bean!-- to get the entity manager --tx:annotation-driven transaction-managertranxyTransactionManager//beans spring-config-Tranxy-BE-main jdbc.tranxy.driverClassNameorg.gjt.mm.mysql.Driver
jdbc.tranxy.urljdbc:mysql://127.0.0.1:3306/tranxy
jdbc.tranxy.usernameroot
jdbc.tranxy.passwordmysql
jdbc.tranxy.jndijdbc/tranxy
hibernate.dialectorg.hibernate.dialect.MySQLDialect spring-config-JPA-Tranxy-dao.xml ?xml version1.0 encodingUTF-8?
!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN//EN http://www.springframework.org/dtd/spring-beans.dtdbeans!-- Import Dao definitions for business components --!-- tranxy --import resourceclasspath:net/sf/mp/demo/tranxy/factory/spring/tranxy/dao-JPA-Tranxy.xml/!-- translation --import resourceclasspath:net/sf/mp/demo/tranxy/factory/spring/translation/dao-JPA-Translation.xml/!-- Import Ext Dao definitions for business components --!-- tranxy extended dao --import resourceclasspath:net/sf/mp/demo/tranxy/factory/spring/tranxy/dao-ext-JPA-Tranxy.xml/!-- translation extended dao --import resourceclasspath:net/sf/mp/demo/tranxy/factory/spring/translation/dao-ext-JPA-Translation.xml//beans dao-JPA-Translation.xml ?xml version1.0 encodingUTF-8?
!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN//EN http://www.springframework.org/dtd/spring-beans.dtd
beansbean idtranslationDao classnet.sf.mp.demo.tranxy.dao.impl.jpa.translation.TranslationJPAImpl singletonfalse property nameentityManagerFactoryref beanJPAtranxyFactory//property/beanbean idtranslationKeyDao classnet.sf.mp.demo.tranxy.dao.impl.jpa.translation.TranslationKeyJPAImpl singletonfalse property nameentityManagerFactoryref beanJPAtranxyFactory//property/beanbean idtranslationRequestDao classnet.sf.mp.demo.tranxy.dao.impl.jpa.translation.TranslationRequestJPAImpl singletonfalse property nameentityManagerFactoryref beanJPAtranxyFactory//property/bean/beans dao-ext-JPA-Translation.xmldao-ext-JPA-Tranxy.xmldao-JPA-Tranxy.xml文件相同 但是请稍等...如何进行单元测试 在编写自己的测试之前您还需要另外两个工件。 一个是persistence.xml … 再次 是的对于嵌入式连接池因为JPA2层的构建随附提供的引用可能是JNDI数据源如果将属性环境设置为远程。 由于它位于/ src / test / resources / META-INF下因此它将覆盖JPA2软件包中的那个。 二是扩展AbstractTransactionalJUnit4SpringContextTests 的适配器 它在/ src / test / java中生成 package net.sf.mp.demo.tranxy.dao.face;import javax.sql.DataSource;import org.apache.commons.lang.StringUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations{classpath:net/sf/mp/demo/tranxy/factory/spring/spring-config-Tranxy-BE-main.xml
})
TransactionConfiguration(transactionManager tranxyTransactionManager)
Transactional
public class AdapterTranxyTestDao extends AbstractTransactionalJUnit4SpringContextTests { OverrideAutowiredpublic void setDataSource(Qualifier(value tranxyDataSource) DataSource dataSource) {this.simpleJdbcTemplate new SimpleJdbcTemplate(dataSource);}
... CXF 层 每个实体都有一个带有JAX-RS批注的Rest Resource工件以启用CRUD访问。 翻译示例 /*** Copyright (c) minuteproject, minuteprojectgmail.com* All rights reserved.* * Licensed under the Apache License, Version 2.0 (the License)* you may not use this file except in compliance with the License.* You may obtain a copy of the License at* * http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* * More information on minuteproject:* twitter minuteproject* wiki http://minuteproject.wikispaces.com * blog http://minuteproject.blogspot.net*
*/
/*** template reference : * - name : CXFSpringEntityResource* - file name : CXFSpringEntityResource.vm
*/
package net.sf.mp.demo.tranxy.rest.translation;import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.sql.*;import javax.servlet.http.*;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;import net.sf.mp.demo.tranxy.dao.face.translation.TranslationDao;
import net.sf.mp.demo.tranxy.dao.face.translation.TranslationExtDao;
import net.sf.mp.demo.tranxy.domain.translation.Translation;/**** pTitle: TranslationResource/p** pDescription: remote interface for TranslationResource service /p**/
Path (/rest/xml/translations)
Produces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
Consumes ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
Service
Transactional
public class TranslationResource {AutowiredQualifier(translationDao)TranslationDao translationDao;AutowiredQualifier(translationExtDao)TranslationExtDao translationExtDao;//MP-MANAGED-UPDATABLE-BEGINNING-DISABLE FIND_ALL-translationGETProduces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public ListTranslation findAll () {ListTranslation r new ArrayListTranslation();ListTranslation l translationDao.searchPrototypeTranslation(new Translation());for (Translation translation : l) {r.add(translation.flat());}return r;}
//MP-MANAGED-UPDATABLE-ENDING//MP-MANAGED-UPDATABLE-BEGINNING-DISABLE FIND_BY_ID-translationGETPath({id})Produces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Translation findById (PathParam (id) java.lang.Long id) {Translation _translation new Translation ();_translation.setId(id);_translation translationExtDao.getFirstTranslation(_translation);if (_translation!null) return _translation.flat();return new Translation ();}
//MP-MANAGED-UPDATABLE-ENDINGDELETEPath({id})public void delete (PathParam (id) Long id) {Translation translation new Translation ();translation.setId(id);translationDao.deleteTranslation(translation);}POSTProduces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) Consumes(MediaType.APPLICATION_FORM_URLENCODED)public Translation create (FormParam(id) Long id,FormParam(translation) String translation,FormParam(language) Integer language,FormParam(key) Long key,FormParam(isFinal) Short isFinal,FormParam(dateFinalization) Date dateFinalization,FormParam(translator) Long translator,Context HttpServletResponse servletResponse) throws IOException {Translation _translation new Translation (id,translation,language,key,isFinal,dateFinalization,translator);return save(_translation);}PUTConsumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})public Translation save(JAXBElementTranslation jaxbTranslation) {Translation translation jaxbTranslation.getValue();if (translation.getId()!null)return translationDao.updateTranslation(translation);return save(translation);}public Translation save (Translation translation) {translationDao.saveTranslation(translation);return translation;}} Web应用程序和Spring的两个文件位于/ src / main / resources / webapp / WEB-INF Web.xml ?xml version1.0 encodingUTF-8?
web-app version2.5 xmlnshttp://java.sun.com/xml/ns/javaee xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsddisplay-nametranxy CXF REST/display-namedescriptiontranxy CXF REST access/descriptioncontext-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/application-context.xml/param-value/context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerservletservlet-nameCXFServlet/servlet-nameservlet-classorg.apache.cxf.transport.servlet.CXFServlet/servlet-classload-on-startup1/load-on-startup/servletservlet-mappingservlet-nameCXFServlet/servlet-nameurl-pattern/*/url-pattern/servlet-mapping/web-app application-context.xml ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:jaxrshttp://cxf.apache.org/jaxrsxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsdimport resourceclasspath:META-INF/cxf/cxf.xml /import resourceclasspath:META-INF/cxf/cxf-extension-jaxrs-binding.xml /import resourceclasspath:META-INF/cxf/cxf-servlet.xml /context:component-scan base-packagenet.sf.mp.demo.tranxy.rest/import resourceclasspath:net/sf/mp/demo/tranxy/factory/spring/spring-config-Tranxy-BE-main.xml/ jaxrs:server idrestContainer address/jaxrs:serviceBeans!-- tranxy -- ref beanapplicationResource/ref beanlanguageResource/ref beanuserResource/!-- translation -- ref beantranslationResource/ref beantranslationKeyResource/ref beantranslationRequestResource/!-- statements --/jaxrs:serviceBeans/jaxrs:server /beans 打包部署和测试 包 在生成软件包之前minuteproject mp-bsla.xyjar附带了一个依赖项以进行安装。 在/ target / mp-bsla /运行脚本中maven-install.cmd / sh 构建 MVN清洁包 结果是/ rest / target中的tranxyRestCxfApp.war 部署方式 启动tomcat 将tranxyRestCxfApp.war放到/ webapps中 有一个嵌入式连接池因此不需要在tomcat上进行任何配置。 测试 USE tranxy ;
DELETE FROM application_x_key;
DELETE FROM translation;
DELETE FROM language_x_translator;
DELETE FROM language_x_speaker;
DELETE FROM request_key;DELETE FROM application;
DELETE FROM translation_key;
DELETE FROM user;
DELETE FROM language;INSERT INTO application (idapplication, name, description, type) VALUES (-1,Porphyry, OS application holding environment app, OPENSOURCE);
INSERT INTO application (idapplication, name, description, type) VALUES (-2,Minuteproject, Minuteproject app, OPENSOURCE);INSERT INTO user (iduser, first_name, last_name, email) VALUES (-1,test, lastName, testtest.me);
INSERT INTO user (iduser, first_name, last_name, email) VALUES (-2,test2, lastName2, test2test.me);INSERT INTO language (idlanguage, code, description, locale) VALUES (-1, FR, France, fr);
INSERT INTO language (idlanguage, code, description, locale) VALUES (-2, ES, Spanish, es);
INSERT INTO language (idlanguage, code, description, locale) VALUES (-3, EN, English, en); INSERT INTO language_x_translator (language_id, user_id) VALUES (-1, -1);
INSERT INTO language_x_translator (language_id, user_id) VALUES (-2, -1);
INSERT INTO language_x_speaker (language_id, user_id) VALUES (-1, -1);
INSERT INTO language_x_speaker (language_id, user_id) VALUES (-2, -1);INSERT INTO language_x_translator (language_id, user_id) VALUES (-1, -2);
INSERT INTO language_x_translator (language_id, user_id) VALUES (-2, -2);
INSERT INTO language_x_translator (language_id, user_id) VALUES (-3, -2); INSERT INTO translation_key (id, key_name, description) VALUES (-1, msg.user.name, user name);
INSERT INTO translation (id, translation, language_id, key_id, is_final, date_finalization, translator_id)
VALUES (-1, nom, -1, -1, 1, 2012-04-04, -1);
INSERT INTO translation (id, translation, language_id, key_id, is_final, date_finalization, translator_id)
VALUES (-2, apellido, -1, -2, 1, CURDATE(), -1); 现在输入 http// localhost8080 / tranxyRestCxfApp / rest / xml / languages以获取所有语言 这是结果 现在输入 http// localhost8080 / tranxyRestCxfApp / rest / xml / users / -1获取第一个用户 这是结果 结论 本文介绍了如何在数据库模型之上快速获取CRUD REST接口。 当然您可能不需要对所有实体都使用CRUD并且可能需要更多的粗粒度函数来操纵模型。 下一篇文章将向您展示语句驱动开发如何使我们更接近用例。 参考 RigaJUG –演示–来自JCG合作伙伴 Florian Adler的REST CXF 位于minuteproject博客博客上。 翻译自: https://www.javacodegeeks.com/2012/05/rest-cxf-for-spring-jpa2-backend.html