zoukankan      html  css  js  c++  java
  • hibernate的save方法和原生的sql语句使用的数据库区别

    hibernate配置如下

    urapport_config.url=jdbc:mysql://10.10.202.152:3307/urapport_config_inner?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8mb4&autoReconnect=true
    urapport_config.user=pgm
    urapport_config.password=pgmfetion
    <bean id="dataSource0_urapport_config" parent="abstractDatasource" >
            <property name="maxPoolSize">
                <value>${c3p0_r.maxPoolSize}</value>
            </property>
            <property name="jdbcUrl" value="${urapport_config.url}" />
            <property name="user" value="${urapport_config.user}" />
            <property name="password" value="${urapport_config.password}" />
        </bean>
    <bean id="sessionFactory_urapport_config" parent="sessionfactory" >
            <property name="dataSource">
                <ref bean="dataSource0_urapport_config"/> 
            </property>        
            <property name="packagesToScan">   
                <list>   
                    <value>com.osp.bean</value>
                </list>   
            </property>    
        </bean>
       
        <bean id="urapport_config" class="com.osp.dao.GenericDAO">
            <property name="sessionFactory" ref="sessionFactory_urapport_config" />
        </bean>

    实体类注解如下

    @Entity
    @Table(name = "cin_clientupdate_new", catalog = "urapport_config")
    public class ClientVersion {
        private Long id;
        private String clienttype;
        private String clientversion;
        private String updatetype;
    }

    使用save方法保存对象时

    public void save(ClientVersion version){
            this.genericDAO("urapport_config").save(version);
        }
    public GenericDAO genericDAO(String daoName) {
            GenericDAO dao = (GenericDAO) SpringHelper.getBean(daoName);
            logger.debug(daoName);
            return dao;
        }
    public T save(T obj) {
            getHibernateTemplate().save(obj);
            return obj;
        }
    此时保存到的数据库是实体类注解上的库中,即数据库urapport_config中。如果实体类注解不指定数据库,则使用hibernate配置的数据库。

    public void updateStatus(String status,long id){
            String sql = "update cin_clientupdate_new set updatetype =? where id =?";
            genericDAO("urapport_config").sqlUpdate(sql, status,id);
        }
    
    public int sqlUpdate(final String sql, final Object... objs) {
            Integer result = 0;
            result = (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws SQLException {
                    int updnum;
                    try {
                        SQLQuery query = session.createSQLQuery(sql);
                        if (objs != null && objs.length > 0) {
                            for (int i = 0; i < objs.length; i++) {
                                query.setParameter(i, objs[i]);
                            }
                        }
                        updnum = 0;
                        updnum = query.executeUpdate();
    
                    }catch (Exception e) {
                        e.printStackTrace();
                        return 0;
                    } finally {
                        releaseSession(session);
                    }
                    return updnum;
                }
            });
            return result;
        }

    执行原生sql时,使用的数据库是hibernate配置的数据库,即urapport_config_inner。

    使用注意:

    执行原生sql时,实体类建议不要用注解配置数据库,以免两处配置的不一样。会使用连接池中配置的数据库。

    执行hibernate提供的持久化方法时,会优先使用实体类注解配置的数据库。如果注解未配置,则使用连接池中配置的。

    urapport_config_inner
  • 相关阅读:
    对json按某个键的值进行排序(转载)
    nrm -- NPM registry 管理工具
    easyui-filebox 文件上传
    Netty,Thrifty
    VS2015 framework4.5代码提示英文切换为中文
    .NET Runtime version 2.0.50727.8762
    js moment.js日期操作类 datetime,日期操作,dayjs
    Oracle.ManagedDataAccess.dll
    打造自己的JavaScript武器库(转)
    Vue.js实战 5.5章 购物车
  • 原文地址:https://www.cnblogs.com/xyfaneast/p/11382576.html
Copyright © 2011-2022 走看看