https://www.w3cschool.cn/mybatis/mybatis-dyr53b5w.html

    pom.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>yubing</groupId>
    7. <artifactId>jdbc</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <build>
    10. <plugins>
    11. <plugin>
    12. <groupId>org.apache.maven.plugins</groupId>
    13. <artifactId>maven-compiler-plugin</artifactId>
    14. <configuration>
    15. <source>8</source>
    16. <target>8</target>
    17. </configuration>
    18. </plugin>
    19. </plugins>
    20. </build>
    21. <dependencies>
    22. <dependency>
    23. <groupId>mysql</groupId>
    24. <artifactId>mysql-connector-java</artifactId>
    25. <version>5.1.47</version>
    26. <scope>runtime</scope>
    27. </dependency>
    28. <dependency>
    29. <groupId>junit</groupId>
    30. <artifactId>junit</artifactId>
    31. <version>4.12</version>
    32. <scope>test</scope>
    33. </dependency>
    34. <dependency>
    35. <groupId>org.springframework</groupId>
    36. <artifactId>spring-jdbc</artifactId>
    37. <version>5.2.12.RELEASE</version>
    38. </dependency>
    39. </dependencies>
    40. </project>

    db.properties

    1. db.user=test
    2. db.password=
    3. db.url=jdbc:mysql://localhost:3306/test?useSSL=no
    4. db.driverClass=com.mysql.jdbc.Driver

    公用类Db.java

    1. import org.springframework.jdbc.core.JdbcTemplate;
    2. import org.springframework.jdbc.datasource.DriverManagerDataSource;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.util.Properties;
    6. public class Db {
    7. public static JdbcTemplate getJdbcTemplate() {
    8. // 创建Properties类对象
    9. Properties properties = new Properties();
    10. try {
    11. // 读取properties属性文件到输入流中
    12. InputStream is = Db.class.getResourceAsStream("/db.properties");
    13. // 从输入流中加载属性列表
    14. properties.load(is);
    15. } catch (IOException e) {
    16. e.printStackTrace();
    17. }
    18. // 获取spring的JdbcTemplate
    19. DriverManagerDataSource dataSource = new DriverManagerDataSource();
    20. dataSource.setDriverClassName(properties.getProperty("db.driverClass"));
    21. dataSource.setUrl(properties.getProperty("db.url"));
    22. dataSource.setUsername(properties.getProperty("db.user"));
    23. dataSource.setPassword(properties.getProperty("db.password"));
    24. return new JdbcTemplate(dataSource);
    25. }
    26. }

    Demo测试类

    1. import org.springframework.jdbc.core.JdbcTemplate;
    2. import java.util.List;
    3. import java.util.Map;
    4. public class Demo04 {
    5. public static void main(String[] args) {
    6. String sql = null;
    7. JdbcTemplate jdbcTemplate = Db.getJdbcTemplate();
    8. // 查询
    9. sql = "select * from test";
    10. List<Map<String, Object>> users = jdbcTemplate.queryForList(sql);
    11. for (Map<String, Object> user : users) {
    12. System.out.println(user);
    13. }
    14. // 查询为字符串
    15. sql = "select name from test";
    16. List<String> list = jdbcTemplate.queryForList(sql, String.class);
    17. for (String str : list) {
    18. System.out.println(str);
    19. }
    20. // 增加记录
    21. sql = "insert into test set name=?";
    22. jdbcTemplate.update(sql, "dfa");
    23. //修改记录
    24. sql = "update test set name=? where id=?";
    25. jdbcTemplate.update(sql, "fdsdf", 3);
    26. }
    27. }

    示例代码可以参考
    链接:https://pan.baidu.com/s/198bZHeQS2o17p5oa8exSNA
    提取码:java