对象关系映射(英语:Object Relational Mapping)
O - Object - 对象 - 1表customers、1个类Customer - 数据存在这里
R - Relational - 关系
M - Mapping - 映射 - 反射 - 反射类的设定的参数
通过rsmd.getColumnLabel(i + 1),做到不定列
创建/使用com.atguigu3.bean包
package com.atguigu3.bean;
import java.sql.Date;
/*
* ORM编程思想 (object relational mapping)
* 一个数据表对应一个java类
* 表中的一条记录对应java类的一个对象
* 表中的一个字段对应java类的一个属性
*
*/
public class Customer {
private int id;
private String name;
private String email;
private Date birth;
public Customer() {
super();
}
public Customer(int id, String name, String email, Date birth) {
super();
this.id = id;
this.name = name;
this.email = email;
this.birth = birth;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", birth=" + birth + "]";
}
}
Java类型 | SQL类型 |
---|---|
boolean | BIT |
byte | TINYINT |
short | SMALL .INT |
int | INTEGER |
long | BIGINT |
String | CHAR,VARCHAR, LONGVARCHAR |
byte array | BINARY,VAR BINARY |
java.sql.Date | DATE |
java.sql.Time | TIME |
package com.atguigu3.preparedstatement.crud;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.junit.Test;
import com.atguigu3.bean.Customer;
import com.atguigu3.util.JDBCUtils;
/**
*
* @Description 针对于customers表的通用的查询操作
* @author shkstart
* @throws Exception
* @date 上午10:23:40
*/
public Customer queryForCustomers(String sql,Object...args){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for(int i = 0;i < args.length;i++){
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();//prepareStatement通过executeQuery获得数据
ResultSetMetaData rsmd = rs.getMetaData();//获取结果集的元数据 :ResultSetMetaData
int columnCount = rsmd.getColumnCount();//通过ResultSetMetaData获取结果集中的列数
if(rs.next()){
Customer cust = new Customer();//一个Customer类对象
//处理结果集一行数据中的每一个列
for(int i = 0;i <columnCount;i++){
Object columValue = rs.getObject(i + 1);//获取列值
// String columnName = rsmd.getColumnName(i + 1);//获取sql结果的每个列的列名
String columnLabel = rsmd.getColumnLabel(i + 1);//获取sql结果的每个列的别名
//给cust对象指定的columnName属性,赋值为columValue:通过反射
Field field = Customer.class.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(cust, columValue);
}
return cust;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.closeResource(conn, ps, rs);
}
return null;
}
getColumnName(i + 1)
getColumnLabel(i + 1)
sql可以换名,getColumnName获取表的名,getColumnLabel可以获取表的名,也可以获取sql换过的名
return cust;//返回Customer对象,而且已经有数据了
package com.atguigu3.preparedstatement.crud;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.junit.Test;
import com.atguigu3.bean.Customer;
import com.atguigu3.util.JDBCUtils;
/**
*
* @Description 针对于Customers表的查询操作
* @author shkstart Email:shkstart@126.com
* @version
* @date 上午10:04:55
*
*/
public class CustomerForQuery {
@Test
public void testQueryForCustomers(){
String sql = "select id,name,birth,email from customers where id = ?";
Customer customer = queryForCustomers(sql, 13);
System.out.println(customer);
sql = "select name,email from customers where name = ?";
Customer customer1 = queryForCustomers(sql,"周杰伦");
System.out.println(customer1);
}