zoukankan      html  css  js  c++  java
  • 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());
        }
  • 相关阅读:
    C#数组的Map、Filter、Reduce操作
    Safari中的input、textarea无法输入的问题
    小练手:用HTML5 Canvas绘制谢尔宾斯基三角形
    Web前端页面的浏览器兼容性测试心得(三)总结一些IE8兼容问题的解决方案
    大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画
    Web前端页面的浏览器兼容性测试心得(二)搭建原汁原味的IE8测试环境
    Web前端页面的浏览器兼容性测试心得(一)搭建测试用本地静态服务器
    CSS3、SVG、Canvas、WebGL动画精选整理
    利用JS代码快速获得知网论文作为参考文献的引用文本
    使用Node.js快速搭建简单的静态文件服务器
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4231278.html
Copyright © 2011-2022 走看看