原教程: https://www.w3cschool.cn/wkspring/
spring JDBC框架
以查询student表为例
- 创建StudentDao接口,封装有关数据库的操作(增删改查以及设置DataSource)【此处DataSource是依赖注入的】
- 创建spring bean类(Student)
- 创建实现了RowMapper接口的Mapper类(StudentMapper),用于查询对象或者对象数组
- 创建实现了DAO接口的JDBCTemplate类(StudentJDBCTemplate)(包含DataSource和JdbcTemplate属性)
- beans.xml中设置org.springframework.jdbc.datasource.DriverManagerDataSource及StudentJDBCTemplate为bean, 并将DriverManagerDataSource注入到StudentJDBCTemplate中。
注意:
- JdbcTemplate类执行SQL语句
- 数据源的配置及使用(注入的方式)
- 导入类时不要导错包
- org.springframework.jdbc.core.JdbcTemplate
- javax.sql.DataSource
- org.springframework.jdbc.core.RowMapper
DriverManagerDataSource bean的设置是固定的,其属性及值参考下面的例子。
1 // StudentDao.java 2 public interface StudentDao { 3 public void setDataSource(DataSource ds); 4 public void create(String name, Integer age); 5 public Student getStudent(Integer id); 6 public List<Student> listStudents(); 7 public void delete(Integer id); 8 public void update(Integer id, Integer age); 9 } 10 11 // StudentJDBCTemplate.java 12 public class StudentJDBCTemplate implements StudentDao{ 13 private DataSource dataSource; 14 private JdbcTemplate JdbcTemplateObject; 15 16 @Override 17 public void setDataSource(DataSource ds) { 18 this.dataSource = ds; 19 this.JdbcTemplateObject = new JdbcTemplate(dataSource); 20 } 21 22 @Override 23 public void create(String name, Integer age) { 24 String sql = "insert into Student(name, age) values(?,?)"; 25 JdbcTemplateObject.update(sql, name, age); 26 System.out.println("Created Record Name = "+name+" Age = "+age); 27 } 28 29 @Override 30 public Student getStudent(Integer id) { 31 String sql = "select * from Student where id = ?"; 32 Student student = JdbcTemplateObject.queryForObject(sql, new Object[]{id}, new StudentMapper()); 33 return student; 34 } 35 36 @Override 37 public List<Student> listStudents() { 38 String sql = "select * from student"; 39 List<Student> students = JdbcTemplateObject.query(sql, new StudentMapper()); 40 return students; 41 } 42 43 @Override 44 public void delete(Integer id) { 45 String sql = "delete from student where id = ?"; 46 JdbcTemplateObject.update(sql, id); 47 System.out.println("Delete Record with ID = "+ id); 48 } 49 50 @Override 51 public void update(Integer id, Integer age) { 52 String sql = "update student set age=? where id = ?"; 53 JdbcTemplateObject.update(sql, age, id); 54 System.out.println("Update Record with ID = "+id); 55 } 56 } 57 58 // Student.java 59 public class Student { 60 private Integer age; 61 private String name; 62 private Integer id; 63 public Integer getAge() { 64 return age; 65 } 66 public void setAge(Integer age) { 67 this.age = age; 68 } 69 public String getName() { 70 return name; 71 } 72 public void setName(String name) { 73 this.name = name; 74 } 75 public Integer getId() { 76 return id; 77 } 78 public void setId(Integer id) { 79 this.id = id; 80 } 81 } 82 83 // StudentMapper.java 84 public class StudentMapper implements RowMapper<Student>{ 85 86 @Override 87 public Student mapRow(ResultSet rs, int rowNum) throws SQLException { 88 Student student = new Student(); 89 student.setId(rs.getInt("id")); 90 student.setName(rs.getString("name")); 91 student.setAge(rs.getInt("age")); 92 return student; 93 } 94 } 95 96 // MainApp.java 97 public class MainApp { 98 public static void main(String[] args) { 99 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 100 StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean("studentJDBCTemplate"); 101 System.out.println("----------插入记录---------"); 102 studentJDBCTemplate.create("Zara", 11); 103 studentJDBCTemplate.create("Nuha", 2); 104 studentJDBCTemplate.create("Ayan", 15); 105 System.out.println("----------查询所有student记录----------"); 106 List<Student> students = studentJDBCTemplate.listStudents(); 107 for (Student student : students) { 108 System.out.print("ID:"+student.getId()); 109 System.out.print(", Name:"+student.getName()); 110 System.out.println(", Age:"+student.getAge()); 111 } 112 System.out.println("----------更新编号为2的学生的年龄为20----------"); 113 studentJDBCTemplate.update(2, 20); 114 System.out.println("----------查询编号为2的学生的年龄----------"); 115 Student student = studentJDBCTemplate.getStudent(2); 116 System.out.print("ID:"+student.getId()); 117 System.out.print(", Name:"+student.getName()); 118 System.out.println(", Age:"+student.getAge()); 119 } 120 } 121 122 // beans.xml 123 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 124 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 125 <property name="url" value="jdbc:mysql://localhost:3306/test"/> 126 <property name="username" value="root"/> 127 <property name="password" value="usbw"/> 128 </bean> 129 130 <bean id="studentJDBCTemplate" class="com.springjdbc.StudentJDBCTemplate"> 131 <property name="dataSource" ref="dataSource"/> 132 </bean>
运行结果: ----------插入记录--------- Created Record Name = Zara Age = 11 Created Record Name = Nuha Age = 2 Created Record Name = Ayan Age = 15 ----------查询所有student记录---------- ID:1, Name:Zara, Age:11 ID:2, Name:Nuha, Age:2 ID:3, Name:Ayan, Age:15 ----------更新编号为2的学生的年龄为20---------- Update Record with ID = 2 ----------查询编号为2的学生的年龄---------- ID:2, Name:Nuha, Age:20