Java 连接到数据库的Spring应用程序

Java 连接到数据库的Spring应用程序,java,database,spring,Java,Database,Spring,任何人都可以告诉我如何使用spring应用程序连接到数据库。使用Hibernate作为ORM工具,并使用spring ORM支持,这将是一个很好的设计 另请参见: 使用Hibernate作为ORM工具,并使用Spring ORM支持,这将是一个很好的设计 另请参见: 首先,您必须在application context.xml中输入数据源 然后我们必须在context.xml中创建一个条目,并且必须在context.xml中提供数据库凭据和位置 我附上我的项目的两个文件 希望它能帮助

任何人都可以告诉我如何使用spring应用程序连接到数据库。

使用Hibernate作为ORM工具,并使用spring ORM支持,这将是一个很好的设计

另请参见:


    • 使用Hibernate作为ORM工具,并使用Spring ORM支持,这将是一个很好的设计

      另请参见:


      首先,您必须在application context.xml中输入数据源

      然后我们必须在context.xml中创建一个条目,并且必须在context.xml中提供数据库凭据和位置

      我附上我的项目的两个文件

      希望它能帮助你

      应用程序上下文.xml的代码

      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context">
      
              <bean id="messageSource"
               class="org.springframework.context.support.ResourceBundleMessageSource">
                      <property name="basenames">
                              <list>
                                      <value>report/cobReports_message</value>
                              </list>
                      </property>
              </bean>
      
              <context:component-scan
                      base-package="com.jpmc.am.architecture.emailaddrutil, com.jpmc.am.architecture.cob" />
      
              <bean id="multipartResolver"
               class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                      <property name="maxUploadSize" value="200000" />
              </bean>
      
      
              <bean id="sqlMapClientTemplate" 
               class="org.springframework.orm.ibatis.SqlMapClientTemplate">
                      <property name="sqlMapClient" ref="sqlMapClient" />
              </bean>
      
              <bean id="sqlMapClient" 
               class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                      <property name="configLocation"
                       value="classpath:sqlMap/cobSqlMapConfig.xml" />
                      <property name="dataSource" ref="cobDataSource" />
                      <property name="useTransactionAwareDataSource" value="true" />
                      <property name="sqlMapClientProperties">
                       <value>COB_SCHEMA=CLTAPPMGR //////// Database Schema Name</value>
                      </property>
              </bean>
      
              /// Data Source Name
              <bean id="cobDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="jndiName" value="java:comp/env/jdbc/cob" />
              </bean>
      </beans>
      
      <Context>
      
       <Resource name="jdbc/cob" auth="Container" type="javax.sql.DataSource" 
       factory="org.apache.commons.dbcp.BasicDataSourceFactory"
       driverClassName="oracle.jdbc.driver.OracleDriver"
       url="database location:databasename"
       username="username"
       password="password"
       />
      
      
      
      
      </Context>
      
      
      报告/COBU报告信息
      COB\u SCHEMA=cltapmgr////数据库架构名称
      ///数据源名称
      
      Context.xml的代码

      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context">
      
              <bean id="messageSource"
               class="org.springframework.context.support.ResourceBundleMessageSource">
                      <property name="basenames">
                              <list>
                                      <value>report/cobReports_message</value>
                              </list>
                      </property>
              </bean>
      
              <context:component-scan
                      base-package="com.jpmc.am.architecture.emailaddrutil, com.jpmc.am.architecture.cob" />
      
              <bean id="multipartResolver"
               class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                      <property name="maxUploadSize" value="200000" />
              </bean>
      
      
              <bean id="sqlMapClientTemplate" 
               class="org.springframework.orm.ibatis.SqlMapClientTemplate">
                      <property name="sqlMapClient" ref="sqlMapClient" />
              </bean>
      
              <bean id="sqlMapClient" 
               class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                      <property name="configLocation"
                       value="classpath:sqlMap/cobSqlMapConfig.xml" />
                      <property name="dataSource" ref="cobDataSource" />
                      <property name="useTransactionAwareDataSource" value="true" />
                      <property name="sqlMapClientProperties">
                       <value>COB_SCHEMA=CLTAPPMGR //////// Database Schema Name</value>
                      </property>
              </bean>
      
              /// Data Source Name
              <bean id="cobDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="jndiName" value="java:comp/env/jdbc/cob" />
              </bean>
      </beans>
      
      <Context>
      
       <Resource name="jdbc/cob" auth="Container" type="javax.sql.DataSource" 
       factory="org.apache.commons.dbcp.BasicDataSourceFactory"
       driverClassName="oracle.jdbc.driver.OracleDriver"
       url="database location:databasename"
       username="username"
       password="password"
       />
      
      
      
      
      </Context>
      

      首先,您必须在application context.xml中输入数据源

      然后我们必须在context.xml中创建一个条目,并且必须在context.xml中提供数据库凭据和位置

      我附上我的项目的两个文件

      希望它能帮助你

      应用程序上下文.xml的代码

      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context">
      
              <bean id="messageSource"
               class="org.springframework.context.support.ResourceBundleMessageSource">
                      <property name="basenames">
                              <list>
                                      <value>report/cobReports_message</value>
                              </list>
                      </property>
              </bean>
      
              <context:component-scan
                      base-package="com.jpmc.am.architecture.emailaddrutil, com.jpmc.am.architecture.cob" />
      
              <bean id="multipartResolver"
               class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                      <property name="maxUploadSize" value="200000" />
              </bean>
      
      
              <bean id="sqlMapClientTemplate" 
               class="org.springframework.orm.ibatis.SqlMapClientTemplate">
                      <property name="sqlMapClient" ref="sqlMapClient" />
              </bean>
      
              <bean id="sqlMapClient" 
               class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                      <property name="configLocation"
                       value="classpath:sqlMap/cobSqlMapConfig.xml" />
                      <property name="dataSource" ref="cobDataSource" />
                      <property name="useTransactionAwareDataSource" value="true" />
                      <property name="sqlMapClientProperties">
                       <value>COB_SCHEMA=CLTAPPMGR //////// Database Schema Name</value>
                      </property>
              </bean>
      
              /// Data Source Name
              <bean id="cobDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="jndiName" value="java:comp/env/jdbc/cob" />
              </bean>
      </beans>
      
      <Context>
      
       <Resource name="jdbc/cob" auth="Container" type="javax.sql.DataSource" 
       factory="org.apache.commons.dbcp.BasicDataSourceFactory"
       driverClassName="oracle.jdbc.driver.OracleDriver"
       url="database location:databasename"
       username="username"
       password="password"
       />
      
      
      
      
      </Context>
      
      
      报告/COBU报告信息
      COB\u SCHEMA=cltapmgr////数据库架构名称
      ///数据源名称
      
      Context.xml的代码

      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context">
      
              <bean id="messageSource"
               class="org.springframework.context.support.ResourceBundleMessageSource">
                      <property name="basenames">
                              <list>
                                      <value>report/cobReports_message</value>
                              </list>
                      </property>
              </bean>
      
              <context:component-scan
                      base-package="com.jpmc.am.architecture.emailaddrutil, com.jpmc.am.architecture.cob" />
      
              <bean id="multipartResolver"
               class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                      <property name="maxUploadSize" value="200000" />
              </bean>
      
      
              <bean id="sqlMapClientTemplate" 
               class="org.springframework.orm.ibatis.SqlMapClientTemplate">
                      <property name="sqlMapClient" ref="sqlMapClient" />
              </bean>
      
              <bean id="sqlMapClient" 
               class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                      <property name="configLocation"
                       value="classpath:sqlMap/cobSqlMapConfig.xml" />
                      <property name="dataSource" ref="cobDataSource" />
                      <property name="useTransactionAwareDataSource" value="true" />
                      <property name="sqlMapClientProperties">
                       <value>COB_SCHEMA=CLTAPPMGR //////// Database Schema Name</value>
                      </property>
              </bean>
      
              /// Data Source Name
              <bean id="cobDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="jndiName" value="java:comp/env/jdbc/cob" />
              </bean>
      </beans>
      
      <Context>
      
       <Resource name="jdbc/cob" auth="Container" type="javax.sql.DataSource" 
       factory="org.apache.commons.dbcp.BasicDataSourceFactory"
       driverClassName="oracle.jdbc.driver.OracleDriver"
       url="database location:databasename"
       username="username"
       password="password"
       />
      
      
      
      
      </Context>
      
      
      
      查看JdbcTemplate,这可能是最快、最简单的路线

      同意上面的观点,除了基本内容外,还可以考虑ORM解决方案。

      查看JdbcTemplate,这可能是最快和最简单的方法


      同意上面的观点,除了基本内容外,还可以考虑ORM解决方案。

      我为您编辑了上下文。如果您不使用这些XSD名称空间(task、util、p、jdbc、j2ee等),则无需添加它们!我为你编辑了上下文。如果您不使用这些XSD名称空间(task、util、p、jdbc、j2ee等),则无需添加它们!为什么每个人都链接到古代版本?将2.0链接替换为3.0版本为什么每个人都链接到古代版本?将2.0链接替换为3.0版本