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());
        }
  • 相关阅读:
    好一张图(饼得慢慢吃)
    kafka消息存储原理及查询机制
    (四)SpringCloud入门篇——工程重构
    (三)SpringCloud入门篇——微服务消费者订单Module模块
    java.sql.SQLException: org.gjt.mm.mysql.Driver
    (二)SpringCloud入门篇——Rest微服务工程:支付模块构建
    (一)SpringCloud入门篇——微服务cloud整体聚合:父工程步骤
    虚拟机中docker安装mysql远程无法访问
    Java初级开发项目
    类和对象
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4231278.html
Copyright © 2011-2022 走看看