1.pom.xml中导入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
2.在Application.properties中指定连接池参数:
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=123456
#以下参数可不设置
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.hikari.idle-timeout=60000
#spring.datasource.hikari.maximum-pool-size=30
#spring.datasource.hikari.minimum-idle=10
3.编写实体类(属性和表字段对应,这里jdbc支持表字段带下划线时自动专为驼峰命名法的属性字段):
package com.lxj.domain;
import java.util.Date;
public class User {
private Integer id;
private String userName;
private String passWord;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private Date created;
private Date updated;
private String note;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
", note='" + note + '\'' +
'}';
}
}
4.编写dao:
package com.lxj.dao;
import com.lxj.domain.User;
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 JdbcDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> findAll(){
//BeanPropertyRowMapper : 可以把同名字段赋值给属性
return jdbcTemplate.query("select * from tb_user",new BeanPropertyRowMapper<>(User.class));
}
}
@Repository注解便用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可。
5.写一个test类进行测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class JdbcDaoTest extends TestCase {
@Autowired
private JdbcDao jdbcDao;
@Test
public void findAll() {
jdbcDao.findAll().forEach(user -> {
System.out.println(user);
});
}
}
@RunWith(SpringRunner.class)的作用表明Test测试类要使用注入的类,比如@Autowired注入的类,有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效。
@SpringBootTest替代了spring-test中的@ContextConfiguration注解,目的是加载ApplicationContext,启动spring容器。使用@SpringBootTest时并没有像@ContextConfiguration一样显示指定locations或classes属性,原因在于@SpringBootTest注解会自动检索程序的配置文件,检索顺序是从当前包开始,逐级向上查找被@SpringBootApplication或@SpringBootConfiguration注解的类。
在方法前加上 @Test , 则该方法即为测试方法,可以执行。
6.打印结果:
表结构: