hibernate.cfg.xml
<property name="connection.datasource">java:comp/env/jdbc/test</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>tomcat的service.xml
<Context className="org.apache.catalina.core.StandardContext" cachingAllowed="true" charsetMapperClass="org.apache.catalina.util.CharsetMapper" cookies="true" crossContext="false" debug="0" docBase="D:\Program Files\tomcat-5.0.28\webapps\test" mapperClass="org.apache.catalina.core.StandardContextMapper" path="/test" privileged="false" reloadable="true" swallowOutput="false" useNaming="true" wrapperClass="org.apache.catalina.core.StandardWrapper">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/test" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/test">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>password</name>
<value>test</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:jtds:sqlserver://127.0.0.1:1433/test</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>username</name>
<value>test</value>
</parameter>
</ResourceParams>
</Context>
<property name="connection.datasource">java:comp/env/jdbc/test</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>tomcat的service.xml
<Context className="org.apache.catalina.core.StandardContext" cachingAllowed="true" charsetMapperClass="org.apache.catalina.util.CharsetMapper" cookies="true" crossContext="false" debug="0" docBase="D:\Program Files\tomcat-5.0.28\webapps\test" mapperClass="org.apache.catalina.core.StandardContextMapper" path="/test" privileged="false" reloadable="true" swallowOutput="false" useNaming="true" wrapperClass="org.apache.catalina.core.StandardWrapper">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/test" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/test">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>password</name>
<value>test</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:jtds:sqlserver://127.0.0.1:1433/test</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>username</name>
<value>test</value>
</parameter>
</ResourceParams>
</Context>
<parameter>
<name>driverClassName</name>
<value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
我想问题是在hibernate读取tomcat数据源上,我不知怎样读取
<Resource name="jdbc/hibernate" type="javax.sql.DataSource"
password="11111111"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2" maxWait="5000" username="root"
url="jdbc:mysql://localhost/myhibernate?useUnicode=true&characterEncoding=gbk"
maxActive="4" />
数据源名字是jdbc/hibernate;连接的数据库是myhibernate;
2、在Tomcat中加入驱动,比如mysql的mysql-connector-java-5.0.0-beta-bin.jar,将驱动考入common\lib里
3、编写hibernate.cfg.xml,放在class文件夹下
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/hibernate</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/studentlogin/hibernate/Student.hbm.xml"/>
<mapping resource="com/studentlogin/hibernate/EntityClass.hbm.xml"/>
</session-factory>
</hibernate-configuration>4、编写xxx.hbn.xml文件,放在与xxx类相同的文件夹下,与上面的
<mapping resource="com/studentlogin/hibernate/Student.hbm.xml"/>
<mapping resource="com/studentlogin/hibernate/EntityClass.hbm.xml"/>
对应<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zhangning.studentlogin.hibernate">
<class name="EntityClass" table="class" lazy="false">
<id name="classid" column="classid" type="string">
<generator class="assigned"/>
</id>
<property name="classname" column="classname" type="string" not-null="true"/>
<set name="students" cascade="save-update" inverse="true" lazy="false">
<key column="classid"/>
<one-to-many class="com.zhangning.studentlogin.hibernate.Student"/>
</set>
</class>
</hibernate-mapping><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zhangning.studentlogin.hibernate">
<class name="Student" table="student" lazy="false">
<id name="studentid" column="studentid" type="string">
<generator class="assigned"/>
</id>
<property name="password" column="password" type="string" not-null="true"/>
<many-to-one name="entityclass" column="classid" class="com.zhangning.studentlogin.hibernate.EntityClass" not-null="true"/>
</class>
</hibernate-mapping>
那要怎样读取数据源呢?