package com.myhibernate;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.activation.MailcapCommandMap;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.model.Student;
public class MySession {
private Connection conection;
private SAXReader sr;
private File f;
private List pro_list=new ArrayList();
private List<Node> list;
private List<String> atrr_list=new ArrayList();
private MyTransaction mt;
private String url;
public MyTransaction getMt() {
return mt;
}
public void setMt(MyTransaction mt) {
this.mt = mt;
}
public Connection getConection() {
return conection;
}
public void setConection(Connection conection) {
this.conection = conection;
}
public void beginTransaction(){
if(this.mt==null){
this.mt =new MyTransaction();
this.url="src/com/model/student.hbm.xml";
}
}
public void save(Student s){
try {
//System.out.println(this.url);
this.ergodic(this.url);
this.qu();
} catch (Exception e) {
e.printStackTrace();
}
int n=0;
String ss="values(";
this.mt.sql+="insert into student(";
while(n<atrr_list.size()){
this.mt.sql+=atrr_list.get(n)+" ";
if(n< atrr_list.size()-1){
this.mt.sql+=",";
}
for(int i =0;i<pro_list.size();i++){
if(pro_list.get(i).toString().equals(atrr_list.get(n))){
ss+=atrr_list.get(i).toString();
if(i<pro_list.size()-1){
ss+=",";
}
//System.out.println(this.mt.sql);
}
}
n++;
}
this.mt.sql+=") ";
this.mt.sql+=ss;
this.mt.sql+=s.getAge()+",'"+s.getId()+"','"+s.getName()+"'";
this.mt.sql+=")";
//System.out.println("sql语句为"+this.mt.sql);
}
//" values("+s.getId()+"" +","+s.getName()+","+s.getAge()+");";
public MyTransaction getTransaction(){
if(this.mt!=null){
this.mt.setMs(this);
return this.mt;
}
else{
System.out.println("事务为空,请重新确认");
return null;
}
}
public void close(){
if(this.conection!=null)
try {
if(this.conection!=null)
this.conection.close();
this.conection=null;
} catch (SQLException e) {
e.printStackTrace();
}
this.conection=null;
}
public void ergodic(String path) throws Exception{
this.f = new File(path);
this.sr = new SAXReader();
Document doc = sr.read(f);
String s =" ";
//ergodicXML(doc.getRootElement(),s);
this.bianLi(doc);
}
private void ergodicXML(Element e,String s){
for(int i=0,size=e.nodeCount();i<size;i++){
Node n =e.node(i);
if(n instanceof Element){
Element nn = (Element)n;
System.out.println(s+n.getName()+"(标签)");
for(Iterator j =nn.attributeIterator();j.hasNext();){
Attribute a =(Attribute)j.next();
System.out.println(s+a.getName()+"="+a.getStringValue()+"(属性)");
}
ergodicXML((Element)n,s+" ");
}
}//dom的读取方法
}
@SuppressWarnings("unchecked")
public void bianLi(Document e){
list = e.selectNodes("/hibernate-mapping/class");
//System.out.println(list.get(0).getName());
for( Node n : list){
Node nn =n.selectSingleNode("//id");
pro_list.add(nn.valueOf("@name"));
}//xpath 读取方法
List<Node> _list =new ArrayList();
for(int i=0;i<list.size();i++){
_list =list.get(i).selectNodes("//property");
for(int j = 0 ;i <_list.size();i++){
System.out.println(_list.get(i).valueOf("@name"));
}
}
for(Object b : pro_list){
System.out.println(b.toString());
}
}
public void qu() throws Exception{
Class c =Class.forName("com.model.Student");
c.newInstance();
Method[] m=c.getMethods();
for(Method mm : m){
if(mm.getName().subSequence(0, 3).equals("set")){
atrr_list.add(mm.getName().substring(3));
// System.out.println(mm.getName().substring(3));
}//反射拿到属性
}
}
}