Spring 使用JdbcTemPlate的方式实现与数据库的连接

包结构:



接口中写一个方法就可以了

package cn.happy.spring22jdbc.dao;

import cn.happy.spring22jdbc.entity.Book;

import java.util.List;

/**
 * Created by linlin on 2017/8/2.
 */
public interface IBookDao {
    public List<Book> getList();
}


实现类中继承

JdbcDaoSupport并实现接口
并进行SQL语句的编写等  操作
package cn.happy.spring22jdbc.dao.impl;

import cn.happy.spring22jdbc.dao.IBookDao;
import cn.happy.spring22jdbc.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 linlin on 2017/8/2.
 */
public class BookDaoImpl extends JdbcDaoSupport implements IBookDao{

    public List<Book> getList() {
        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("bookid"));
              book.setBookname(rs.getString("bookname"));
              book.setBookprice(rs.getInt("bookprice"));
               return book;
           }
       });
       return list;
    }
}

package cn.happy.spring22jdbc.entity;

/**
 * Created by linlin on 2017/8/2.
 */
public class Book {
    private int bookid;
    private String bookname;
    private int bookprice;

    public int getBookid() {
        return bookid;
    }

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

    public String getBookname() {
        return bookname;
    }

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

    public int getBookprice() {
        return bookprice;
    }

    public void setBookprice(int bookprice) {
        this.bookprice = bookprice;
    }
}



实体类中 把 属性进行封装
package cn.happy.spring22jdbc.service;

import cn.happy.spring22jdbc.entity.Book;

import java.util.List;

/**
 * Created by linlin on 2017/8/2.
 */
public interface IBookService {
    public List<Book> getList();
}

package cn.happy.spring22jdbc.service.impl;

import cn.happy.spring22jdbc.dao.IBookDao;
import cn.happy.spring22jdbc.entity.Book;
import cn.happy.spring22jdbc.service.IBookService;

import java.util.List;

/**
 * Created by linlin on 2017/8/2.
 */
public class BookServiceImpl implements IBookService{
    private IBookDao dao;
    public List<Book> getList() {
        return dao.getList();
    }

    public IBookDao getDao() {
        return dao;
    }

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


连接数据库主要是配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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 ">


<context:property-placeholder location="jdbc.properties"></context:property-placeholder>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <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>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>

    </bean>

    <bean id="bookDao" class="cn.happy.spring22jdbc.dao.impl.BookDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>

    </bean>
    <bean id="bookService" class="cn.happy.spring22jdbc.service.impl.BookServiceImpl">

        <property name="dao" ref="bookDao"></property>
    </bean>

</beans>

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book
jdbc.username=root
jdbc.password=


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值