Spring连接数据库整合-实例

  1. package cn.com.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.com.bean.PersonBean;  
  6.   
  7. public interface PersonService {  
  8.     //保存  
  9.     public void save(PersonBean person);  
  10.     //更新  
  11.     public void update(PersonBean person);  
  12.     //获取person  
  13.     public PersonBean getPerson(int id);  
  14.       
  15.     public List<PersonBean> getPersonBean();  
  16.     //删除记录  
  17.     public void delete(int personid);  
  18. }  


Person Bean类:

[java]  view plain  copy
  1. package cn.com.bean;  
  2.   
  3. public class PersonBean {  
  4.     private int id;  
  5.     private String name;  
  6.       
  7.     public PersonBean(String name) {  
  8.         this.name=name;  
  9.     }  
  10.     public int getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(int id) {  
  14.         this.id = id;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.       
  23. }  


接口实现:

[java]  view plain  copy
  1. package cn.com.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.sql.DataSource;  
  6.   
  7. import org.springframework.jdbc.core.JdbcTemplate;  
  8.   
  9. import cn.com.bean.PersonBean;  
  10. import cn.com.service.PersonService;  
  11.   
  12. public class PersonServiceImpl implements PersonService {  
  13.     private JdbcTemplate jdbcTemplate;  
  14.     public void setDataSource(DataSource dataSource) {  
  15.         this.jdbcTemplate = new JdbcTemplate(dataSource);  
  16.     }  
  17.   
  18.     @Override  
  19.     public void save(PersonBean person) {  
  20.         // TODO Auto-generated method stub  
  21.         jdbcTemplate.update("insert into person(name) values(?)"new Object[]{person.getName()},   
  22.                 new int[]{java.sql.Types.VARCHAR});  
  23.     }  
  24.   
  25.     @Override  
  26.     public void update(PersonBean person) {  
  27.         // TODO Auto-generated method stub  
  28.         jdbcTemplate.update("update person set name=? where id=?"new Object[]{person.getName(),person.getId()},   
  29.                 new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});  
  30.     }  
  31.   
  32.     @Override  
  33.     public PersonBean getPerson(int id) {  
  34.         // TODO Auto-generated method stub  
  35.         return (PersonBean)jdbcTemplate.queryForObject("select * from person where id=?"new Object[]{id},   
  36.                 new int[]{java.sql.Types.INTEGER},new PersonRowMapper() );  
  37.     }  
  38.   
  39.     @SuppressWarnings("unchecked")  
  40.     @Override  
  41.     public List<PersonBean> getPersonBean() {  
  42.         // TODO Auto-generated method stub  
  43.         return (List<PersonBean>)jdbcTemplate.query("select * from person",   
  44.                 new PersonRowMapper() );  
  45.     }  
  46.   
  47.     @Override  
  48.     public void delete(int personid) {  
  49.         // TODO Auto-generated method stub  
  50.         jdbcTemplate.update("delete from person  where id=?"new Object[]{personid},   
  51.                 new int[]{java.sql.Types.INTEGER});  
  52.     }  
  53.   
  54. }  


RowMapper:

[java]  view plain  copy
  1. package cn.com.service.impl;  
  2.   
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5.   
  6. import org.springframework.jdbc.core.RowMapper;  
  7.   
  8. import cn.com.bean.PersonBean;  
  9.   
  10. public class PersonRowMapper implements RowMapper {  
  11.   
  12.     @Override  
  13.     public Object mapRow(ResultSet rs, int index) throws SQLException {  
  14.         // TODO Auto-generated method stub  
  15.         PersonBean person =new PersonBean(rs.getString("name"));  
  16.         person.setId(rs.getInt("id"));  
  17.         return person;  
  18.     }  
  19.   
  20. }  


beans.xml配置

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"   
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xmlns:tx="http://www.springframework.org/schema/tx"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  11.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  12.            ">            
  13.              <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->  
  14.              <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  15.                 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
  16.                 <property name="url" value="jdbc:mysql://localhost:3306/wy"/>  
  17.                 <property name="username" value="root"/>  
  18.                 <!-- property池启动时的初始值  -->  
  19.                  <property  name="password" value="123"/>  
  20.                  <!-- 连接name="initialSize" value="${initialSize}"/>-->  
  21.                  <property name="initialSize" value="1"/>  
  22.                  <!-- 连接池的最大值 -->  
  23.                  <property name="maxActive" value="500"/>  
  24.                  <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
  25.                  <property name="maxIdle" value="2"/>  
  26.                  <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
  27.                  <property name="minIdle" value="1"/>  
  28.              </bean>  
  29.              <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  30.                 <property name="dataSource" ref="dataSource"/>  
  31.              </bean>  
  32.              <tx:annotation-driven transaction-manager="txManager"/>  
  33.              <bean id="personService" class="cn.com.service.impl.PersonServiceImpl">  
  34.                 <property name="dataSource" ref="dataSource"></property>  
  35.              </bean>  
  36. </beans>  


测试类:

[sql]  view plain  copy
  1. package Junit.test;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import org.junit.BeforeClass;  
  6. import org.junit.Test;  
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  9.   
  10. import cn.com.bean.PersonBean;  
  11. import cn.com.service.PersonService;  
  12.   
  13. public class PersonTest2 {  
  14.  private static  PersonService personService;  
  15.  @BeforeClass  
  16.  public static void setUpBeforeClass() throws Exception {  
  17.   ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");  
  18.   personService=(PersonService) act.getBean("personService");  
  19.  }  
  20.   
  21.  @Test  
  22.  public void save() {  
  23.   personService.save(new PersonBean("wyy"));  
  24.  }  
  25.  @Test  
  26.  public void update() {  
  27.   PersonBean person=personService.getPerson(1);  
  28.   person.setName("wy");  
  29.   personService.update(person);  
  30.  }  
  31.  @Test  
  32.  public void getPerson() {  
  33.   PersonBean person=personService.getPerson(1);  
  34.   System.out.println(person.getName());  
  35.  }  
  36.  @Test  
  37.  public void delete() {  
  38.   personService.delete(1);  
  39.     
  40.  }  
  41.   
  42.   
  43. }  

数据库:

[sql]  view plain  copy
  1. Create Table  
  2.   
  3. CREATE TABLE `person` (  
  4.   `id` int(11) NOT NULL auto_increment,  
  5.   `namevarchar(10) NOT NULL,  
  6.   PRIMARY KEY  (`id`)  
  7. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值