zoukankan      html  css  js  c++  java
  • 四种方法配置数据源

    在Spring中配置数据源实例

    我们先创建一个实例

    public interface IBookDao {
        public List<Book> findAllBook();
    }

    实现此接口方法

    public class BookDaoImpl extends JdbcDaoSupport implements IBookDao {
        String sql ="select * from book";
        public List<Book> findAllBook() {
            List<Book> list = this.getJdbcTemplate().query(sql,new RowMapper<Book>() {
                public Book mapRow(ResultSet rs, int i) throws SQLException {
                    Book book = new Book();
                    book.setBookID(rs.getInt("bookID"));
                    book.setBookName(rs.getString("bookName"));
                    book.setBookAuthor(rs.getString("bookAuthor"));
                    book.setBookPrice(rs.getInt("bookPrice"));
                    return book;
                }
            });
            return list;
        }
    }

    创建service层

    public interface BookService {
        public List<Book> list();
    }

    实现service接口

    public class BookServiceImpl implements BookService {
        IBookDao dao;
        public List<Book> list() {
            return dao.findAllBook();
        }
    
        public IBookDao getDao() {
            return dao;
        }
    
        public void setDao(IBookDao dao) {
            this.dao = dao;
        }
    }

    编写xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--spring自带的配置方法-->
    <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
        <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>-->
        <!--<property name="url" value="jdbc:mysql:///books"></property>-->
        <!--<property name="username" value="root"></property>-->
        <!--<property name="password" value=""></property>-->
    <!--</bean>-->
        <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">-->
            <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>-->
            <!--<property name="url" value="jdbc:mysql:///books"></property>-->
            <!--<property name="username" value="root"></property>-->
            <!--<property name="password" value=""></property>-->
        <!--</bean>-->
        <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
            <!--<property name="driverClass" value="com.mysql.jdbc.Driver"></property>-->
            <!--<property name="jdbcUrl" value="jdbc:mysql:///books"></property>-->
            <!--<property name="user" value="root"></property>-->
            <!--<property name="password" value=""></property>-->
    <!--阿里的-->
        <!--</bean>-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
    
        <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <bean id="bookDao" class="cn.dome05.dao.impl.BookDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
    
        <bean id="bookService" class="cn.dome05.service.impl.BookServiceImpl">
            <property name="dao" ref="bookDao"></property>
        </bean>
    </beans>
    以上就是四种配置方法
  • 相关阅读:
    Android系统介绍与框架(转)
    6个值得推荐的Android开源框架简介(转)
    程序员最喜爱的12个Android应用开发框架二(转)
    android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
    Android数据缓存(转)
    [UI]实用案例--Shape绘制实用圆圈
    接口API测试和返回值JSON解析的插件
    Android LayoutInflater详解(转)
    一个json字符串
    Android中设定EditText的输入长度(转)
  • 原文地址:https://www.cnblogs.com/ztm1021810064/p/8588075.html
Copyright © 2011-2022 走看看