jdbctemplate

Posted Remnart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbctemplate相关的知识,希望对你有一定的参考价值。

 

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的主要内容,如果未能解决你的问题,请参考以下文章

Spring 模板技术JdbcTemplate

Spring对JDBC的模板支持——JdbcTemplate

浅谈jdbcTemplate与mybatis

Spring JdbcTemplate+JdbcDaoSupport实例

Spring jdbcTemplate

Spring_使用 JdbcTemplate和JdbcDaoSupport-代码