代码
package com.wkcto.jdbc.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.wkcto.jdbc.domain.Student;
public class Test01 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 准备一个List集合
List<Student> studentList = new ArrayList<>(); // JDK7的新特性
try {
// 1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2、获取连接
String url = "jdbc:mysql://localhost:3306/wkcto";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
// 3、获取数据库操作对象
String sql = "select id,name,birth from tbl_student";
ps = conn.prepareStatement(sql); // 这个时候会发送sql语句给DBMS,DBMS系统对sql语句进行编译。
// 4、执行sql
rs = ps.executeQuery();
// 5、处理查询结果集
while(rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
String birth = rs.getString("birth");
//封装javabean
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setBirth(birth);
// 将学生添加到集合
studentList.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
// 6、释放资源
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
System.out.println(studentList);
}
}
分析
JDBC开发有哪些缺点?
* JDBC开发尤其在处理结果集方面,代码非常繁琐、冗余(出现了大量的重复代码)
重复的从结果集中取数据,重复的将取出的数据赋值到对象属性上。
while(rs.next()){
String id = rs.getString("id"); // 重复的从结果集中取数据
String name = rs.getString("name");
String birth = rs.getString("birth");
//封装javabean
Student stu = new Student();
stu.setId(id); // 重复的set赋值
stu.setName(name);
stu.setBirth(birth);
// 将学生添加到集合
studentList.add(stu);
}
有没有这样的一段程序,可以自动从结果集中取出数据,可以自动的将取出
的数据set到对象属性上呢?
完全可以,我们程序员自己也可以写。不一定借助框架。
只不过这段代码需要编写反射机制的代码。
对以上代码进行封装,需要知道的前提条件是什么?
数据库表当中的字段名、java程序中类的属性名
假设现在你已经知道了:字段名和属性名...
tbl_student
id name birth
------------------------------
public class Student{
id
name
birth
}
//这是已知条件:
String[] columnNames = {"id","name","birth"};
String[] propertyNames = {"id","name","birth"};
String className = ".....Student";
// 封装这个代码
Class c = Class.forName(className);
List list;
while(rs.next()){
Object obj = c.newInstance(); // Student obj = new Student();
Object[] values = ....;
for(i....){
String value = rs.getString(columnNames[i]);
// 把以上的value放到数组当中...
values[i] = value;
}
for(i...){
String propertyName = propertyNames[i]; // id
// setId
String methodName = "set" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1);
Method method = c.getDeclaredMethod(methodName, String.class);
// 调用method
method.invoke(obj , values[i]); // obj.setId("1");
}
list.add(obj);
}
mybatis已将将上面的代码完成了。
使用mybatis之后,我们不再需要从结果集中取数据,把结果集的数据封装到java对象当中。
这个过程完全交给mybatis框架去完成。
* JDBC的第二缺点:
sql语句写到了java程序中。
在实际开发中为了让项目的执行速度提高,sql语句是需要不断优化的。
也就是说sql语句是不断的修改。
违背了开闭原则:OCP。
在mybatis当中sql语句可以在XML文件中配置。解决了这个问题。
mybatis就是封装了JDBC。简化了JDBC的编程。使用mybatis开发效率比JDBC要高很多。