hibernate validator工具类

一、相关的gradle配置文件

   // https://mvnrepository.com/artifact/javax.el/javax.el-api
    compile group: ‘javax.el‘, name: ‘javax.el-api‘, version: ‘3.0.0‘
    // https://mvnrepository.com/artifact/org.glassfish.web/javax.el
    compile group: ‘org.glassfish.web‘, name: ‘javax.el‘, version: ‘2.2.6‘
    // https://mvnrepository.com/artifact/org.hibernate/hibernate-validator
    compile group: ‘org.hibernate‘, name: ‘hibernate-validator‘, version: ‘5.4.1.Final‘

二、validate接口支持泛型参数

public static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

    public String validObject(Object msg) {
        String result = ConstantUtil.VALID_PASS;
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(msg);
        if(constraintViolations.isEmpty()){
            return result;
        }
        StringBuffer buff = new StringBuffer();
        for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
            buff.append(constraintViolation.getInvalidValue());
            buff.append(":");
            buff.append(constraintViolation.getMessage());
            buff.append("!\n");
        }
        return buff.toString().substring(0,buff.toString().length()-1);
    }

三、调用示例

public MsgBaseResponse processEmService(Emp em) {
        MsgBaseResponse response = new MsgBaseResponse();
        String validResult = validUtil.validObject(em);
        if (validResult.equals(ConstantUtil.VALID_PASS)) {
            //TODO: the Operate and data.
            System.out.println("valid success, can write to store.");
            response.setData("");
        } else {
            response.setResult(-1);
            response.setData(validResult);
        }
        return response;
时间: 2024-10-11 23:25:18

hibernate validator工具类的相关文章

hibernate之工具类

package util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Hibernate工具类 * 作用: 获取session与关闭session * @author 尹涛 * */ public class HibernateUtil { // 创建私有 静态 线程对象 private static Thread

hibernate简单工具类的封装

通过该工具类可以获得hibernate的session对象,并在项目中使用该工具 public class HibernateUtil { private static SessionFactory sessionFactory; //表示构造方法私有化,保证单例模式 private MyHibernateSessionFactory(){ } /** * @return 获取会话工厂 */ public static SessionFactory getSessionFactory() { i

hibernate之工具类HibernateUtil

1.最简单的工具类,实现SessionFactory的单例共享,session的管理 package com.cc.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service

hibernate 的工具类

package com.anrongtec.utils; import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * 工具类. * * @author CAOXIAOYANG * */public class HibernateUtil { private static final SessionFactory sessionFactor

Hibernate基础工具类

1 import org.hibernate.Session; 2 import org.hibernate.SessionFactory; 3 import org.hibernate.cfg.Configuration; 4 5 public class Hibernate_Util { 6 7 private static SessionFactory factory; 8 9 static { 10 Configuration configure = new Configuration(

hibernate工具类HibernateUtil详解

1.为什么要用hibernateUtil这个类,先看这段代码:  //加载配置文件信息默认为hiberna.cfg.xml,如果不是的话那么就在config()方法里面去解析他      Configuration config = new Configuration();      config.config();      //创建工厂      SessionFactory factory = config.bulidSessionFactory();      //得到session  

Hibernate工具类_抽取重复核心代码

问题:在Hibernate中每次执行一次操作总是需要加载核心配置文件,获取连接池等等都是重复动作,所以抽取出来 解决: package com.xxx.utils; /** *Hibernate的工具类 *@author cxh */ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg

为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?

既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了? Hibernate操作的步骤如下: 1. 获得Configuration对象 2. 创建SessionFactory 3. 创建Session 4. 打开事务 5. 进行持久化操作.比如上面的添加用户操作 6. 提交事务 7. 发生异常,回滚事务

【SSH三大框架】Hibernate基础第二篇:编写HibernateUtil工具类优化性能

相对于上一篇中的代码编写HibernateUtil类以提高程序的执行速度 首先,仍然要写一个javabean(User.java): package cn.itcast.hibernate.domain; import java.util.Date; public class User { private int id; private String name; private Date birthday; public int getId() { return id; } public voi