Spring-MyBatis(c3p0-alibaba-dbcp-springframework)识别jbdc。propertties文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
">
<!--00.识别jbdc。propertties文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

   <!-- 01.建立数据库 ${}Spring n内设的一个数据  DriverManger&ndash;&gt;

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>-->

   <!--&lt;!&ndash;02.阿里巴巴的&ndash;&gt;

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>-->
  <!--  &lt;!&ndash;03.c3p0 com.mchange.v2.c3p0.ComboPooledDataSource&ndash;&gt;

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="dataSourceName" value="${jdbc.driverClassName}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>-->
      <!--04.dbcp-->

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${jdbc.driverClassName}"></property>
       <property name="url" value="${jdbc.url}"></property>
       <property name="username" value="${jdbc.user}"></property>
       <property name="password" value="${jdbc.password}"></property>

   </bean>

    <!--02.jdbcTemplate 配置-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--03.dao配置-->
    <bean id="bookDao" class="cn.bdqn.spring19jdbctemplate.dao.impl.BookDAOImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!--04.service   bookService-->
    <bean id="bookService" class="cn.bdqn.spring19jdbctemplate.service.ipml.BookServiceImpl">
        <property name="dao" ref="bookDao"></property>
    </bean>


</beans>


package cn.bdqn.spring19jdbctemplate.dao.impl;



import cn.bdqn.spring19jdbctemplate.dao.IBookDAO;
import cn.bdqn.spring19jdbctemplate.entity.Book;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Happy on 2017-08-02.
 */
public class BookDAOImpl extends JdbcDaoSupport implements IBookDAO {
    //植入 jdbcTemplate
    public List<Book> findAll() {
        String sql="select * from book";
        //接口的实现类 匿名内部类
        List<Book> list=this.getJdbcTemplate().query(sql, new RowMapper<Book>() {
            /**
             *
             * @param
             * @param
             * @return
             * @throws SQLException
             */
            public Book mapRow(ResultSet rs, int i) throws SQLException {
                Book book=new Book();
                book.setBookid(rs.getInt("bookid"));
                book.setBookname(rs.getString("bookname"));
                book.setBookprice(rs.getInt("bookprice"));
                return book;
            }
        });
        return list;
    }
}



package cn.bdqn.spring19jdbctemplate.dao;



import cn.bdqn.spring19jdbctemplate.entity.Book;

import java.util.List;

/**
 * Created by Happy on 2017-08-02.
 */
public interface IBookDAO {
    //01.查询素有图书
    public List<Book> findAll();
}



package cn.bdqn.spring19jdbctemplate.service;



import cn.bdqn.spring19jdbctemplate.entity.Book;

import java.util.List;

/**
 * Created by Happy on 2017-08-02.
 */
public interface IBookService {
    //01.查询素有图书
    public List<Book> findAll();
}
 
 
 
 
 
 
 
 
package cn.bdqn.spring19jdbctemplate.service.ipml;


import cn.bdqn.spring19jdbctemplate.dao.IBookDAO;
import cn.bdqn.spring19jdbctemplate.entity.Book;
import cn.bdqn.spring19jdbctemplate.service.IBookService;

import java.util.List;

/**
 * Created by Happy on 2017-08-02.
 */
public class BookServiceImpl implements IBookService {

    //植入dao
    private IBookDAO dao;

    public List<Book> findAll() {
        return dao.findAll();
    }

    public IBookDAO getDao() {
        return dao;
    }

    public void setDao(IBookDAO dao) {
        this.dao = dao;
    }
}


猜你喜欢

转载自blog.csdn.net/qq_36074218/article/details/76678548