jdbc

controller

  1. @RestController
  2. public class JDBCController {
  3. @Autowired
  4. JdbcTemplate jdbcTemplate;
  5. @GetMapping("/userList")
  6. public List<Map<String, Object>> userList() {
  7. String s = "select * from user";
  8. List<Map<String, Object>> maps = jdbcTemplate.queryForList(s);
  9. return maps;
  10. }
  11. @GetMapping("/addUser")
  12. public String addUser() {
  13. String s = "insert into user values (4,'hah')";
  14. jdbcTemplate.update(s);
  15. return "true";
  16. }
  17. }

application.yml

  1. spring:
  2. datasource:
  3. username: root
  4. password: 123456
  5. url: jdbc:mysql://1.117.202.163:3306/mybatis1?useUnicode=true&characterEncoding=utf-8
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. filters: stat,wall,log4j

image.png

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.5.1</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.haifeng</groupId>
  12. <artifactId>springboot04</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot04</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-jdbc</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>mysql</groupId>
  30. <artifactId>mysql-connector-java</artifactId>
  31. <scope>runtime</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.projectlombok</groupId>
  35. <artifactId>lombok</artifactId>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>log4j</groupId>
  45. <artifactId>log4j</artifactId>
  46. <version>1.2.12</version>
  47. </dependency>
  48. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  49. <dependency>
  50. <groupId>com.alibaba</groupId>
  51. <artifactId>druid</artifactId>
  52. <version>1.2.6</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-web</artifactId>
  57. </dependency>
  58. </dependencies>
  59. <build>
  60. <plugins>
  61. <plugin>
  62. <groupId>org.springframework.boot</groupId>
  63. <artifactId>spring-boot-maven-plugin</artifactId>
  64. <configuration>
  65. <excludes>
  66. <exclude>
  67. <groupId>org.projectlombok</groupId>
  68. <artifactId>lombok</artifactId>
  69. </exclude>
  70. </excludes>
  71. </configuration>
  72. </plugin>
  73. </plugins>
  74. </build>
  75. </project>