如何在TOMCAT中配置数据源通过JNDI访问测试操作步骤
1个回答
展开全部
给你两个 方法,建议用方法二,简单易懂,方法一就配置和依赖的jar包多些~一下方法基于spring
方法一:
1.相应的数据库jar包 放tomcat的lib里
2.在tomcat中的context.xml文件中配置数据源参数如(db2数据库):
<GlobalNamingResources>
<Resource auth ="Container" description="User database that can be updated and saved" factory= "org.apache.catalina.users.MemoryUserDatabaseFactory" name= "UserDatabase" pathname ="conf/tomcat-users.xml" type= "org.apache.catalina.UserDatabase" />
<Resource auth ="Container" driverClassName="com.ibm.db2.jcc.DB2Driver" factory= "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" maxActive= "20" maxIdel ="10" maxWait="1000" name= "xxx" password= "xxx" type ="javax.sql.DataSource" url= "xxxx" username ="xxx"/>
</GlobalNamingResources >
<ResourceLink name= "jdbc/xxx" global= "jdbc/xxx" type="javax.sql.DataSource" />
3.在项目配置中配置:JNDI名服务查找
<!-- JNDI数据源 -->
<jee:jndi-lookup id ="dataSource" jndi-name="jdbc/xxxx"
proxy-interface= "javax.sql.DataSource" lookup-on-startup="false" />
4.在配置中建立数据库操作对象与数据源的关系:
<bean id= "dalClient" class= "xxxxxxx" >
<property name ="resources" value= "classpath*:conf/sqlMap/sqlMap_*.xml" />
<!-- 定义默义的数据源 -->
<property name ="defaultDataSource" ref= "xxxxxx" />
</bean >
5.在web.xml中将3,4配置文件通过上下文加载到容器中
6.在代码中直接使用dalClient就可以了
方法二:
1.applicationContex.xml配置
<!-- 读取数据源配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源 -->
<bean id= "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}" ></property>
<property name="url" value="${jdbc.url}"></ property>
<property name="username" value="${jdbc.username}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<bean id= "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource"></ property>
</bean>
2.jdbc.properties配置内容:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xxx
jdbc.username=root
jdbc.password=admin
3.在web里把1配置加载到容器里~~
方法一:
1.相应的数据库jar包 放tomcat的lib里
2.在tomcat中的context.xml文件中配置数据源参数如(db2数据库):
<GlobalNamingResources>
<Resource auth ="Container" description="User database that can be updated and saved" factory= "org.apache.catalina.users.MemoryUserDatabaseFactory" name= "UserDatabase" pathname ="conf/tomcat-users.xml" type= "org.apache.catalina.UserDatabase" />
<Resource auth ="Container" driverClassName="com.ibm.db2.jcc.DB2Driver" factory= "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" maxActive= "20" maxIdel ="10" maxWait="1000" name= "xxx" password= "xxx" type ="javax.sql.DataSource" url= "xxxx" username ="xxx"/>
</GlobalNamingResources >
<ResourceLink name= "jdbc/xxx" global= "jdbc/xxx" type="javax.sql.DataSource" />
3.在项目配置中配置:JNDI名服务查找
<!-- JNDI数据源 -->
<jee:jndi-lookup id ="dataSource" jndi-name="jdbc/xxxx"
proxy-interface= "javax.sql.DataSource" lookup-on-startup="false" />
4.在配置中建立数据库操作对象与数据源的关系:
<bean id= "dalClient" class= "xxxxxxx" >
<property name ="resources" value= "classpath*:conf/sqlMap/sqlMap_*.xml" />
<!-- 定义默义的数据源 -->
<property name ="defaultDataSource" ref= "xxxxxx" />
</bean >
5.在web.xml中将3,4配置文件通过上下文加载到容器中
6.在代码中直接使用dalClient就可以了
方法二:
1.applicationContex.xml配置
<!-- 读取数据源配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源 -->
<bean id= "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}" ></property>
<property name="url" value="${jdbc.url}"></ property>
<property name="username" value="${jdbc.username}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<bean id= "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource"></ property>
</bean>
2.jdbc.properties配置内容:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xxx
jdbc.username=root
jdbc.password=admin
3.在web里把1配置加载到容器里~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询