注意:不要忘记在主安装文件夹中将“mysql-connector-java-5.1.36.jar”复制到Tomcat的“lib”子文件夹中。
第一:添加按照你“的pom.xml”文件相关:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
二:在“Web应用程序”根文件夹类似下面的图片创建META-INF文件夹和“context.xml的”文件:

三:添加下面的代码片断在 “context.xml中” 文件:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000"
username="DatabaseUsername" password="DatabasePasssword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/DatabaseName"/>
</Context>
第四:在Spring上下文配置文件创建以下豆:
@Bean
public DataSource dataSource() {
JndiDataSourceLookup dataSource = new JndiDataSourceLookup();
dataSource.setResourceRef(true);
return dataSource.getDataSource("jdbc/DatabaseName");
}
注:“JDBC /数据库名称”是“名”属性,我们在“context.xml中”文件已经添加。
是在这里回答:http://stackoverflow.com/questions/24941829/how-to-create-jndi-context-in-spring-boot-with-embedded-tomcat-container/26005740#26005740 – nekperu15739
这里回答:http: //stackoverflow.com/questions/24941829/how-to-create-jndi-context-in-spring-boot-with-embedded-tomcat-container/26005740#26005740 – nekperu15739