码迷,mamicode.com
首页 > 数据库 > 详细

jdbctemplate

时间:2017-08-20 10:19:33      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:value   get   manager   ram   source   apach   2-2   word   建立   

1.建数据表

技术分享

 

2.搭建分层.

技术分享

 

 2.1.entity层   

2.1.1 book类

技术分享
public class Book {
    private Integer bookid;
    private String bookname;
    private Integer bookprice;

    public Integer getBookid() {
        return bookid;
    }

    public void setBookid(Integer bookid) {
        this.bookid = bookid;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public Integer getBookprice() {
        return bookprice;
    }

    public void setBookprice(Integer bookprice) {
        this.bookprice = bookprice;
    }
}
技术分享

2.2 dao层

2.2.1接口

public interface IBookDAO {
    //查询素有图书
    public List<Book> findAll();
}

2.2.2.BookDAOimpl 类

技术分享
public class BookDAOimpl extends JdbcDaoSupport implements IBookDAO{
    public List<Book> findAll() {
        String sql="select * from book";
        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("Id"));
                book.setBookname(rs.getString("Name"));
                book.setBookprice(rs.getInt("Price"));
                return book;
            }
        });

        return list;
    }
}
技术分享

2.3service层

2.3.1IBookService 接口

public interface IBookService {
    //查询素有图书
    public List<Book> findAll();
}

2.3.2 BookServiceImpl 

技术分享
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;
    }
}
技术分享

3.jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///****
jdbc.user=root
jdbc.password=root

4.xml文件

技术分享
<?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.识别jdbc.properties文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>


    <!--01.建立数据源  ${} Spring 内置的一个数据源 DriverManager-->
    <!-- <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>-->

    <!--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>-->

    <!--c3p0-->
    <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" 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>-->

    <!--druid-->
    <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>

    <!--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.spring19.dao.impl.BookDAOimpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

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

</beans>
技术分享

5.测试类

技术分享
 // jdbctemplate
    @Test
    public void test21(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring17.xml");
        IBookService service = (IBookService) ctx.getBean("bookService");
        List<Book> list = service.findAll();
        for (Book item:list) {
            System.out.println(item.getBookname());
        }
    }
技术分享

jdbctemplate

标签:value   get   manager   ram   source   apach   2-2   word   建立   

原文地址:http://www.cnblogs.com/s1297-lgy/p/7398772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!