4.在 spring-hibernate.xml中添加另外一个sessionFactory的bean
SQL Server2000的JDBC驱动程序的DriverClassName是 "com.microsoft.jdbc.sqlserver.SQLServerDriver"
SQL Server2005的JDBC驱动程序的DriverClassName是 "com.microsoft.sqlserver.jdbc.SQLServerDriver" Tomcat *confcontext.xml配置 <Resource name="jdbc/pubs" auth="Container" type="javax.sql.DataSource" maxActive="100" maxldle="30"
maxWait="10000" username="sa" password="sa" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://localhost:1433;tabaseName=webshop" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"/> <ResourceLink global="jdbc/pubs" name="jdbc/pubs" type="javax.sql.DataSource"/> 并web.xml里配置
<resource-ref>
<description>DataSource</description> <res-ref-name>jdbc/pubs</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> applicationContext.xml
设置
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/comp/env/jdbc/pubs"/> </bean> |
各种配置
Oracle连接配置
driverClassName = oracle.jdbc.driver.OracleDriver
jdbc_url = jdbc:oracle:thin:@localhost:1521:dbname
jdbc_username = test
jdbc_password = test
MySql连接配置
driverClassName = com.mysql.jdbc.Driver
jdbc_url = jdbc:mysql://localhost:3306/sshf?useUnicode=true&characterEncoding=utf-8
jdbc_username = test
jdbc_password = test
Sql Server连接配置
driverClassName = net.sourceforge.jtds.jdbc.Driver
jdbc_url = jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname
jdbc_username = test
jdbc_password = test
DB2连接配置
driverClassName = com.ibm.db2.jdbc.app.DB2Driver
jdbc_url = jdbc:db2://localhost:5000/sample
jdbc_username = test
jdbc_password = test
sybase连接配置
driverClassName = com.sybase.jdbc.SybDrive
jdbc_url = jdbc:sybase:Tds:localhost:5007/myDB
jdbc_username = test
jdbc_password = test
PostgreSQL连接配置
driverClassName = org.postgresql.Driver
jdbc_url = jdbc:postgresql://localhost/myDB
jdbc_username = test
jdbc_password = test
1、使用org.springframework.jdbc.datasource.DriverManagerDataSource
说明:DriverManagerDataSource建立连接是只要有连接就新建一个connection,根本没有连接池的作用。
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
2、使用org.apache.commons.dbcp.BasicDataSource
说明:这是一种推荐说明的数据源配置方式,它真正使用了连接池技术
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="username">
<value>test</value>
</property>
<property name="password">
<value>test</value>
</property>
<property name="maxActive">
<value>255</value>
</property>
<property name="maxIdle">
<value>2</value>
</property>
<property name="maxWait">
<value>120000</value>
</property>
</bean>
3、使用org.springframework.jndi.JndiObjectFactoryBean
说明:JndiObjectFactoryBean 能够通过JNDI获取DataSource
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>java:comp/env/jdbc/roseindiaDB_local</value></property>
</bean>
4、使用com.mchange.v2.c3p0.ComboPooledDataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value=" oracle.jdbc.driver.OracleDriver "/>
<property name="jdbcUrl" value=" jdbc:oracle:thin:@localhost:1521:ora9i "/>
<property name="user" value="admin"/>
<property name="password" value="1234"/>
</bean>
总结:4种方式中的第一种没有使用连接池,故少在项目中用到,第三种方式需要在web server中配置数据源,不方便于部署。
推荐使用第2, 4方式进行数据源的配置,下面详细说明。 如果想要第三种方式web server中配置数据源,则请见 Tomcat配置DataSource
<!--MySql 驱动程序 eg. mysql-connector-java-5.0.4-bin.jar--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:mysql://localhost/dbname?characterEncoding=gb2312</property> <!-- 数据库用户名--> <property name="connection.username">root</property> <!-- 数据库密码--> <property name="connection.password">root</property>
<!--Sql Server 驱动程序 eg. jtds-1.2.jar--> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname</property> <!-- 数据库用户名--> <property name="connection.username">sa</property> <!-- 数据库密码--> <property name="connection.password"></property>
<!--Oracle 驱动程序 ojdbc14.jar--> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:dbname</property> <!-- 数据库用户名--> <property name="connection.username">test</property> <!-- 数据库密码--> <property name="connection.password">test</property>
RDBMS 方言
DB2 | org.hibernate.dialect.DB2Dialect |
DB2 AS/400 | org.hibernate.dialect.DB2400Dialect |
DB2 OS390 | org.hibernate.dialect.DB2390Dialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
MySQL | org.hibernate.dialect.MySQLDialect |
MySQL with InnoDB | org.hibernate.dialect.MySQLInnoDBDialect |
MySQL with MyISAM | org.hibernate.dialect.MySQLMyISAMDialect |
Oracle (any version) | org.hibernate.dialect.OracleDialect |
Oracle 9i/10g | org.hibernate.dialect.Oracle9Dialect |
Sybase | org.hibernate.dialect.SybaseDialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialect |
Microsoft SQL Server | org.hibernate.dialect.SQLServerDialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Informix | org.hibernate.dialect.InformixDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
Ingres | org.hibernate.dialect.IngresDialect |
Progress | org.hibernate.dialect.ProgressDialect |
Mckoi SQL | org.hibernate.dialect.MckoiDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Pointbase | org.hibernate.dialect.PointbaseDialect |
FrontBase | org.hibernate.dialect.FrontbaseDialect |
Firebird |
org.hibernate.dialect.FirebirdDialect |