Spring入门第二十四课

时间:2023-12-21 18:54:14

Spring对JDBC的支持

直接看代码:

db.properties

jdbc.user=root
jdbc.password=logan123
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/selective-courses-system jdbc.initPoolSize=5
jdbc.maxPoolSize=10

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 配置C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean> <!-- 配置Spring的JDBCTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
package logan.study.spring.jdbc;

public class Student {
String student_id;
String student_name;
String card_id;
String student_class;
String sex;
String password;
String perovince;
String address;
String tel;
String interests;
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getCard_id() {
return card_id;
}
public void setCard_id(String card_id) {
this.card_id = card_id;
}
public String getStudent_class() {
return student_class;
}
public void setStudent_class(String student_class) {
this.student_class = student_class;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPerovince() {
return perovince;
}
public void setPerovince(String perovince) {
this.perovince = perovince;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getInterests() {
return interests;
}
public void setInterests(String interests) {
this.interests = interests;
}
@Override
public String toString() {
return "Student [student_id=" + student_id + ", student_name=" + student_name + ", card_id=" + card_id
+ ", student_class=" + student_class + ", sex=" + sex + ", password=" + password + ", perovince="
+ perovince + ", address=" + address + ", tel=" + tel + ", interests=" + interests + "]";
} }
package logan.study.spring.jdbc;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.sql.DataSource; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; public class JDBCTest { private ApplicationContext ctx = null;
private JdbcTemplate jdbcTemplate; {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
} @Test
public void testDataSource() throws SQLException {
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
/**
* 获取单个列的值,或者做统计查询
*/
@Test
public void testQueryForObject2(){
String sql = "SELECT count(*) FROM student_info";
long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
} /**
* 查到实体类的集合
*/
@Test
public void testQueryForList(){
String sql = "SELECT student_id,student_name,card_id,class student_class,sex,password,perovince,tel,interests from student_info where student_id > ?";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
List<Student> students = jdbcTemplate.query(sql, rowMapper,5);
System.out.println(students);
}
/**
* 从数据库中获取一条记录,实际得到对应的一个对象
* 注意不是调用queryForObject(String sql,Class<Student> requiredType,Object... args)方法!
* 而是调用queryForObject(String sql,RowMapper<Student> rowMapper,Object... args)方法!
* 1.其中的RowMapper指定如何去映射结果集的行,常用的实现类为BeanPropertyRowMapper
* 2.使用SQL中列的别名完成列名和类的属性名的映射。例如class student_class
* 3.不支持级联框架,JdbcTemplate到底是一个JDBC的小工具,而不是ORM框架。
*/
@Test
public void testQueryForObject(){
String sql = "SELECT student_id,student_name,card_id,class student_class,sex,password,perovince,tel,interests from student_info where student_id=?";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
Student student = jdbcTemplate.queryForObject(sql, rowMapper,1);
System.out.println(student);
}
/**
* 执行批量更新,批量INSERT,UPDATE,DELETE
*
*/
@Test
public void testBatchUpdate(){
String sql = "INSERT INTO student_info (student_id,student_name) VALUES(?,?)";
List<Object[]> pss = new ArrayList<Object[]>();
pss.add(new Object[]{"009","AA"});
pss.add(new Object[]{"010","BB"});
pss.add(new Object[]{"011","CC"});
pss.add(new Object[]{"012","DD"});
pss.add(new Object[]{"013","EE"});
pss.add(new Object[]{"014","FF"});
pss.add(new Object[]{"015","GG"});
jdbcTemplate.batchUpdate(sql, pss); }
/**
* 执行INSERT,UPDATE,DELETE
*/
@Test
public void testUpdate(){
String sql = "UPDATE student_info SET student_name = ? WHERE student_id = ?";
jdbcTemplate.update(sql,"小黑","002");
} }

不推荐使用继承JdbcDaoSupport