Java数据库编程(二)

接上一章——Java数据库编程(一),本章将讲解就Java程序对数据库的进一步访问。

数据库编程——持久层工具

本章一样是要用到前几章所提到的工具编程思想进行编程。
任务目标:
1.不需要写SQL语句,就能执行对数据库的操作;
2.自动执行相关SQL语句;
3.能将查询结果转换成类的对象。

数据库表和类的关系:
表是由记录组成的;记录是由若干字段组成的。
这里,记录是问题的关键。

记录由多个字段和字段的取值(这里可以看成是键值对)组成的;
类的对象是由多个成员和成员的取值(这里可以看成是键值对)组成的。

1.把表sys_student_info和类StudentInfo对应起来;(这里我们只需给一个普通的类和这个表对应,只是增加成员的get和set方法,覆盖了toString方法,和equals方法)
在这里插入图片描述

package com.mec.orm.model;

public class StudentInfo {
	private String stuId;
	private String name;
	private String id;
	private String nativeId;
	private String nationId;
	private String sdm;
	private String status;
	
	public StudentInfo() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStuId() {
		return stuId;
	}
	
	public String getNativeId() {
		return nativeId;
	}

	public void setNativeId(String nativeId) {
		this.nativeId = nativeId;
	}

	public String getNationId() {
		return nationId;
	}

	public void setNationId(String nationId) {
		this.nationId = nationId;
	}

	public String getSdm() {
		return sdm;
	}

	public void setSdm(String sdm) {
		this.sdm = sdm;
	}

	public void setStuId(String stuId) {
		this.stuId = stuId;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	/**
	 * 这里的相等比较,是通过比较 id的值;
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StudentInfo other = (StudentInfo) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "StudentInfo [id=" + id + ", stuId=" + stuId + ", name=" + name
				+ ", native=" + nativeId
				+ ", nation=" + nationId
				+ ", s_d_m=" + sdm
				+ ",status=" + status + "]";
	}
	
}

把表sys_student_info和类StudentInfo对应起来后,只要提供表名称和类,就应该能取出这个表中的所有记录(但是发现表sys_student_info和类StudentInfo中的字段名称和成员名称不完全一样,这个后面会进行处理);

下面我们在编写XML配置文件,这里只需将把类和表对应起来,把字段名称和类成员对应起来;
:这里我们只需将字段名和类成员名不同的以这样的形式写出来。

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
	<mapping class="com.mec.orm.model.StudentInfo" table="sys_student_info">
		<column name="nation" property="nationId"></column>
		<column name="native" property="nativeId"></column>
		<column name="s_d_m" property="sdm"></column>
	</mapping>
	<mapping class="com.mec.orm.model.SubjectInfo" table="sys_subject_info">
	</mapping>
</mappings>

再编写properties配置文件,这个主要负责的是数据库的连接信息(用户名,密码等)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/com_jyx_javase_20191119
user=root
password=123456

首先,我所说的访问数据库时,肯定不仅仅只是一张表,而是多表查询的问题。而表中又有多个字段,很多的信息。我们的着手点应该是先看一张表和一个类的对应关系,再看表里的字段名称和类成员的对应关系。最后才是多个表和多个类的对应关系。
现在编写一张表和一个类的对应关系TableClassDefinition类;

package com.mec.orm.core;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TableClassDefinition {
	private Class<?> klass;		//类
	private String table;		//表
	private Map<String, Property> propertiesMap;	//<成员名称,Property>
	
	public TableClassDefinition() {
	}
	
	List<Property> columnNameList() {		//获取成员名称的集合 ,所有的成员和字段的映射
		List<Property> columns = new ArrayList<Property>();
		
		for (String key : propertiesMap.keySet()) {
			columns.add(propertiesMap.get(key));
		}
		
		return columns;
	}
	
	public String columnList() {			//形成SQL语句
		StringBuffer str = new StringBuffer();
		
		boolean first = true;
		for (String key : propertiesMap.keySet()) {
			Property property = propertiesMap.get(key);
			str.append(first ? "" : ",");
			str.append(table).append(".").append(property.getColumn());
			first = false;
		}
		
		return str.toString();
	}
	
	public void setKlass(String className) {		//类的所有成员存入propertiesMap中
		try {
			this.klass = Class.forName(className);
			propertiesMap = new HashMap<String, Property>();
			
			Field[] fields = this.klass.getDeclaredFields();
			for (Field field : fields) {
				String propertyName = field.getName();
				Property property = new Property();
				property.setProperty(field);
				property.setColumn(propertyName);
				
				propertiesMap.put(propertyName, property);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void setColumn(String propertyName, String column) {		//处理名称,将类成员名称和字段名称不同的,统一设置为字段名称
		Property property = propertiesMap.get(propertyName);
		if (property == null) {
			return;
		}
		property.setColumn(column);
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public Class<?> getKlass() {
		return klass;
	}

	@Override
	public String toString() {		
		StringBuffer str = new StringBuffer();
		
		str.append("类:" + klass.getName()).append(" <=> 表:") .append(table);
		for (String key : propertiesMap.keySet()) {
			Property property = propertiesMap.get(key);
			if (property == null) {
				continue;
			}
			str.append("\n\t").append(property);
		}
		
		return str.toString();
	}
	
}

其次是编写成员和字段名称的对应关系Property类;

package com.mec.orm.core;

import java.lang.reflect.Field;

public class Property {
	private Field property;	//成员
	private String column;	//字段名
	
	public Property() {
	}

	public Field getProperty() {
		return property;
	}
	
	public String getPropertyName() {
		return property.getName();
	}

	public Class<?> getType() {
		return property.getType();
	}
	
	public void setProperty(Field property) {
		this.property = property;
	}

	public String getColumn() {
		return column;
	}

	public void setColumn(String column) {
		this.column = column;
	}
	
	@Override
	public String toString() {
		return "成员:" + property.getName() + " <=> 字段:" + column;
	}
	
}

现在才是开始编写多个表和多个类的对应关系的类;这里因为也没多少东西所以,我们就直接把从XML配置文件读取信息和设置信息的操作放在这个类中。

package com.mec.orm.core;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;

import com.mec.util.XMLParser;

public class TableClassFactory {
	private static final Map<String, TableClassDefinition> tableClassPool;
	static {
		tableClassPool = new HashMap<String, TableClassDefinition>();
	}
	
	public TableClassFactory() {
	}

	public static void scanTableClassMapping(String xmlPath) {		//XML解析获取xml文件中的值,并set到TableClassDefinition类和Property类中。
		new XMLParser() {
			@Override
			public void dealElement(Element element, int index) {
				String className = element.getAttribute("class");
				String tableName = element.getAttribute("table");
				
				TableClassDefinition tcd = new TableClassDefinition();
				tcd.setKlass(className);
				tcd.setTable(tableName);
				
				new XMLParser() {
					@Override
					public void dealElement(Element element, int index) {
						String propertyName = element.getAttribute("property");
						String columnName = element.getAttribute("name");
						tcd.setColumn(propertyName, columnName);
					}
				}.parseTag(element, "column");
				
				tableClassPool.put(className, tcd);
			}
		}.parseTag(XMLParser.getDocument(xmlPath), "mapping");
	}
	
	public static TableClassDefinition getTableClass(Class<?> klass) {
		return getTableClass(klass.getName());
	}
	
	public static TableClassDefinition getTableClass(String className) {
		return tableClassPool.get(className);
	}
	
}

完成一以上 几步,开始编写连接数据库的类;

package com.mec.orm.core;

import java.lang.reflect.Method;
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.mec.util.PropertiesParser;

public class MecDatabase {
	private static Connection connection;
	
	public MecDatabase() {
	}
	
	public static void loadDatabaseConfig(String configFile) 		//解析properties文件,建立连接
			throws ClassNotFoundException, SQLException {
		PropertiesParser propertiesParser = new PropertiesParser();
		propertiesParser.loadProperties(configFile);
		
		Class.forName(propertiesParser.value("driver"));
		connection = DriverManager.getConnection(propertiesParser.value("url"), 
				propertiesParser.value("user"), 
				propertiesParser.value("password"));
	}
	
	public static void loadOrmMapping(String mappingPath) {
		TableClassFactory.scanTableClassMapping(mappingPath);
	}
	
	public <T> List<T> list(Class<?> klass) {		
		List<T> result = new ArrayList<T>();		//对象集
		StringBuffer str = new StringBuffer("SELECT ");		//建立SQL语句
		
		TableClassDefinition tcd = TableClassFactory.getTableClass(klass);
		str.append(tcd.columnList()).append(" ")		
			.append("FROM ").append(tcd.getTable());
		List<Property> propertyList = tcd.columnNameList();		//存的是所有的成员和字段的映射
		
		try {
			PreparedStatement state = connection.prepareStatement(str.toString());		//建立SQL语句的预编译
			ResultSet rs = state.executeQuery();		//执行SQL语句,得到结果集(表)
			while (rs.next()) {
				try {
					@SuppressWarnings("unchecked")
					T value = (T) klass.newInstance();	//调用类的无参构造
					for (Property property : propertyList) {
						Object obj = rs.getObject(property.getColumn());
						setValue(klass, value, property, obj);
					}
					result.add(value);
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * @param klass	用来进行反射
	 * @param object		是要完成成员赋值的那个对象
	 * @param propertyName	成员名称
	 * @param value		成员的值
	 */
	private void setValue(Class<?> klass, Object object, Property property, Object value) {
		String name = property.getPropertyName();
		String methodName = "set" + name.substring(0, 1).toUpperCase()
				+ name.substring(1);
		try {
			Method method = klass.getDeclaredMethod(methodName, new Class<?>[] {property.getType()});
			method.invoke(object, new Object[] {value});		//反射机制调用方法
		} catch (Exception e) {
		}
	}
	
}

最后编写Test类,进行试验;

package com.mec.orm.test;

import java.sql.SQLException;
import java.util.List;

import com.mec.orm.core.MecDatabase;
import com.mec.orm.model.StudentInfo;
import com.mec.orm.model.SubjectInfo;

public class Test {

	public static void main(String[] args) {
		MecDatabase md = new MecDatabase();
		try {
			MecDatabase.loadDatabaseConfig("/mecOrm.properties");
			MecDatabase.loadOrmMapping("/morm.tcm.xml");
			
			List<StudentInfo> studentList = md.list(StudentInfo.class);
			for(StudentInfo student : studentList) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

结果:
在这里插入图片描述
这里我们就基本实现了我们的一个简易的 hibernate 框架。
我们只要给出类名称就可以得到其中的信息,我们只需改变配置文件,和编写对应的简单类就可以访问。
也就是说我们想再得到数据库里面任何一个表的信息,我只需根据数据库表里面的字段,先形成一个类,再把XML文件进行一个简单的配置,就可以得到这张表里的所有信息。

感谢mec铁血教主。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值