Spring--整合JDBC===》JdbcTemplate
List
query(String sql, RowMapper rowMapper, @Nullable Object... args)
- String sql 执行的sql语句,可以使用占位符
- RowMapper
接口:
2.1 mapRow(ResultSet rs, int rowNum) : 查询出数据的每一行的映射
2.1.1 ResultSet rs :结果中存储的是一个行的数据
2.1.2 int rowNum:每一行的下标- Object... args: 可变参 :传递过来的参数,可以匹配sql中的占位符
依赖的引入:
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.20</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</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/23 0023 9:47:57
*/
public class Student {
private int id ;
private String name ;
private String birth ;
private String sex ;
private String password ;
}
创建dao层的内容
StudentDao
package com.bboy.dao;
import com.bboy.pojo.Student;
import java.util.List;
public interface StudentDao {
public Student getStudentById(int id);
public List<Student> listStudents();
public List<Student> listStudentsBySex(String sex);
public int updateNameById(String name,int id);
}
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 java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/23 0023 9:51:13
*/
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Student getStudentById(int id) {
String sql = "select * from t_student where id=?";
/**
* T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
* 1. String sql 执行的sql语句,可以使用占位符
* 2. RowMapper<T> 接口:
* 2.1 mapRow(ResultSet rs, int rowNum) : 查询出数据的每一行的映射
* 2.1.1 ResultSet rs :结果中存储的是一个行的数据
* 2.1.2 int rowNum:每一行的下标
* 3. Object... args: 可变参 :传递过来的参数,可以匹配sql中的占位符
*/
Student student = jdbcTemplate.queryForObject(sql, 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"));
return stu;
}
},id);
return student;
}
@Override
public List<Student> listStudents() {
//-:定义sql语句
String sql = "select * from t_student";
/**
* 执行jdbc
* <T> List<T> query(String sql, RowMapper<T> rowMapper)
* 1. String sql 执行的sql语句
* 2. RowMapper<T> 接口:
* 2.1 mapRow(ResultSet rs, int rowNum) : 查询出数据的每一行的映射
* 2.1.1 ResultSet rs :结果中存储的是一个行的数据
* 2.1.2 int rowNum:每一行的下标
*/
List<Student> students = jdbcTemplate.query(sql, 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"));
return stu;
}
});
return students;
}
@Override
public List<Student> listStudentsBySex(String sex) {
String sql = "select * from t_student where sex=?";
/**
* List<T> query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
* 1. String sql 执行的sql语句,可以使用占位符
* 2. RowMapper<T> 接口:
* 2.1 mapRow(ResultSet rs, int rowNum) : 查询出数据的每一行的映射
* 2.1.1 ResultSet rs :结果中存储的是一个行的数据
* 2.1.2 int rowNum:每一行的下标
* 3. Object... args: 可变参 :传递过来的参数,可以匹配sql中的占位符
*/
List<Student> students = jdbcTemplate.query(sql, 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"));
return stu;
}
},sex);
return students;
}
@Override
public int updateNameById(String name, int id) {
String sql = "update t_student set name=? where id=?";
/**
* int update(String sql, @Nullable Object... args)
* 1. String sql 执行的sql语句,可以使用占位符
* 2. Object... args: 可变参 :传递过来的参数,可以匹配sql中的占位符
*/
int res = jdbcTemplate.update(sql,name,id);
return res;
}
}
创建service层的内容
StudentService
package com.bboy.service;
import com.bboy.pojo.Student;
import java.util.List;
public interface StudentService {
public Student getStudentById(int id);
public List<Student> listStudents();
public List<Student> listStudentsBySex(String sex);
public int updateNameById(String name,int id);
}
StudentServiceImpl
package com.bboy.service.impl;
import com.bboy.dao.StudentDao;
import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import java.util.List;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/23 0023 9:50:15
*/
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public Student getStudentById(int id) {
return studentDao.getStudentById(id);
}
@Override
public List<Student> listStudents() {
return studentDao.listStudents();
}
@Override
public List<Student> listStudentsBySex(String sex) {
return studentDao.listStudentsBySex(sex);
}
@Override
public int updateNameById(String name, int id) {
return studentDao.updateNameById(name,id);
}
}
核心配置文件中进行配置
<?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>
<!--将studentDao注入到service中-->
<bean id="studentService" class="com.bboy.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
测试类
package com.bboy.demo;
import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/23 0023 10:23:26
*/
public class Demo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) context.getBean("studentService");
//-查询所有的数据
List<Student> students = studentService.listStudents();
System.out.println(students);
System.out.println("======================================================================================================================");
//-根据性别查询所有的数据
List<Student> students1 = studentService.listStudentsBySex("女");
System.out.println(students1);
System.out.println("======================================================================================================================");
//-根据Id查询指定的对象信息
Student student = studentService.getStudentById(2);
System.out.println(student);
System.out.println("======================================================================================================================");
//-更新数据
int res = studentService.updateNameById("汤姆",1);
if(res>0){
System.out.println("修改成功");
//-:查询出修改之后的数据
students = studentService.listStudents();
System.out.println(students);
System.out.println("======================================================================================================================");
}else{
System.out.println("修改失败");
System.out.println("======================================================================================================================");
}
}
}