Hybris Item And Bean Definition
Note: Both Mode l and DTO do not need to be written manually. Hybris can be automatically generated through Item. XML and bean. XML configuration, which is also a powerful feature of hybris.
Item
item is the nearest thing to the bottom that programmers can touch in hybris. GenericItem is the lowest parent of all entity classes, which is equivalent to java.lang.Object of Java type system. All items inherit GenericItem by default.
Definition of Model
autocreate = true: When executing Hybris command line ant initialize to initialize the Hybris system, create the corresponding type in the database table according to the definition of items.xml.
Gene = true: Generate the corresponding POJO class for this type at ant compilation time.
(The default generation and autocreate are true, and if generate = false, the corresponding model will be generated, but the corresponding jalo will not be generated. If generate = true, the corresponding project model and jalo will be generated. If you want your son to inherit the parent, you need to set the parent generator to true. If deployment table is set, autocreate needs to be set to true, and when updatesystem is set, the corresponding table is generated.
<itemtypes> <itemtype code="Person" generate="true"> <deployment table="Persons" typecode="32333"/> <attributes> <attribute qualifier="name" type="java.lang.String"> <persistence type="property"/> </attribute> <attribute qualifier="age" type="java.lang.Integer"> <persistence type="property"/> </attribute> </attributes> </itemtype> <itemtype code="Animal"> <deployment table="Animal" typecode="32231"/> <attributes> <attribute qualifier="name" type="java.lang.String"> <persistence type="property"/> </attribute> <attribute qualifier="age" type="java.lang.Integer"> <persistence type="property"/> </attribute> </attributes> </itemtype> <itemtype code="Plant"> <deployment table="Plant" typecode="32330"/> <attributes> <attribute qualifier="name" type="java.lang.String"> <persistence type="property"/> </attribute> <attribute qualifier="age" type="java.lang.Integer"> <persistence type="property"/> </attribute> </attributes> </itemtype> <itemtype code="Teacher" extends="Person"> <attributes> <attribute qualifier="century" type="java.lang.String"> <persistence type="property"/> </attribute> </attributes> </itemtype> </itemtypes>
bean
bean.xml is mainly used to define some data. Data is similar to dto in ordinary java engineering. In hybris, service layer returns model, then converts is called by facade layer, converts model into data and returns it to controller for use. The specific use of converts is explained in detail below.
<bean class="org.caps.training.Data.BaseItem"> <property name="code" type="java.lang.String"/> <property name="created" type="java.util.Date"/> <property name="updated" type="java.util.Date"/> <property name="type" type="java.lang.String"/> <property name="action" type="java.lang.String"/> </bean> <bean class="org.caps.training.Data.TeacherData" extends="org.caps.training.Data.BaseItem"> <property name="name" type="java.lang.String"/> <property name="age" type="java.lang.Integer"/> <property name="address" type="java.lang.String"/> </bean>
convert &popular
Convert mainly acts on the facade layer. It is commonly used with popular. It configures popular in convert and realizes the transformation from model to data.
Usage
- Customize a Populator, implement the Populator interface, generics pass in a model and a data
- Put this Populator into spring
- Inject a Converter whose parent class is abstractPopulating Converter, setting targetClass and populators
- Call convert
The cases are as follows:
- Custom populator
public class TeacherModelPopular implements Populator<TeacherModel, TeacherData> { @Override public void populate(TeacherModel teacherModel, TeacherData teacherData) throws ConversionException { teacherData.setCode(teacherModel.getCode()); teacherData.setAge(teacherModel.getAge()); teacherData.setName(teacherData.getName()); teacherData.setAddress(teacherModel.getAddress()); teacherData.setType("TeacherData"); teacherData.setCreated(new Date()); } }
- Injection spring
<bean id="teacherModelPopular" class="org.caps.training.convert.pop.TeacherModelPopular"/> <bean id="teacherModelConverter" parent="abstractPopulatingConverter"> <property name="targetClass" value="org.caps.training.Data.TeacherData"/> <property name="populators"> <list> <ref bean="teacherModelPopular"/> </list> </property> </bean>
- call
@Resource(name = "teacherModelConverter") private Converter<TeacherModel, TeacherData> teacherModelConverter; TeacherData convert = teacherModelConverter.convert(modelByExample,new TeacherData());