domain、entity和model的package的不同用法?

本文探讨了在软件开发中实体(entity)、模型(model)与域(domain)的概念及其使用场景。解释了实体类通常用于映射数据库表,模型类则侧重于前端展示,而域类适用于复杂的业务对象组合。
以前写项目,都是先快速创建entity、dao、service再说,其他的包一般都是根据不同的框架来创建不同功能的包。比如用struts框架创建一个action包,用servlet就创建一个servlet包,用jersey框架就创建一个resource包。但是最近看了一个国外的开源项目源代码,有一个包叫domain,打开一看里面就是存的实体类,功能和我们平时用的entity是一样的,那为什么要用domain而不用entity呢,那就涉及到这个包的具体用处了,虽然都是存的实体类,但是用途是不一样的。
(1)、entity(实体)
entity的意思就是实体的意思,所以也是最常用到的,entity包中的类是必须和数据库相对应的,比如说:数据库有个user表,字段有long类型的id,string类型的姓名,那么entity中的user类也必须是含有这两个字段的,且类型必须一致。不能数据库存的是long类型,user类里的属性是string类型。这样做的好处是保持实体类和数据库保持一致,另外,当用到hibernate或是mybatie框架来操作数据库的时候,操作这个实体类就行,写sql文之前不需要再做数据格式处理。

(2)、model(模型)
model大家不陌生,都知道是模型的意思,当用model当包名的时候,一般里面存的是实体类的模型,一般是用来给前端用的。比如:前端页面需要显示一个user信息,user包含姓名,性别,居住地,这些信息存在数据库的时候,姓名直接存姓名,但是性别和居住地一般会用数据字典的编号存到数据库,比如:111代表男,222代表女,数据库存的就是111或222,如果用entity的话,把111、222前端都不知道是什么玩意,就算前端知道111代表男,222代表女,写了一个js判断数据处理。后来数据库变动了,111代表女,222代表男,前端的js又需要重新写,很显然这样不利于维护。所以就需要model来解决,后台从数据库取了数据转化为前端需要的数据直接传给前端,前端就不需要对数据来处理,直接显示就行了。还有一种情况,数据库里面的user表字段有十个,包含姓名,qq,生辰八字乱七八糟的等,但是前台页面只需要显示姓名,如果把entity全部传给前台,无疑传了很多没用的数据。这时候model就很好的解决了这个问题,前台需要什么数据,model就包含什么数据就行了
(3)、domain(域)
domain这个包国外很多项目经常用到,字面意思是域的意思。范围有点广了,比如一个商城的项目,商城主要的模块就是用户,订单,商品三大模块,那么这三块数据就可以叫做三个域,domain包里就是存的就是这些数据,表面上这个包和entity和model包里存的数据没什么区别,其实差别还是挺大的,特别是一些大型的项目。比如一个招聘网站的项目,最重要的对象就是简历了,那么简历是怎么存到数据库的呢,不可能用一张表就能存的,因为简历包含基本信息和工作经验,项目经验,学习经验等。基本信息可以存在简历表,但是涉及到多条的就不行,因为没人知道有多少条工作经验,项目经验,所以必须要单独建工作经验表和项目经验表关联到简历基本信息表。但是前台页面是不关心这些的,前台需要的数据就是一个简历所有信息,这时就可以用到domain来处理,domain里面的类就是一个简历对象,包含了简历基本信息以及list的工作经验,项目经验等。这样前端只需要获取一个对象就行了,不需要同时即要获取基本信息,还要从基本信息里面获取工作经验关联的简历编号,然后再去获取对应的工作经验了。
当然,如果用model的话也是可以达到domain的效果的。这个完全是看个人喜好和项目的整体架构,因为创建不同的package的作用本来也就是想把项目分成不同的层,便于管理和维护。如果你乐意,你可以创建entity包,然后在里面存图片,创建images文件夹,里面存js。你已经看懂就行,前提是如果是团队开发的话能保证别人不打你。这个和语言一个道理,你在200面前和英国人说:privatevoid set(intage),人家说:滚犊子;现在你这样说,人家就知道是java语言了。能被人们通用的才叫语言,你说的别人听不懂那只能算是鸟语。所以开发的时候,建类建包的命名规则规范性还是很重要的。

那么三句话总结下entity、model、domain的不同:
1.entity字段必须和数据库字段一样
2.前端需要什么我们就给什么
3.domain很少用,代表一个对象模块
Checking package ... - Circular dependency - Circularity with mandatory links - Shortcut potentially generated as child table of a reference Checking data item ... - Data Item name uniqueness - Data Item code uniqueness - Data Item not used - Data Item used multiple times Warning The following data items are used more than once: -> Data Item '日期' (<Model>) - Detect differences between data item and associated domain - Detect inconsistencies between check parameters - Precision > Maximum Length - Undefined data type - Invalid data type - Incompatible format type Checking entity ... - Entity name uniqueness - Entity code uniqueness - Entity name maximum length - Entity code maximum length - Existence of attributes - Number of serial types > 1 - Existence of identifiers - Existence of relationship or association link - Redundant inheritance - Multiple inheritance - Parent of several inheritances - Redefined primary identifier Checking entity attribute ... - Entity Attribute name uniqueness Error The following entity attribute does not have unique name: -> Entity Attribute '机动车.日期' (<Model>::机动车) -> Entity Attribute '通知书.日期' (<Model>::通知书) - Entity Attribute code uniqueness Error The following entity attribute does not have unique code: -> Entity Attribute '机动车.日期' (<Model>::机动车) -> Entity Attribute '通知书.日期' (<Model>::通知书) Checking identifier of entity - Identifier name uniqueness - Identifier code uniqueness - Existence of entity attribute Error The following identifier has no entity attribute: -> Identifier '司机.Identifier_1' (<Model>::司机) - Identifier inclusion - Primary identifier in child entity Checking relationship ... - Relationship name uniqueness - Relationship code uniqueness - Reflexive dependency - Reflexive mandatory - Bijective relationship between two entities - Name uniqueness constraint between many-to-many relationships and entities - Identifier link from child entity 5 error(s), 1 warning(s). The Conceptual Data Model is incorrect, there are 5 error(s). 这个怎么改
05-14
package com.kucun.Service; import java.io.Serializable; import java.lang.reflect.Field; import java.util.*; import java.util.stream.Collectors; import javax.persistence.EntityNotFoundException; import javax.transaction.Transactional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; import com.kucun.data.entity.*; import com.kucun.dataDo.*; /** * * * */ @Service public class AppService { // 自动注入所有依赖 @Autowired private KucunRepository kucunRepository; @Autowired private DynamicRepositoryService repositoryService; @Autowired private EntityDependencyService dependencyService; /** * 获取所有实体类型的数据 * @return 包含所有实体数据的Map * * */ public Information getAllData() { Map<String, Object> response = new HashMap<>(); repositoryService.getRepositoryNameMap().forEach((key, repo) -> response.put(key + "s", repo.findAll()) ); return Information.NewSuccess(response); } /** * 获取自指定时间以来的所有更新 * @param since 时间戳 * @return 按实体类型分组的更新数据 */ public Information getUpdatesSince(Date since) { Map<String, List<?>> updates = new HashMap<>(); Map<String, CustomRepository<?, ?>> repoMap = repositoryService.getRepositoryNameMap(); repoMap.forEach((entityName, repo) -> { // 使用反射查询大于指定时间的记录 List<?> results = repositoryService.findUpdatedSince(entityName, since); if (!results.isEmpty()) { updates.put(entityName+'s', results); } }); return Information.NewSuccess(updates); } /** * 获取指定实体类型的所有数据 * @param entityName 实体名称 * @return 实体数据列表 * * */ public Information getEntityData(String entityName) { JpaRepository<?, ?> repo = repositoryService.getJpaRepository(entityName); return Information.NewSuccess(repo.findAll()); } /** * 添加新实体 * @param entity 要添加的实体对象 * @return 添加结果 * * */ public Information addEntity(@Valid Object entity) { try { // 处理关联字段 handleAssociations(entity); // 获取对应的Repository并保存 JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(entity.getClass()); //System.out.println(Information.NewSuccess(entity).DataJson()); if(entity instanceof User) { if(((User) entity).getPass()!=null) { String psass=((User) entity).getPass(); ((User) entity).setPass(PasswordService.hashPassword(psass)); } } Object savedEntity = repo.save(entity); // 如果是板材,初始化库存 if (savedEntity instanceof Bancai) { initializeKucunForBancai((Bancai) savedEntity); } return Information.NewSuccess(savedEntity); } catch (Exception ex) { return Information.Newfail(500, "创建失败: " + ex.getMessage(), null); } } /** * 更新实体 * @param entity 包含ID的实体对象 * @return 更新结果 * * */ public Information updateEntity(Object entity) { if (entity == null) { return Information.Newfail(403, "参数为空", null); } // 获取ID字段 Field idField=null; try { idField = getIdField(entity); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { idField.setAccessible(true); Object idValue = idField.get(entity); if (idValue == null) { return Information.Newfail(403, "ID为空", null); } // 获取Repository现有实体 JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(entity.getClass()); Object existingEntity = repo.findById((Serializable) idValue) .orElseThrow(() -> new RuntimeException("实体不存在")); if(entity instanceof User) { if(((User) entity).getPass()!=null) { String psass=((User) entity).getPass(); ((User) entity).setPass(PasswordService.hashPassword(psass)); } } // 复制非空属性并保存 copyNonNullProperties(entity, existingEntity); return Information.NewSuccess(repo.save(existingEntity)); } catch (Exception ex) { ex.fillInStackTrace(); return Information.Newfail(500, "更新失败: " + ex.getMessage(), null); } } /** * 删除实体 * @param entity 要删除的实体 * @return 删除结果 */ public Information deleteEntity(EntityBasis entity) { if (entity == null) { return Information.NewFail("删除对象不能为空"); } try { String entityName = entity.getClass().getSimpleName().toLowerCase(); JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(entityName); // 先加载完整实体(关键步骤) Object managedEntity = repo.findById(entity.getId()) .orElseThrow(() -> new EntityNotFoundException("实体不存在")); if(entity instanceof Bancai) { Kucun k=kucunRepository.findByBancai((Bancai)entity); if(k!=null&&k.getShuliang()>0) { return Information.NewFail("库存不为零,无法删除"); } if(k!=null) { Field deletedField = k.getClass().getDeclaredField("deleted"); deletedField.setAccessible(true); deletedField.set(k, true); Field deletedAtField = k.getClass().getDeclaredField("deletedAt"); deletedAtField.setAccessible(true); deletedAtField.set(k, new Date()); kucunRepository.save(k); } } // 检查依赖关系 if (dependencyService.hasDependencies(entity.getClass(), entity.getId())) { return Information.NewFail("该记录已被引用,无法删除"); } // 使用实体对象删除(触发级联操作) Field deletedField = managedEntity.getClass().getDeclaredField("deleted"); deletedField.setAccessible(true); deletedField.set(managedEntity, true); Field deletedAtField = managedEntity.getClass().getDeclaredField("deletedAt"); deletedAtField.setAccessible(true); deletedAtField.set(managedEntity, new Date()); repo.save(managedEntity); return Information.NewSuccess("删除成功"); } catch (Exception e) { return Information.NewFail("删除错误: " + e.getMessage()); } }/** * 动态查询实体 * @param entity 包含查询条件的实体对象 * @return 查询结果 */ public <T extends EntityBasis> Information queryEntity(T entity) { if (entity == null) { return Information.NewFail("查询参数不能为空"); } try { JpaRepository<T, ?> repo = (JpaRepository<T, ?>) repositoryService.getJpaRepository(entity.getClass()); // 创建基础查询条件 ExampleMatcher matcher = ExampleMatcher.matching() .withIgnoreNullValues() .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING); if(entity.getDeleted()==null) { // 添加软删除过滤条件 if (entity instanceof EntityBasis) { // 动态设置deleted=false条件 Field deletedField = EntityBasis.class.getDeclaredField("deleted"); deletedField.setAccessible(true); deletedField.set(entity, false); } } Example<T> example = Example.of(entity, matcher); return Information.NewSuccess(repo.findAll(example)); } catch (Exception e) { return Information.NewFail("查询失败: " + e.getMessage()); } } // ====================== 私有辅助方法 ====================== /** * 为板材初始化库存 * @param bancai 板材对象 */ private void initializeKucunForBancai(Bancai bancai) throws Exception { Kucun kucun = new Kucun(null, bancai, 0); bancai.setKucun(kucunRepository.save(kucun)); } /** * 处理实体关联关系 * @param entity 要处理的实体 */ private void handleAssociations(Object entity) throws Exception { for (Field field : entity.getClass().getDeclaredFields()) { field.setAccessible(true); Object value = field.get(entity); if (value == null) continue; // 处理 JPA 实体关联 if (value instanceof EntityBasis) { EntityBasis associated = (EntityBasis) value; JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(associated.getClass().getSimpleName().toLowerCase()); // 只处理已存在实体(不创建新关联) if (associated.getId() != null) { Object managedEntity = repo.findById(associated.getId()) .orElseThrow(() -> new Exception("关联实体不存在")); field.set(entity, managedEntity); } } // 处理集合关联 else if (value instanceof Collection) { List<Object> managedEntities = new ArrayList<>(); for (Object item : (Collection<?>) value) { if (item instanceof EntityBasis) { EntityBasis eb = (EntityBasis) item; JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(eb.getClass().getSimpleName().toLowerCase()); if (eb.getId() != null) { managedEntities.add(repo.findById(eb.getId()) .orElseThrow(() -> new Exception("关联实体不存在"))); } } } if (!managedEntities.isEmpty()) { field.set(entity, managedEntities); } } } } /** * 处理实体关联关系 * @param entity 要处理的实体 */ private void handleAssociationss(Object entity) throws Exception { for (Field field : entity.getClass().getDeclaredFields()) { field.setAccessible(true); Object value = field.get(entity); if(value instanceof EntityBasis) { } } } /** * 复制非空属性 * @param source 源对象 * @param target 目标对象 */ private void copyNonNullProperties(Object source, Object target) throws IllegalAccessException { for (Field field : source.getClass().getDeclaredFields()) { field.setAccessible(true); Object value = field.get(source); // 跳过关联字段ID字段 if (value != null && !(value instanceof EntityBasis) && !(value instanceof Collection) && !field.getName().equals("id")) { try { Field targetField = target.getClass().getDeclaredField(field.getName()); targetField.setAccessible(true); targetField.set(target, value); } catch (NoSuchFieldException ignored) {} } } } /* * 获取实体类的id字段(支持父类查找) * @param entity 实体对象 * @return Field对象或null */ public static Field getIdField(Object entity) { if (entity == null) { return null; } Class<?> clazz = entity.getClass(); // 递归查找类及其父类的id字段 while (clazz != null && clazz != Object.class) { try { Field idField = clazz.getDeclaredField("id"); idField.setAccessible(true); return idField; } catch (NoSuchFieldException e) { // 继续向上查找父类 clazz = clazz.getSuperclass(); } catch (SecurityException e) { e.printStackTrace(); return null; } } return null; } // ====================== 专用方法处理 Dingdan_bancai ====================== @Transactional public Information handleDingdanBancaiOperation( Dingdan_bancai entity, String operation ) { try { // 获取对应的Repository JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository("dingdan_bancai"); switch (operation) { case "add": Dingdan_bancai savedEntity = (Dingdan_bancai) repo.save(entity); updateKucunAndCreateJinhuo(savedEntity, savedEntity.getShuliang()); return Information.NewSuccess(savedEntity); case "update": // 先获取现有实体以计算差异 Dingdan_bancai existing = (Dingdan_bancai) repo.findById(entity.getId()) .orElseThrow(() -> new EntityNotFoundException("实体不存在")); int quantityDifference = entity.getShuliang() - existing.getShuliang(); Dingdan_bancai updatedEntity = (Dingdan_bancai) repo.save(entity); if (quantityDifference != 0) { updateKucunAndCreateJinhuo(updatedEntity, quantityDifference); } return Information.NewSuccess(updatedEntity); default: return Information.NewFail("不支持的操作类型: " + operation); } } catch (Exception ex) { return Information.NewFail(operation + "操作失败: " + ex.getMessage()); } } // 更新库存并创建进货记录 private void updateKucunAndCreateJinhuo(Dingdan_bancai db, int quantityChange) { // 1. 更新库存 Bancai bancai = db.getBancai(); Kucun kucun = kucunRepository.findByBancai(bancai); if (kucun == null) { kucun = new Kucun(null, bancai, 0); } kucun.setShuliang(kucun.getShuliang() + quantityChange); kucunRepository.save(kucun); // 2. 创建进货记录 Jinhuo jinhuo = new Jinhuo(); jinhuo.setDingdan_bancai(db); jinhuo.setShuliang(quantityChange); jinhuo.setDate(new Date()); // 使用临时字段设置用户 if (db.getCurrentUserId() != null) { User user = new User(); user.setId(db.getCurrentUserId()); jinhuo.setUser(user); } ((JinhuoRepository)repositoryService.getJpaRepository(jinhuo.getClass())).save( jinhuo); } // ... existing code ... /** * 保存所有实体类型的数据 * @param allData 包含所有实体数据的Map,key为实体类型名+"s",value为实体列表 * @return 保存结果 */ @Transactional public Information saveAllData(Map<String, List<?>> allData) { Map<String, Object> response = new HashMap<>(); allData.forEach((key, entities) -> { String entityName = key.endsWith("s") ? key.substring(0, key.length() - 1) : key; // 获取实体类类型 Class<?> entityClass = repositoryService.getEntityClass(entityName); if (entityClass != null) { // 类型安全转换 List<?> convertedList = entities.stream() .filter(entityClass::isInstance) .map(entityClass::cast) .collect(Collectors.toList()); // 保存实体 List<?> savedEntities = repositoryService.saveEntities( entityName, convertedList ); response.put(key, savedEntities); } else { response.put(key + "_error", "实体类型未注册"); } }); return Information.NewSuccess(response); } // ... existing code ... } package com.kucun.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.kucun.Service.AppService; import com.kucun.Service.DynamicRepositoryService; import com.kucun.data.entity.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.List; import java.util.Map; import javax.validation.Valid; @RestController @RequestMapping("/app") public class AppController { @Autowired private AppService appService; @Autowired private ObjectMapper objectMapper; private Map<String, Class<?>> ENTITY_MAP ; @Autowired public AppController( DynamicRepositoryService dynamicRepositoryService, AppService appService, ObjectMapper objectMapper ) { this.ENTITY_MAP = dynamicRepositoryService.getStringClassMap(); this.appService = appService; this.objectMapper = objectMapper; } // ====================== 数据查询 ====================== @GetMapping("/all") public Information getAllData( @RequestParam(value = "since",required=false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date since ) { System.out.println("since:"+since); try { if(since == null) return appService.getAllData(); else return appService.getUpdatesSince(since); } catch (Exception e) { // TODO: handle exception return Information.NewFail(e.getMessage()); } } // 添加保存全部数据的API端点 @PostMapping("/save-all") public Information saveAllData(@RequestBody Map<String, List<?>> allData) { try { return appService.saveAllData(allData); } catch (Exception e) { return Information.NewFail("保存全部数据失败: " + e.getMessage()); } } @GetMapping("/all/{entityType}") public Information getEntityData(@PathVariable String entityType, @RequestParam(value = "since",required=false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date since) { return appService.getEntityData(entityType.toLowerCase()); } // ====================== CRUD操作 ====================== @PostMapping("/add/{entityType}") public Information addEntity( @PathVariable String entityType, @RequestBody @Valid Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "add"); } @PostMapping("/select/{entityType}") public Information queryEntity( @PathVariable String entityType, @RequestBody Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "select"); } @PostMapping("/delete/{entityType}") public Information deleteEntity( @PathVariable String entityType, @RequestBody Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "delete"); } @PostMapping("/update/{entityType}") public Information updateEntity( @PathVariable String entityType, @RequestBody Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "update"); } // ====================== 核心辅助方法 ====================== private Information handleEntityOperation( String entityType, Map<String, Object> requestBody, String operation ) { String normalizedType = entityType.toLowerCase(); // 特殊处理 Dingdan_bancai if ("dingdan_bancai".equals(normalizedType)) { try { Dingdan_bancai entity = objectMapper.convertValue(requestBody, Dingdan_bancai.class); // 从请求体中提取用户ID并设置到临时字段 if (requestBody.containsKey("currentUserId")) { entity.setCurrentUserId(((Integer) requestBody.get("currentUserId"))); } return appService.handleDingdanBancaiOperation( entity, operation); } catch (Exception e) { return Information.NewFail(operation + "操作失败: " + e.getMessage()); } } Class<?> entityClass = ENTITY_MAP.get(normalizedType); if (entityClass == null) { return Information.NewFail("不支持的实体类型: " + entityType); } try { Object entity = objectMapper.convertValue(requestBody, entityClass); //System.out.println(Information.NewSuccess(requestBody).DataJson()); switch (operation) { case "add": return appService.addEntity(entity); case "select": return appService.queryEntity((EntityBasis) entity); case "delete": // 确保实体实现了EntityBasis接口 if (entity instanceof EntityBasis) { return appService.deleteEntity((EntityBasis) entity); } else { return Information.NewFail("删除操作需要实体实现EntityBasis接口"); } case "update": return appService.updateEntity(entity); default: return Information.NewFail("无效的操作类型"); } } catch (Exception e) { return Information.NewFail(operation + "操作失败: " + e.getMessage()); } } } 用php实现api
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值