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
  • 相关阅读:
    程序猿神吐槽,说出那些苦逼的日子!
    java设计模式演示样例
    DP Leetcode
    标准差(standard deviation)和标准误差(standard error)你能解释清楚吗?
    移动开发 Native APP、Hybrid APP和Web APP介绍
    Oracle cloud control 12c 的启动与关闭
    Ubuntu文件的复制、移动和删除命令
    Android中集成第三方库的方法和问题
    腾讯QQ企业邮箱POP3/SMTP设置
    又发现支付宝和淘宝的设计漏洞
  • 原文地址:https://www.cnblogs.com/xiaoyu1997/p/6644863.html
Copyright © 2011-2022 走看看