Spring笔记-spring集合jdbc

xml文件配置:

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

<!-- 此句 spring读取属性文件的值 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动的初始值 -->
<property name="initialSize" value="${initialSize}" />
<!--连接池的最大值-->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空间值 高峰时间过后,释放连接到最少数-->
<property name="maxIdle" value="${maxIdle}"/>
<!--最少空间值,当空闲的连接数小于阀数,连接池就会申请连接-->
<property name="minIdle" value="${minIdle}"/>
</bean>
<!--配置spring的事务管理 控制事物打开/提交/回滚 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 用@Transactional注解方式使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personService" class="daniel.jdbctest.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

根据数据源的表写出实体类:

package daniel.jdbctest.bean;

public class Person {
private int id;
private String name;
private String password;

public Person(String n, String p) {
this.name =n;
this.password = p;
}
public Person(int Id,String n, String p) {
this.id = Id;
this.name =n;
this.password = p;
}
public void setId(int Id)
{
this.id = Id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name =name;
}
public String getName()
{
return name;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return password;
}
}

接口

package daniel.jdbctest.service;
/*
* 增删改接口方法,获取一个或多个person对像方法
*/
import daniel.jdbctest.bean.Person;
import java.util.List;

public interface PersonService {
public void save(Person person);
public void update(Person person);
public void delete(int personid);
public Person getPerson(int personid);
public List<Person> getPersons();
}

根据接口创建实现类


package daniel.jdbctest.service.impl;


import daniel.jdbctest.bean.Person;
import daniel.jdbctest.service.PersonService;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class PersonServiceImpl implements PersonService{
private JdbcTemplate jdbcTemplate ;
public void setDataSource(DataSource dataSource)
{
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void save(Person person) {
jdbcTemplate.update("insert into person(id,name,password) values(?,?,?)", new Object[]{person.getId(),person.getName(),person.getPassword()},
new int[]{java.sql.Types.INTEGER,java.sql.Types.VARCHAR,java.sql.Types.VARCHAR});
}//参数1:sql语句,参数2:对应?的具体的值,参数3:值的类型

public void update(Person person) {
jdbcTemplate.update("update person set name=? where id=?",new Object[]{person.getName(),person.getId()},
new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
jdbcTemplate.update("update person set password=? where id=?",new Object[]{person.getPassword(),person.getId()},
new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
}参数1:sql语句,参数2:对应?的具体的值,参数3:值的类型

public void delete(int personid) {
jdbcTemplate.update("delete from person where id =?",new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
}

public Person getPerson(int personid) {
return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid}, new int[]{java.sql.Types.INTEGER}, new PersonRowMapper());
}//获取指定记录,参数3:需要回调方法

public List<Person> getPersons() {
return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper());
}

}

回调的类

package daniel.jdbctest.service.impl;
import daniel.jdbctest.bean.Person;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class PersonRowMapper implements RowMapper{

public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person(rs.getString("name"),rs.getString("password"));
person.setId(rs.getInt("id"));
return person;
}

}

测试类



import daniel.jdbctest.bean.Person;
import daniel.jdbctest.service.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.junit.Test;
import org.junit.BeforeClass;
public class TestJdbc{
private static PersonService personservice ;
@BeforeClass
public static void main() throws Exception{
try{
ApplicationContext cxt = new ClassPathXmlApplicationContext("beanconfig.xml");
personservice = (PersonService) cxt.getBean("personService");
}
catch(Exception e)
{
e.printStackTrace();
}

}
// @Test
public void save ()throws Exception
{
try{
Person p = new Person("daniel","123456");
personservice.save(p);
}
catch(Exception e)
{
e.printStackTrace();
}
}
// @Test
public void delete()throws Exception
{
personservice.delete(1239);
}
@Test
public void update()throws Exception
{
Person p = personservice.getPerson(1);
if(p.getName().equals("tiger"))
{
p.setPassword("11111");
p.setName("jason");
}
personservice.update(p);
}
@Test
public void getPersons()throws Exception
{
for(Person person:personservice.getPersons()){
System.out.println("name:"+person.getName()+" password:"+person.getPassword());
}

}
}

spring.jar,commons-pool.jar-->Spring需要用的jar
aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1_3-->AOP需要用的jar
commons-pool.jar,commons-dbcp.jar-->JDBC需要用的jar
common-annotations.jar-->注解需要用的Jar
mysql-connector-java-5[1][1].0.3-bin.jar-->mysql的连接驱动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值