标签:hibernate 多数据库 防火墙 mysql dao
最近老师给了一个任务,需求是这样的private int id; private String serverName; private String host; private String userName; private String passWord;我们要通过读取A数据库上的服务器信息,去对应的数据库里获得数据。
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="packagesToScan"> <list> <value>com.core.model</value> </list> </property> </bean>还可以有packagesToScan这个属性扫描一下。
package com.core.dao; @Component public class UtilDAO extends HibernateDaoSupport { protected void initDao() { // do nothing } public void save(Object transientInstance) { try { getHibernateTemplate().save(transientInstance); // log.debug("save successful"); } catch (RuntimeException re) { // log.error("save failed", re); throw re; } } public void update(Object transientInstance) { try { getHibernateTemplate().update(transientInstance); // log.debug("save successful"); } catch (RuntimeException re) { // log.error("save failed", re); throw re; } } public List<?> findAllList(String entity){ try { String queryString = null; queryString = "from "+entity; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { // log.error("find by property name failed", re); throw re; } } public void delete(Object transientInstance){ try { getHibernateTemplate().delete(transientInstance); // log.debug("save successful"); } catch (RuntimeException re) { // log.error("save failed", re); throw re; } } @Resource public void setSessionFactory0(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } }看最后一个方法setSessionFactory0,将我们用Properties类生成的sessionFactory注入即可。
@SuppressWarnings("unchecked") public String getTreeFromRemote(){ UtilDAO _utilDAO=new UtilDAO(); JSONArray ja=new JSONArray(); List<Server> servers=(List<Server>) utilDAO.findAllList("Server"); //Server就是最开始说的那个Server for (Server server : servers) { _utilDAO=Hibernate3WithoutConfig.getUtilDAO(server); ja.add(getAPPTree(_utilDAO,server.getServerName())); } System.out.println("_________"); System.out.println(ja); return SUCCESS; }在数据库中,我们有一个Server表。
如下:
Hibernate3WithoutConfig.java public static UtilDAO getUtilDAO(Server s){ Properties p = new Properties(); p.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); p.put("hibernate.connection.url", "jdbc:mysql://"+s.getHost()+"/WG?useUnicode=true&characterEncoding=UTF-8"); p.put("hibernate.connection.username", s.getUserName()); p.put("hibernate.connection.password", s.getPassWord()); p.put("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect"); p.put("hibernate.hbm2ddl.auto","update"); p.put("hibernate.current_session_context_class", "thread"); p.put("hibernate.show_sql", "true"); Configuration conf = new AnnotationConfiguration().setProperties(p); conf.addClass(User.class); //这几个类是我需要用的class conf.addClass(Collection.class); conf.addClass(Groups.class); conf.addClass(Item.class); SessionFactory sf = conf.buildSessionFactory(); UtilDAO utilDAO=new UtilDAO(); utilDAO.setSessionFactory0(sf); return utilDAO; }
参考资料
http://developer.51cto.com/art/200907/133239.htm
http://blog.csdn.net/xiazdong/article/details/7562765
标签:hibernate 多数据库 防火墙 mysql dao
原文地址:http://blog.csdn.net/dlf123321/article/details/44261435