Spring--连接数据库
Spring提供了JdbcTemplate模板类
依赖的引入:
i. Spring-context
ii. Spring-jdbc
iii. Mysql
iv. dbcp(连接池)
<!--spring基础依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<!--spring连接数据库-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.30</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.11.0</version>
</dependency>
创建实体类对象(表对应的对象)
package com.bboy.pojo;
import lombok.Data;
@Data
/*
* @类描述:
* @作者:秦帅
* @时间:2023/11/22 0022 17:39:54
*/
public class Student {
private int id ;
private String name ;
private String birth ;
private String sex ;
private String password ;
}
创建dao层的内容
i. StudentDao
package com.bboy.dao;
import com.bboy.pojo.Student;
import java.util.List;
public interface StudentDao {
public List<Student> listStudents();
}
ii. StudentDaoImpl 需要继承JdbcDaoSupport类
package com.neuedu.dao.impl;
import com.neuedu.dao.StudentDao;
import com.neuedu.pojo.Student;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Student> listStudents() {
List<Student> lists = new ArrayList<>();
//-使用jdbc模板执行sql语句
jdbcTemplate.query("select * from t_student", new RowMapper<Student>() {
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setBirth(rs.getString("birth"));
stu.setSex(rs.getString("sex"));
lists.add(stu);
return stu;
}
});
return lists;
}
}
核心配置文件中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/>
<property name="username" value="root"/>
<property name="password" value="127003"/>
</bean>
<!--将数据源配置到JdbcTemplate模板中-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--将JdbcTemplate注入到dao中-->
<bean id="studentDao" class="com.bboy.dao.impl.StudentDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
测试类
package com.bboy.demo;
import com.bboy.dao.StudentDao;
import com.bboy.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/22 0022 18:06:46
*/
public class Demo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao studentDao = (StudentDao) context.getBean("studentDao");
List<Student> stus = studentDao.listStudents();
System.out.println(stus);
}
}