Spring--连接数据库
Spring提供了JdbcTemplate模板类
依赖的引入:
i. Spring-context
ii. Spring-jdbc
iii. Mysql
iv. dbcp(连接池)
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-context</artifactId> |
| <version>5.3.20</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-jdbc</artifactId> |
| <version>5.3.30</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>mysql</groupId> |
| <artifactId>mysql-connector-java</artifactId> |
| <version>8.0.29</version> |
| </dependency> |
| |
| <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 |
| |
| |
| |
| |
| |
| 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.bboy.dao.impl; |
| |
| import com.bboy.dao.StudentDao; |
| import com.bboy.pojo.Student; |
| import org.springframework.jdbc.core.JdbcTemplate; |
| import org.springframework.jdbc.core.RowMapper; |
| import org.springframework.jdbc.core.support.JdbcDaoSupport; |
| import org.springframework.stereotype.Component; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.stereotype.Repository; |
| import org.springframework.stereotype.Service; |
| |
| import javax.annotation.Resources; |
| 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<>(); |
| |
| 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")); |
| stu.setPassword(rs.getString("password")); |
| 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> |
| |
| <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> |
| <property name="dataSource" ref="dataSource"/> |
| </bean> |
| |
| <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; |
| |
| |
| |
| |
| |
| |
| 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); |
| } |
| } |
| |
运行结果

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本