Spring与Jdbc Demo
方法一:继承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()); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。