smile~
Create a database
1 CREATE TABLEStudent (2Sidint( One) not NULLAuto_increment,3Namevarchar(255)DEFAULT NULL,4Ageint( One)DEFAULT NULL,5 PRIMARY KEY(SID)6) ENGINE=InnoDB auto_increment=3 DEFAULTCHARSET=UTF8 COMMENT='Student Table';7 8 9 INSERT intoStudentVALUES(1,'Flowers thousand Bones',1462),(2,'The white child draws',511);
Stuservice.java
1Package Com.dc.service;2 3 Importjava.util.List;4 ImportJava.util.Map;5 6 Importcom.dc.entity.Student;7 Importcom.dc.jdbc.JDBCUtils;8 9 Public classStuservice {Ten Public intUpdatestuinfo (map<string, object> Map)throwsexception{ One inti = jdbcutils.update ("update student set age = age+1 where Name=#{name}", map); A returni; - } - PublicList<map<string,object>> Getstudentinfo ()throwsexception{ thelist<map<string,object>> stulist = jdbcutils.selectlist ("SELECT * FROM Student"); - returnstulist; - } - PublicList<student> GetStudentInfo2 (map<string,object> Map)throwsexception{ +list<student> stulist = jdbcutils.selectlist ("select * from Student where name=#{n}", Map,student.class); - returnstulist; + } A PublicStudent GetOne (list<object> condition)throwsexception{ atStudent stu = Jdbcutils.selectone ("select * from Student where name=? and Sid =? ", condition, Student.class); - returnStu; - } -}
The separate Main method uses:
1 Packagecom.dc.test;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 8 Importcom.dc.entity.Student;9 ImportCom.dc.jdbc.JDBCProxy;Ten ImportCom.dc.service.StuService; One A Public classMyTest { - Public Static voidMain (string[] args) { -Stuservice $stuService =NewStuservice (); theJdbcproxy Proxystuservice =jdbcproxy.getinstance (); - Proxystuservice.settarget ($stuService); -Stuservice stuservice=(Stuservice) proxystuservice.gettarget (); - + Try { -List<map<string,object>> stulist =stuservice.getstudentinfo (); + for(inti = 0; I < stulist.size (); i++) { ASystem.out.println (Stulist.get (i). Get ("name") + "age =" +stulist.get (i). Get ("age") + "year old"); at } - -map<string,object> map =NewHashmap<string,object>(); -Map.put ("name", "White painting"); -System.out.println ("---------Change the age of a white child and one year old---------------"); - stuservice.updatestuinfo (map); in - tolist<object> L =NewArraylist<object>(); +L.add ("White painting"); -L.add (2); theStudent s =Stuservice.getone (l); *System.out.println ("Age of the white child painted" +s.getage ()); $}Catch(Exception e) {Panax Notoginseng e.printstacktrace (); - } the } +}
Spring in conjunction with
Applicationcontext.xml Configuration
<bean id= "$stuService" class= "Com.dc.service.StuService" ></bean>
<bean id= "Jdbcproxy" class= "Com.dc.jdbc.JDBCProxy" >
<property name= "target" ref= "$stuService" ></property>
</bean>
<bean id= "Stuserviceproxy" factory-bean= "Jdbcproxy" factory-method= "Gettarget"/>
Packagecom.dc.test;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importcom.dc.entity.Student;ImportCom.dc.service.StuService; Public classBeantest { Public Static voidMain (string[] args) {ApplicationContext ac=NewClasspathxmlapplicationcontext ("Classpath:applicationContext.xml"); Stuservice Stuservice= (Stuservice) ac.getbean ("Stuserviceproxy"); Try{List<Map<String,Object>> stulist =Stuservice.getstudentinfo (); for(inti = 0; I < stulist.size (); i++) {System.out.println (Stulist.get (i). Get ("Name") + "age =" +stulist.get (i). Get ("age") + "year old"); } Map<String,Object> map =NewHashmap<string,object>(); Map.put ("Name", "White child painting"); System.out.println ("---------Change the age of a white child and one year old---------------"); Stuservice.updatestuinfo (map); List<Object> L =NewArraylist<object>(); L.add ("White child painting"); L.add (2); Student s=Stuservice.getone (L); System.out.println ("Age of the white child" after being modified +s.getage ()); } Catch(Exception e) {e.printstacktrace (); } }}
JDBC Reverse proxy framework (Smile) Usage notes