1,Spring 集成JDBC 使用org.springframework.jdbc.core.JdbcTemplate
1,集成spring 框架:
pom 文件如下:
<project xmlns=“http://maven.apache.org/POM/4.0.0”
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gsc.org2.spring01</groupId>
<artifactId>test.demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test.demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.25</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
</dependencies>
<build>
<finalName>test.demo</finalName>
</build>
2,创建数据库对应的|Bean 对象:如下例子:
package test.demo.beans;
public class Demo {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = 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 {
private JdbcTemplate jdbcTemplate;//重点
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Demo> findDemo() {
String sql = "select * from demo";
final List<Demo> listAllUser = new ArrayList<Demo>();
jdbcTemplate.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
Demo u=new Demo();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
listAllUser.add(u);
}
});
return listAllUser;
}
@Override
public void insertDemo(Demo demo) {
String sql="insert into demo(id,name) values(?,?) ";
jdbcTemplate.update(sql, demo.getId(),demo.getName());
}
@Override
public void delDemoById(int id) {
String sql="delete from demo where id= "+id;
jdbcTemplate.update(sql);
}
}
4.创建Spring 配置文件:
<?xml version="1.0" encoding="UTF-8"?><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/stu</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="demoDAO" class="test.demo.dao.DemoDaoImp">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
<bean id="demo" class="test.demo.beans.Demo">
</bean>
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”);
@SuppressWarnings("resource")
ApplicationContext actx=new ClassPathXmlApplicationContext("applicationContext.xml");
DemoDao demodao=(DemoDao) actx.getBean("demoDAO");
List<Demo> list=demodao.findDemo();
//
// demodao.delDemoById(1100);
// Demo demo1=new Demo();
// demo1.setId(1900);
// demo1.setName(“NO1”);
// demodao.insertDemo(demo1);
for(Demo d:list) {
System.out.println("demo id="+d.getId());
System.out.println("demo name="+d.getName());
}
}
}