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
  • 相关阅读:
    Memento模式
    CSS实现半透明div层的方法
    JS解析json数据(如何将json字符串转化为数组)
    并发容器Map之二:ConcurrentNavigableMap
    JSinArray检查数组中是否存在某个值
    Servlet3.0之七:@WebFilter申明该类为过滤器
    Spring源码阅览——BeanFactory体系结构
    使用 Selenium RC 测试 web 应用程序
    函数式编程
    9 个 Java 处理 Exception 的最佳实践
  • 原文地址:https://www.cnblogs.com/xiaoyu1997/p/6644863.html
Copyright © 2011-2022 走看看