# hibernate-plus **Repository Path**: tenmg/hibernate-plus ## Basic Information - **Project Name**: hibernate-plus - **Description**: 集成hibernate、dsql、sql-paging三个框架,为Java应用提供强大的数据库读写能力 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-05-16 - **Last Updated**: 2023-07-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # hibernate-plus ## 介绍 集成hibernate、dsql和sql-paging三个框架,为Java应用提供强大的数据库读写能力。 ## 使用说明 以基于Spring MVC的Maven项目为例 ### 1. pom.xml添加依赖 其中${hibernate-plus.version}为版本号,可定义属性或直接使用版本号替换。这里省略pom.xml的其他配置信息,其他配置请根据自己的项目实际自行添加。 ``` cn.tenmg hibernate-plus ${hibernate-plus.version} ``` ### 2. spring-context.xml配置文件 ``` ``` ### 3. hibernate.xml配置文件 ``` ${hibernate.dialect} ${hibernate.show_sql} ${hibernate.format_sql} ${hibernate.id.new_generator_mappings} ${hibernate.hbm2ddl.auto} ${hibernate.cache.region.factory_class} ${hibernate.cache.use_query_cache} ${hibernate.cache.use_second_level_cache} ${hibernate.jdbc.batch_size} ${hibernate.jdbc.fetch_size} cn.tenmg ``` ### 4. hibernate-plus.xml配置文件 ``` ``` ### 5. 编写Controller ``` @Controller @RequestMapping(value = "sec/user") public class UserController { @Resource private UserService userService; …… @RequestMapping(method = RequestMethod.GET, value = "page") public @ResponseBody Response page(@RequestParam Long currentPage, @RequestParam Integer pageSize, @RequestParam(required = false) String userName, @RequestParam(required = false) String roleId, @RequestParam(required = false) String menuId, @RequestParam(required = false) String intfcId) { return Response.status(Response.SUCCESS) .value(userService.page(currentPage, pageSize, userName, roleId, menuId, intfcId)); } @RequestMapping(method = RequestMethod.POST) public @ResponseBody Response post(@RequestBody String body) { JSONObject json = JsonUtils.createCheck(body, "userName", "password"); User user = new User(); json = JsonUtils.remove(json, "userId", "creUser", "creTm", "modUser", "modTm"); user.setState(UserState.NORMAL); String password = SecurityUtils.getSecurityManager().getEncryptor().encrypt(json.getString("password")); json.put("password", password); JsonUtils.loadJson(user, json); return Response.status(Response.SUCCESS).message("保存成功").value(this.userService.save(user)); } …… } ``` ### 6. 编写Service及实现类 6.1 编写用户服务接口 ``` public interface UserService extends Service { …… Page page(Long currentPage, Integer pageSize, String userName, String roleId, String menuId, String intfcId); …… } ``` 6.1 编写用户服务实现类,其中“sec.user.find”为DSQL的全局唯一编号,这样DSQL就可以通过配置文件来管理,如果逻辑发生变更,不需要总是调整java代码。 ``` @Service public class UserServiceImpl extends ServiceImpl implements UserService { …… @Override public Page page(Long currentPage, Integer pageSize, String userName, String roleId, String menuId, String intfcId) { return this.page(User.class, "sec.user.find", currentPage, pageSize, "valid", State.VALID, "roleId", roleId, "menuId", menuId, "intfcId", intfcId, "userName", userName); } …… } ``` 6.3 其中,封装一个基础服务类ServiceImpl,该类继承自cn.tenmg.hibernate.plus.service.impl.AbstractService以便继承对数据库的CURD操作。该类仅需注入配置的Dao并实现getDao方法即可。AbstractService主要是基于Dao的接口实现并指定了所对应的实体类型,以便简化对 Service对应的实体对象 的CURD操作。如果不进行此项封装,也可以每个Service实现类单独注入Dao,并使用Dao的接口执行CURD操作。 ``` /** * 数据库访问基础服务 * * @author June * * @param

*/ public class ServiceImpl

extends cn.tenmg.hibernate.plus.service.impl.AbstractService

{ @Autowired protected Dao dao; @Override protected Dao getDao() { return dao; } } ``` ### 7. 配置DSQL文件 配置`user.dsql.xml`,注意需要将文件放在`XMLFileDSQLFactory`扫描的路径下。 ``` ``` ## 相关链接 dsql开源地址:https://gitee.com/tenmg/dsql sql-paging开源地址:https://gitee.com/tenmg/sql-paging