-
源码分析
(1)DruidDataSourceFactory源码public final static String PROP_PASSWORD = "password"; public final static String PROP_URL = "url"; public final static String PROP_USERNAME = "username";
从上面的代码中可以看出Druid获取数据库的字段是没有前缀的
(2)Hibernate的源码public static final String URL ="hibernate.connection.url"; /** * Names the connection user. This might mean one of 2 things in out-of-the-box Hibernate * {@link org.hibernate.service.jdbc.connections.spi.ConnectionProvider}: <ul> * <li>The username used to pass along to creating the JDBC connection</li> * <li>The username used to obtain a JDBC connection from a data source</li> * </ul> */ public static final String USER ="hibernate.connection.username"; /** * Names the connection password. See usage discussion on {@link #USER} */ public static final String PASS ="hibernate.connection.password";
Hibernate的数据库配置信息是有hibernate.connection前缀的,所以当使用Druid是要注意property的转换
-
修改Hibernate数据库配置文件
<property name="connection.provider_class">posp.tools.DruidConnectionProvider</property>#Druid连接池的Hibernate辅助类 <property name="url"></property> <property name="username"></property> <property name="password"></property>
-
修改Druid辅助类的configure方法
配置文件 <property name="connection.provider_class">posp.tools.DruidConnectionProvider</property>#Druid连接池的Hibernate辅助类 <property name="hibernate.connection.url"></property> <property name="hibernate.connection.username"></property> <property name="hibernate.connection.password"></property> 辅助类 public void configure(Map props) { String userName = (String) props.get(Environment.USER); String password = (String) props.get(Environment.PASS); String url = (String) props.get(Environment.URL); try { //在这里可以对数据库连接信息解密 super.configure(props); } catch (Exception e) { e.printStackTrace(); } }
Hibernate使用Druid连接池
最新推荐文章于 2021-01-26 22:16:50 发布