四种方法配置数据源

在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>
以上就是四种配置方法
原文地址:https://www.cnblogs.com/ztm1021810064/p/8588075.html