spring实战学习(五)jdbc模板

jdbc是什么?

JDBC和mybatis,hibernate一样都能实现数据持久化的作用,它可以让我们访问到数据库,进行一些增,删,查,改的操作。

用spring的jdbc模板访问数据的demo

创建maven项目,导入依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>//使用dbcp连接池
         <groupId>commons-dbcp</groupId>
         <artifactId>commons-dbcp</artifactId>
         <version>1.4</version>
   </dependency>

建立数据库里的对象

public class Person {
	String name;
	int age;
	int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}

写一个Dao类

@Repository("personDAO")
public class PersonDAO {
	
	@Resource(name="template")
	private JdbcTemplate template;
	
	public void save(Person person) {
		String sql="insert into demo values(null,?,?)";
		template.updata(sql,person.getName(),person.getAge());
		
	}
	
	public void delete(int id){
		String sql="delete * from demo where id=?";
		template.updata(sql,id);
	}
	
	public void updata(Person person) {
		String sql="updata demo set name=? where id=?";
		template.updata(sql,person.getName(),person.getId());
	}
	
	public Person selectById(int id) {
		String sql="select * from demo where id=?";
		return template.queryForObject(sql,new RowMapper<Person>(){
			public Person mapRow(ResultSet rs,int arg)throws SQLException{
				Person person=new Person();
				person.setId(rs.getInt("id"));
				person.setName(rs.getString("name"));
				person.setAge(rs.getInt("age"));
				return person;
			}
		}, id);
	}
	
	
	public List<Person> selectAll() {
		String sql="select * from demo";
		List<Person> list=template.query(sql,new RowMapper<Person>() {
			public Person mapRow(ResultSet rs, int arg1) throws SQLException {
				Person person = new Person();
				person.setId(rs.getInt("id"));
				person.setName(rs.getString("name"));
				person.setAge(rs.getString("age"));
                return Person;
			}
		});
		return list;
	}	
}

xml配置`

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

	   <context:component-scan base-package="org.csii.springjdbc">
	   
	   <context:annotation-config/>
	   
	   <util:properties id="config" location="classpath:db.properties" />
	   
	   <bean: id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	       <property name="dataSource" ref="dataSource"/>
	   </bean>
	   
	   <bean: id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	       <property name="driverClassName" value="#{config.driver}"/>
	       <property name="url" value="#{config.url}"/>
	       <property name="password" value="#{config.password}"/>
	       <property name="username" value="#{config.username}"/>	   
	   </bean>
	  	   
</beans>

.properties文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;autoReconnect=true&amp;autoReconnectForPools=true&amp;characterEncoding=utf8
jdbc.username=xxx//填自己的数据库账号
jdbc.password=xxx

test测试类

public class Test {
	private PersonDAO dao;
	
	@Before
	public void init(){
		ApplicationContext ac = 
				new ClassPathXmlApplicationContext("springmvc.xml");
		dao = ac.getBean("personDAO",PersonDAO.class);
	}
	
	@Test
	public void test1() {
		Person person=new Person();
		person.setId(1);
		person.setName("tang");
		person.setAge(22);
		dao.save(person);
	}
}

连接池注入模板,jdbc模板再注入dao
这样就可以将数据插入数据库了
`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值