1、创建maven工程并添加依赖
<!-- Import dependency management from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<!-- 统一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--启动器构建web,包括RESTful,使用Spring MVC的应用程序。使用Tomcat作为默认嵌入式容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot集成jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok:用来消除Java类中的大量样板代码,产生干净,简洁且易于维护的Java类 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--SpingBoot集成junit测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、添加配置文件application.yml
# DB Configuration
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8
username: root
password: 123456
3、编写启动类JdbcApplication
package com.wedu.jdbc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JdbcApplication {
public static void main(String[] args) {
SpringApplication.run(JdbcApplication.class, args);
}
}
4、编写实体
package com.wedu.jdbc.pojo;
import lombok.Data;
import java.util.Date;
/**
* 用户信息实体
*/
@Data
public class UserInfo {
private Integer id;
private String username;
private String password;
private Integer age;
private Date regtime;
private String siteaddress;
}
5、编写持久层接口
package com.wedu.jdbc.dao;
import com.wedu.jdbc.pojo.UserInfo;
import java.util.List;
public interface IUserInfoDao {
List<UserInfo> findAll();
}
6、编写持久层接口实现类
package com.wedu.jdbc.dao.impl;
import com.wedu.jdbc.dao.IUserInfoDao;
import com.wedu.jdbc.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserInfoDaoImpl implements IUserInfoDao {
@Autowired
private JdbcTemplate jdbcTemplate;//Spring的JdbcTemplate是自动配置的,可直接使用
@Override
public List<UserInfo> findAll() {
return jdbcTemplate.query("select * from userinfo", new BeanPropertyRowMapper<UserInfo>(UserInfo.class));
}
}
注:JdbcTemplate是Spring MVC内置的对JDBC的一个封装,使用的数据源是HikariDataSource;RowMapper接口的实现类BeanPropertyRowMapper将获取的值封装到对象内。
7、编写测试类进行测试
package com.wedu.jdbc.dao;
import com.wedu.jdbc.JdbcApplication;
import com.wedu.jdbc.pojo.UserInfo;
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 java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = JdbcApplication.class)
public class UserInfoDaoTest {
@Autowired
private IUserInfoDao userInfoDao;
@Test
public void findAll() {
List<UserInfo> userinfos = userInfoDao.findAll();
userinfos.forEach(userInfo -> System.out.println(userInfo));
}
}
运行结果如下: