• 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
  • 相关阅读:
    事务1-JDBC事务管理
    Tomcat地址栏传中文参数乱码问题处理
    hibernate报错:org.hibernate.MappingException: No Dialect mapping for JDBC type: -1
    UVA12170 Easy Climb
    [POI2004]旅行问题
    [SCOI2010]股票交易
    [USACO11OPEN] Mowing the Lawn G
    查看文件个数 ls |wc -l
    【zombie】如何查看并杀死僵尸进程?
    [Windows] 屏幕截图
  • 原文地址:https://www.cnblogs.com/xiaoyu1997/p/6644863.html
Copyright © 2020-2023  润新知