准备数据
create database if not exists db_spring;
use db_spring;
drop table if exists tb_user;
create table if not exists tb_user
(
id int primary key auto_increment,
name varchar(10) not null unique,
age int,
id_card varchar(10)
);
insert into tb_user(name, age, id_card)
values ('张三', 23, '10001'),
('李四', 18, '10002'),
('王五', 34, '10003'),
('赵六', 45, '10004');
select * from tb_user;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
pom.xml
文件中引用需要的库
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>6.0.12version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>com.mysqlgroupId>
<artifactId>mysql-connector-jartifactId>
<version>8.0.33version>
dependency>
dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
准备好dao层接口和service层接口和实现类
- dao层
package com.test.dao;
public interface UserDao {
void selectAll();
void selectById();
}
- service层
package com.test.service;
public interface UserService {
void selectAll();
void selectById();
}
package com.test.service.impl;
import com.test.dao.UserDao;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void selectAll() {
userDao.selectAll();
}
@Override
public void selectById() {
userDao.selectById();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
准备好 jdbc.properties
和 user.properties
这里分开写,是为了练习加载多个配置文件,所以需要再resources
资源文件中新建这两个配置文件
编写Druid的jdbcConfig配置类
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
编写spring的配置类SpringConfig
package com.test.config;
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan({"com.test.dao", "com.test.service"})
@PropertySource({"classpath:user.properties", "classpath:jdbc.properties"})
@Import(JdbcConfig.class)
public class SpringConfig {
}
编写Dao层的实现类的逻辑
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
private DataSource dataSource;
@Value("${id}")
private int id;
@Override
public void selectAll() {
try {
Connection connection = dataSource.getConnection();
String sql = "select * from tb_user";
PreparedStatement prepareStatement = connection.prepareStatement(sql);
ResultSet resultSet = prepareStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String idCard = resultSet.getString("id_card");
int age = resultSet.getInt("age");
System.out.println("id:" + id + " , name:" + name + " , age:" + age + " , idCard:" + idCard);
}
resultSet.close();
prepareStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void selectById() {
try {
Connection connection = dataSource.getConnection();
String sql = "select * from tb_user where id = ?";
PreparedStatement prepareStatement = connection.prepareStatement(sql);
prepareStatement.setInt(1, id);
ResultSet resultSet = prepareStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String idCard = resultSet.getString("id_card");
int age = resultSet.getInt("age");
System.out.println("id:" + id + " , name:" + name + " , age:" + age + " , idCard:" + idCard);
}
resultSet.close();
prepareStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
测试类
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = ctx.getBean(UserService.class);
userService.selectAll();
System.out.println("====== selectById ======");
userService.selectById();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
参考文献
1. 黑马程序员SSM框架教程