Smile~
创建数据库
1 CREATE TABLE student ( 2 sid int(11) NOT NULL AUTO_INCREMENT, 3 name varchar(255) DEFAULT NULL, 4 age int(11) DEFAULT NULL, 5 PRIMARY KEY (sid) 6 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT=‘学生表‘; 7 8 9 INSERT INTO student VALUES (1,‘花千骨‘,1462),(2,‘白子画‘,511);
StuService.java
1 package com.dc.service; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import com.dc.entity.Student; 7 import com.dc.jdbc.JDBCUtils; 8 9 public class StuService { 10 public int updateStuInfo(Map<String, Object> map) throws Exception{ 11 int i = JDBCUtils.update("update student set age = age+1 where name=#{name}", map); 12 return i; 13 } 14 public List<Map<String,Object>> getStudentInfo() throws Exception{ 15 List<Map<String,Object>> stuList = JDBCUtils.selectList("select * from student"); 16 return stuList; 17 } 18 public List<Student> getStudentInfo2(Map<String,Object> map) throws Exception{ 19 List<Student> stuList = JDBCUtils.selectList("select * from student where name=#{n}",map,Student.class); 20 return stuList; 21 } 22 public Student getOne(List<Object> condition) throws Exception{ 23 Student stu = JDBCUtils.selectOne("select * from student where name=? and sid = ?", condition, Student.class); 24 return stu; 25 } 26 }
单独的main方法使用:
1 package com.dc.test; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import com.dc.entity.Student; 9 import com.dc.jdbc.JDBCProxy; 10 import com.dc.service.StuService; 11 12 public class MyTest { 13 public static void main(String[] args){ 14 StuService $stuService = new StuService(); 15 JDBCProxy proxyStuService = JDBCProxy.getInstance(); 16 proxyStuService.setTarget($stuService); 17 StuService stuService= (StuService)proxyStuService.getTarget(); 18 19 try { 20 List<Map<String,Object>> stuList = stuService.getStudentInfo(); 21 for (int i = 0; i < stuList.size(); i++) { 22 System.out.println(stuList.get(i).get("name")+"的年龄="+stuList.get(i).get("age")+"岁"); 23 } 24 25 Map<String,Object> map = new HashMap<String,Object>(); 26 map.put("name", "白子画"); 27 System.out.println("---------修改白子画的年龄加一岁---------------"); 28 stuService.updateStuInfo(map); 29 30 31 List<Object> l = new ArrayList<Object>(); 32 l.add("白子画"); 33 l.add(2); 34 Student s = stuService.getOne(l); 35 System.out.println("被修改后白子画的年龄"+s.getAge()); 36 } catch (Exception e) { 37 e.printStackTrace(); 38 } 39 } 40 }
Spring配合使用
applicationContext.xml配置
<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" />
package com.dc.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dc.entity.Student; import com.dc.service.StuService; public class BeanTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); StuService stuService = (StuService) ac.getBean("stuServiceProxy"); try { List<Map<String,Object>> stuList = stuService.getStudentInfo(); for (int i = 0; i < stuList.size(); i++) { System.out.println(stuList.get(i).get("name")+"的年龄="+stuList.get(i).get("age")+"岁"); } Map<String,Object> map = new HashMap<String,Object>(); map.put("name", "白子画"); System.out.println("---------修改白子画的年龄加一岁---------------"); stuService.updateStuInfo(map); List<Object> l = new ArrayList<Object>(); l.add("白子画"); l.add(2); Student s = stuService.getOne(l); System.out.println("被修改后白子画的年龄"+s.getAge()); } catch (Exception e) { e.printStackTrace(); } } }
时间: 2025-01-16 07:23:02