1,集成spring 框架:
pom 文件如下:
2,创建数据库对应的|Bean 对象:如下例子: package test.demo.beans; public class Demo { private String name; private int id; public String getName() { } public void setName(String name) { } public int getId() { } public void setId(int id) { } } 3.创建Dao 类和实现类 package test.demo.dao; import java.util.List; import test.demo.beans.Demo; public interface DemoDao { public List findDemo(); public void insertDemo(Demo demo); public void delDemoById(int id); } package test.demo.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import test.demo.beans.Demo; public class DemoDaoImp implements DemoDao { } 4.创建Spring 配置文件: 4,测试 package test.demo.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import test.demo.beans.Demo; import test.demo.dao.DemoDao; public class MyTest { public static void main(String[] args) { // Resource resource = new ClassPathResource(“applicationContext.xml”); // BeanFactory factory=new XmlBeanFactory(resource); // Demo user = (Demo) factory.getBean(“demo”); // // demodao.delDemoById(1100); // Demo demo1=new Demo(); // demo1.setId(1900); // demo1.setName(“NO1”); // demodao.insertDemo(demo1); } }xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
return name;
this.name = name;
return id;
this.id = id;
private JdbcTemplate jdbcTemplate;//重点
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List
@SuppressWarnings("resource")
ApplicationContext actx=new ClassPathXmlApplicationContext("applicationContext.xml");
DemoDao demodao=(DemoDao) actx.getBean("demoDAO");
List
for(Demo d:list) {
System.out.println("demo id="+d.getId());
System.out.println("demo name="+d.getName());
}