创建一个空的project
在project中创建一个module并在里面导入下面的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.29</version>
</dependency>
<!-- 数据库驱动和连接池-->
<dependency>
<groupId>name.remal.tools.test</groupId>
<artifactId>junit4</artifactId>
<version>1.26.140</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.29</version>
</dependency>
</dependencies>
创建一个实体类Student,并实现对应的setter,getter方法,创建空参构造器,有参构造器
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String classes;
private String score;
}
创建数据库
CREATE TABLE `students` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'id',
`age` int(3) NOT NULL COMMENT '年龄',
`name` varchar(255) NOT NULL COMMENT '姓名',
`gender` varchar(255) NOT NULL COMMENT '年级',
`class` varchar(255) NOT NULL COMMENT '班级',
`score` varchar(255) CHARACTER SET utf8 COLLATE utf8_german2_ci NOT NULL COMMENT '分数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
构建jdbc,properties配置文件
su.url=jdbc:mysql://localhost:3306/testforspring?useSSL=false
su.driver=com.mysql.jdbc.Driver
su.username=root
su.password=
创建三层架构进行数据库的访问.
此时我们不需要再在各个对象中new对象,spring中对象的创建我们交给IOC容器进行管理,由IOC容器统一帮我们进行管理.我们只需要声明就可
例如studentServiceImpl中引用studentDao接口下的实体类studentDaoImpl
只需要声明private studentDao studentDao即可,代码如下
/**
* @author haustsusu
* @create 2023-08-29-19:20
*/
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public List<Student> getStudent(String sql) {
List<Student> students = studentDao.getStudents(sql);
return students;
}
}
此时的对象还无法完全交给IOC容器进行管理
需要在配置文件中对各个对象之间的进行依赖注入DI的操作
重点如下
<!-- 指定配置文件位置-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 将配置文件的值注入到datasource中-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${su.url}"></property>
<!-- 注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<!-- name中的值是driverClassName不是driver-->
<property name="driverClassName" value="${su.driver}"></property>
<property name="username" value="${su.username}"></property>
<property name="password" value="${su.password}"></property>
</bean>
<!--将DataSource注入到jdbcTemplate中-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">
</property>
</bean>
<!-- 将对象按照三层架构顺序依次注入-->
<!-- 注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<!-- 下列三行代码中无法讲jdbcTemplate注入到StudentDaoImpl中去,原因是在StudentDaoImpl中没有setter方法-->
<!-- <bean id="studentDao" class="com.fys.su.Dao.DaoImpl.StudentDaoImpl">-->
<!-- <property name="jdbcTemplate" ref="jdbcTemplate"></property>-->
<!-- </bean>-->
<bean id="studentDao" class="com.fys.su.Dao.DaoImpl.StudentDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="studentService" class="com.fys.su.Service.ServiceImpl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
<bean id="queryStudents" class="com.fys.su.Controller.QueryStudents">
<property name="studentService" ref="studentService"></property>
</bean>
注意,在注入前要检查被注入的类中是否声明了需要注入的对象的setter方法