首頁>Club>
2
回覆列表
  • 1 # 使用者4631824322

    目前我知道的就四種。

    如下所示:

    1:使用spring自帶的DriverManagerDataSource 配置檔案如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xs http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean name="dataSource"

    p:driverClassName="com.mysql.jdbc.Driver"

    p:url="jdbc:mysql://localhost:3306/test"

    p:username="root"

    p:password="123456" / >

    <bean name="dataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />

    <property name="url" value="jdbc:mysql://localhost:3306/test" />

    <property name="username" value="root" />

    <property name="password" value="123456" />

    </bean>

    -->

    </beans>

    2:C3P0資料來源。

    需要使c3p0的核心jar包,我使用的是c3p0-0.9.1.jar,比較穩定,推薦使用。一般在下載hibernate的時候都會自帶一個: 我在hibernate-release-4.3.0.Final\lib\optional\c3p0路徑下找到的。

    配置檔案中如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean name="dataSource"

    p:driverClass="com.mysql.jdbc.Driver"

    p:jdbcUrl="jdbc:mysql://localhost:3306/test"

    p:user="root"

    p:password="123456" >

    </bean>

    <property name="driverClass" value="com.mysql.jdbc.Driver" />

    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />

    <property name="user" value="root" />

    <property name="password" value="123456" />

    </bean>

    -->

    </beans>

    3:使用apache的dbcp外掛連線資料庫 需要下載的jar包:commons-dbcp.jar,commons-pool.jar,commons-collection.jar

    spring的配置檔案中如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean name="dataSource"

    p:driverClassName="com.mysql.jdbc.Driver"

    p:url="jdbc:mysql://localhost:3306/test"

    p:username="root"

    p:password="123456" >

    </bean>

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />

    <property name="url" value="jdbc:mysql://localhost:3306/test" />

    <property name="username" value="root" />

    <property name="password" value="123456" />

    </bean>

    -->

    </beans>

    4:使用hibernate資料來源 需要hiberante核心jar包,我使用的hibernate1的版本是hibernate-release-4.3.0.Final

    目前三大框架較流行,spring一般與hiberante做搭檔,資料庫連線方式寫在hiberante的配置檔案中,在spring管理hibernate中的配置檔案

    中,直接讀取hibernate核心配置檔案即可。在使用hibernate連線資料庫的時候需要讀取hibernate.cfg.xml的配置檔案和相應的實體類

    可參照下面的自己配置一下

    <bean>

    <property name="configLocations">

    <list>

    <value>classpath:com/config/hibernate.cfg.xml</value>

    </list>

    </property>

    <property name="mappingLocations">

    <list>

    <value>classpath:com/hibernate/*.hbm.xml</value>

    </list>

    </property>

  • 中秋節和大豐收的關聯?
  • 電纜穿線管規格型號有哪些?