Spring 集成JDBC 使用org.springframework.jdbc.core.JdbcTemplate

1,Spring 集成JDBC 使用org.springframework.jdbc.core.JdbcTemplate

1,集成spring 框架:

pom 文件如下:

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">

4.0.0

com.gsc.org2.spring01

test.demo

war

0.0.1-SNAPSHOT

test.demo Maven Webapp

http://maven.apache.org



	

		junit

		junit

		3.8.1

		test

	

	

	

	

		org.springframework

		spring-core

		4.1.4.RELEASE

	

	
	
	
		org.springframework
		spring-context
		4.1.4.RELEASE
	



	
		org.springframework
		spring-jdbc
		4.1.4.RELEASE
	
	
	
		com.alibaba
		druid
		0.2.25
	
	
	
		mysql
		mysql-connector-java
		5.1.18
	









	test.demo

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 findDemo() {
	   String sql = "select * from demo";
        final List listAllUser = new ArrayList();
        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 配置文件:



    

        

    





    

        com.mysql.jdbc.Driver

    

    

        jdbc:mysql://localhost:3306/stu

    

    

        root

    

    

        123456

    





    

        

    





   


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 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());

  }

}

}

你可能感兴趣的:(java,框架修炼)