Spring连接数据库

Posted 颢Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring连接数据库相关的知识,希望对你有一定的参考价值。

 

 

 

public class Book {
    private int bookid;
    private String bookname;
    private String bookauthor;
    private int bookprice;
    private int bookcategory;

    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 String getBookauthor() {
        return bookauthor;
    }

    public void setBookauthor(String bookauthor) {
        this.bookauthor = bookauthor;
    }

    public int getBookprice() {
        return bookprice;
    }

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

    public int getBookcategory() {
        return bookcategory;
    }

    public void setBookcategory(int bookcategory) {
        this.bookcategory = bookcategory;
    }

    public Book() {
    }

    public Book(int bookid, String bookname, String bookauthor, int bookprice, int bookcategory) {
        this.bookid = bookid;
        this.bookname = bookname;
        this.bookauthor = bookauthor;
        this.bookprice = bookprice;
        this.bookcategory = bookcategory;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookid=" + bookid +
                ", bookname=\'" + bookname + \'\\\'\' +
                ", bookauthor=\'" + bookauthor + \'\\\'\' +
                ", bookprice=" + bookprice +
                ", bookcategory=" + bookcategory +
                \'}\';
    }
}

 

public interface IBookDao {
    List<Book> findAll();
}

 

public class BookDaoImpl extends JdbcDaoSupport implements IBookDao {
    public List<Book> findAll() {
        String sql = "select * from book";
        List<Book> list = 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.setBookauthor(rs.getString("bookauthor"));
                book.setBookprice(rs.getInt("bookprice"));
                book.setBookcategory(rs.getInt("bookcategory"));
                return book;
            }
        });
        return list;
    }
}

 

public interface IBookService {
    List<Book> findAll();
}

 

public class BookServiceImpl implements IBookService {
    IBookDao book;

    public IBookDao getBook() {
        return book;
    }

    public void setBook(IBookDao book) {
        this.book = book;
    }

    public List<Book> findAll() {
        return book.findAll();
    }
}

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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
">
    <!--1.DataSource 数据源  提供咱们连接 ,配置的是数据连接的四大属性--><!--jdbc连接数据源,不建议使用,因为节点太少,不利于扩展-->
    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="url" value="jdbc:mysql:///t14"></property>
         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
         <property name="username" value="root"></property>
         <property name="password" value="1234"></property>
     </bean>-->
    <!--2.DataSource 数据源  提供咱们连接 ,配置的是数据连接的四大属性--><!--dbcp连接数据源-->
    <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql:///t14"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>-->
    <!--3.DataSource 数据源  提供咱们连接 ,配置的是数据连接的四大属性--><!--c3p0连接数据库-->
    <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql:///t14"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>-->

    <!--4.DataSource 数据源  提供咱们连接 ,配置的是数据连接的四大属性--><!--网上搜的,阿里的提供的连接方式-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql:///t14"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>

    <!--2.JDBCTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--Dao-->
    <bean id="bookDAO" class="cn.happy.dao.impl.BookDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!--4.service-->
    <bean id="bookService" class="cn.happy.service.impl.BookServiceImpl">
        <property name="book" ref="bookDAO"></property>
    </bean>
</beans>

 

public class BookTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        IBookService bookService = (IBookService) context.getBean("bookService");
        List<Book> books = bookService.findAll();
        for (Book item : books) {
            System.out.println(bookService.findAll());
        }
    }
}

 

以上是关于Spring连接数据库的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

这些角度电子邮件指令代码片段如何连接

错误:E/RecyclerView:未连接适配器;跳过片段上的布局

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段