zoukankan      html  css  js  c++  java
  • Spring jdbc Template

    终于可以连接数据库来操作数据了!!!!!!

    首先搭建Beans层

    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;
        }
    }

    然后搭建dao

    public interface IBookDAO {
        public int addBook(Book book);
    }
    
    @Service("bookService")  //这个层的
    public class BookServiceImpl implements IBookService {
        //得去引入/引用/调度/植入
        //jdbc的
        @Autowired
        private IBookDAO dao;
        public int addBook(Book book) {
            int count= dao.addBook(book);
            return count;
        }
    
        public IBookDAO getDao() {
            return dao;
        }
    
        public void setDao(IBookDAO dao) {
            this.dao = dao;
        }
    }

    service

    public interface IBookService {
        public int addBook(Book book);
    }
    
    @Service("bookService")  //这个层的
    public class BookServiceImpl implements IBookService {
        //得去引入/引用/调度/植入
        //jdbc的
        @Autowired
        private IBookDAO dao;
        public int addBook(Book book) {
            int count= dao.addBook(book);
            return count;
        }
    
        public IBookDAO getDao() {
            return dao;
        }
    
        public void setDao(IBookDAO dao) {
            this.dao = dao;
        }
    }
    <context:property-placeholder location="jdbc.properties"/>
       <!--00.开启包扫描器-->
       <context:component-scan base-package="cn.jdbc"></context:component-scan>
      <!--1.配置数据源-->
       <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.username}"></property>
          <property name="password" value="${jdbc.password}"></property>
       </bean>
    
       <!--2.jdbcTemplate-->
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
           <property name="dataSource" ref="dataSource"></property>
       </bean>
    </beans>
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///book
    jdbc.username=root
    jdbc.password=1234
  • 相关阅读:
    字符串内部查找函数
    vs2005 编译zlib 1.2.3 小记
    ies4linux 安装
    详述IP数据包的转发流程
    看源代码
    091213
    值得你记住并受用一生的Word XP/2003快捷键
    java开源框架的源代码怎么读?
    excel中的EMBED域介绍
    如何用c语言实现CString的构造函数、析构函数和赋值函数?
  • 原文地址:https://www.cnblogs.com/xiaoyu1997/p/6644863.html
Copyright © 2011-2022 走看看