jdbcTemplate 引入
<!-- jdbctemplate --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
配置數據源信息
spring.datasource.url=jdbc:mysql://XX.XXX.XXX.XX:3306/XXXX spring.datasource.username=root spring.datasource.password=XXXX spring.datasource.driver-class-name=com.mysql.jdbc.Driver
編寫service
package com.tzp.jdbc.service; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; @Service public class UserServiceImpl { @Autowired private JdbcTemplate jdbcTemplate; public List<Map<String, Object>> queryUsers(){ List<Map<String, Object>> list = jdbcTemplate.queryForList(" select * from t_user "); return list; } }
測試
package com.tzp.jdbc; import java.util.List; import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.tzp.jdbc.service.UserServiceImpl; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootJdbcApplicationTests { @Autowired private UserServiceImpl impl; @Test public void contextLoads() { List<Map<String, Object>> list = impl.queryUsers(); System.out.println(list.get(0).get("user_name")); } }
測試結果
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.4.RELEASE) 2019-05-05 23:01:20.219 INFO 11236 --- [ main] c.t.jdbc.SpringbootJdbcApplicationTests : Starting SpringbootJdbcApplicationTests on DESKTOP-J6HT18A with PID 11236 (started by tangzengping in D:\eclipse-workspace\springboot-workspace\springboot-jdbc) 2019-05-05 23:01:20.219 INFO 11236 --- [ main] c.t.jdbc.SpringbootJdbcApplicationTests : No active profile set, falling back to default profiles: default Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 2019-05-05 23:01:24.356 INFO 11236 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-05-05 23:01:24.670 WARN 11236 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration) 2019-05-05 23:01:25.623 INFO 11236 --- [ main] c.t.jdbc.SpringbootJdbcApplicationTests : Started SpringbootJdbcApplicationTests in 5.845 seconds (JVM running for 8.654) 2019-05-05 23:01:26.398 INFO 11236 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2019-05-05 23:01:26.429 WARN 11236 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation. 2019-05-05 23:01:28.804 INFO 11236 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 張三 2019-05-05 23:01:28.965 INFO 11236 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2019-05-05 23:01:29.028 INFO 11236 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2019-05-05 23:01:29.075 INFO 11236 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.