spring-boot测试JNDI
环境
- tomcat9
- JDK8
- spring-boot 2.1.3
使用外置tomcat,tomcat9 配置 JNDI
https://blog.csdn.net/qq_26264237/article/details/90636085
项目结构
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
spring-boot配置文件
application.yml
spring:
datasource:
jndi-name: jdbc/TestDB
TestJndi2Application
@SpringBootApplication
public class TestJndi2Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(TestJndi2Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TestJndi2Application.class);
}
}
HelloController
@Controller
@RequestMapping("/hello")
public class HelloController {
@Autowired
DataSource dataSource;
@GetMapping("index")
@ResponseBody
Object index() throws Exception {
Connection conn = dataSource.getConnection();
Statement stt = conn.createStatement();
ResultSet rs = stt.executeQuery("SELECT id, name FROM t_hello");
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
while(rs.next()) {
Map<String, String> map = new HashMap<String, String>();
map.put("id", rs.getString(1));
map.put("name", rs.getString(2));
list.add(map);
}
conn.close();
return "" + list;
}
}