Spring DataSource JNDI与Tomcat示例

Welcome to the Spring DataSource JNDI Tomcat Example Tutorial. Earlier we saw how to implement database operations using Spring JDBC integration. However most of the time enterprise applications are deployed in a servlet container such as Tomcat, JBoss etc.

欢迎使用Spring DataSource JNDI Tomcat示例教程。 前面我们看到了如何使用Spring JDBC集成来实现数据库操作。 但是,大多数时候,企业应用程序都部署在servlet容器中,例如Tomcat,JBoss等。

Spring数据源 (Spring DataSource)

We know that DataSource with JNDI is the preferred way to achieve connection pooling and get benefits of container implementations. Today we will look how we can configure a Spring Web Application to use JNDI connections provided by Tomcat.

我们知道, 带有JNDI的DataSource是实现连接池并从容器实现中受益的首选方法。 今天,我们将研究如何配置Spring Web应用程序以使用Tomcat提供的JNDI连接。

For my example, I will use MySQL database server and create a simple table with some rows. We will create a Spring Rest web service that will return the JSON response with list of all the data in the table.

对于我的示例,我将使用MySQL数据库服务器并创建一个包含一些行的简单表。 我们将创建一个Spring Rest Web服务,该服务将返回JSON响应以及表中所有数据的列表。

数据库设置 (Database Setup)

CREATE TABLE `Employee` (
  `id` int(11) unsigned NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `role` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `Employee` (`id`, `name`, `role`)
VALUES
	(1, 'Pankaj', 'CEO'),
	(2, 'David', 'Manager');
commit;

Spring DataSource MVC项目 (Spring DataSource MVC Project)

Create a Spring MVC Project in the Spring Tool Suite so that our spring application skeleton code is ready. Once we will be done with our implementation, our project structure will look like below image.

在Spring Tool Suite中创建一个Spring MVC项目,以便准备好我们的Spring应用程序框架代码。 完成实施后,我们的项目结构将如下图所示。

Spring JDBC和Jackson依赖关系 (Spring JDBC and Jackson Dependencies)

We will have to add Spring JDBC, Jackson and MySQL Database driver as the dependencies in the pom.xml file. My final pom.xml file looks like below.

我们将不得不在pom.xml文件中添加Spring JDBC,Jackson和MySQL数据库驱动程序作为依赖项。 我的最终pom.xml文件如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.journaldev.spring</groupId>
	<artifactId>SpringDataSource</artifactId>
	<name>SpringDataSource</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>1.6</java-version>
		<org.springframework-version>4.0.2.RELEASE</org.springframework-version>
		<org.aspe
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值