码迷,mamicode.com
首页 > 数据库 > 详细

Spring与Jdbc Demo

时间:2015-01-17 23:32:01      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

方法一:继承JdbcTemplate来实现

  1、配置applicationContext  

 1 <!-- 获取数据源连接   dbcp -->    
 2 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 3 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
 4 <property name="url" value="jdbc:mysql://localhost:3306/hibernatetest"></property>
 5 <property name="username" value="root"></property>
 6 <property name="password" value="mysql"></property>
 7 </bean>  
 8  
 9 <!-- ********************************** -->
10 <bean id="personDao" class="cn.test.spring.jdbc.PersonDaoImpl">
11     <property name="dataSource">
12         <ref bean="dataSource"/>
13     </property>
14 </bean>

  2、继承applicationContext

1 public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao {
2 
3     public void savePerson() {
4         this.getJdbcTemplate().execute(" INSERT INTO person(pid,Pname,Page) VALUES(2,‘李四‘,50) ");
5         
6     }

  3、测试

@Test
    public void savePerson(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
        PersonDao personDao= (PersonDao) applicationContext.getBean("personDao");
        personDao.savePerson();
    }

方法二:jdbcTemplate作为属性带入

  1、配置applicationContext.xml

 

<!-- 获取数据源连接   dbcp -->    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/hibernatetest"></property>
<property name="username" value="root"></property>
<property name="password" value="mysql"></property>
</bean> 

<!-- ********************************** -->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
        <ref bean="dataSource"/>
    </property>
</bean>
<bean id="personDao2" class="cn.test.spring.jdbc.PersonDaoImpl2">
    <property name="jdbcTemplate">
        <ref bean="jdbcTemplate"/>
    </property>
</bean>

 

   2、声明字段

 1 public class PersonDaoImpl2  implements PersonDao {
 2 
 3     private JdbcTemplate jdbcTemplate;
 4     
 5     public JdbcTemplate getJdbcTemplate() {
 6         return jdbcTemplate;
 7     }
 8 
 9     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
10         this.jdbcTemplate = jdbcTemplate;
11     }
12 
13     
14     public void savePerson() {
15         this.getJdbcTemplate().execute(" INSERT INTO person(pid,Pname,Page) VALUES(22,‘李四2‘,502) ");
16         
17     }

  3、测试

@Test
    public void savePerson2(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
        PersonDao personDao= (PersonDao) applicationContext.getBean("personDao2");
        personDao.savePerson();
    }

 

方法三:通过构造函数

1、配置applicationContext.xml

<!-- 获取数据源连接   dbcp -->    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/hibernatetest"></property>
<property name="username" value="root"></property>
<property name="password" value="mysql"></property>
</bean> 


<!-- ********************************** -->
<bean id="personDao3" class="cn.test.spring.jdbc.PersonDaoImpl3">
    <constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>

 

2、构造函数

public class PersonDaoImpl3 extends JdbcTemplate  implements PersonDao {

    public PersonDaoImpl3(DataSource dataSource){
        super(dataSource);
    }
    
    public void savePerson() {
        this.execute(" INSERT INTO person(pid,Pname,Page) VALUES(23,‘李四3‘,503) ");
        
    }

 

3、测试

@Test
    public void savePerson3(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
        PersonDao personDao= (PersonDao) applicationContext.getBean("personDao3");
        personDao.savePerson();
    }

 

注:以上方法只能进行增删改,不能进行查找

查找:

目标方法:

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

 

PersonRowMapper.java

public class PersonRowMapper implements RowMapper {

    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        Person person=new Person();
        person.setPid(rs.getLong("pid"));
        person.setPname(rs.getString("Pname"));
        person.setPage(rs.getString("Page"));
        return person;
    }

 

测试

@Test
    public void testQuery(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
        PersonDao personDao= (PersonDao) applicationContext.getBean("personDao");
        List<Person> personList = personDao.getPersons();
        System.out.println(personList.size());
    }

 

Spring与Jdbc Demo

标签:

原文地址:http://www.cnblogs.com/liuwt365/p/4231278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!