1
2 1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2
Spring自带一个ORM持久化框架JdbcTemplate,他可以说是jdbc的加强版,但是对最细微的控制肯定做不到像jdbc那么细,那么性能高,他的出现是为了让jdbc更加便于使用
下面我说一下我这篇博客要干嘛,
一,简单的JdbcTemplate做出来的案例
二,除了自带的Spring-jdbc,补充c3p0,druid,dbcp三种DataSource数据源
**1.**案例查询图书
1.1数据表:
接下来就是java程序开发工具idea做的事了
**1.2引入依赖jar包:
**
以前的jar包外,还需要spring-jdbc和jdbc的mysql的jar
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1 <!--spring jdbc-->
2 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
3 <dependency>
4 <groupId>org.springframework</groupId>
5 <artifactId>spring-jdbc</artifactId>
6 <version>4.3.14.RELEASE</version>
7 </dependency>
8
9 <dependency>
10 <groupId>mysql</groupId>
11 <artifactId>mysql-connector-java</artifactId>
12 <version>5.1.39</version>
13 </dependency>
14
1.3分层开发开始,先是entity实体类层
Book
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 1package cn.dawn.day21jdbctemplate.entity;
2
3public class Book {
4 private Integer bookID;
5 private String bookName;
6 private String bookAuthor;
7 private Integer bookPrice;
8
9 @Override
10 public String toString() {
11 return "Book{" +
12 "bookID=" + bookID +
13 ", bookName='" + bookName + '\'' +
14 ", bookAuthor='" + bookAuthor + '\'' +
15 ", bookPrice=" + bookPrice +
16 '}';
17 }
18
19 public Book() {
20 }
21
22 public Integer getBookID() {
23 return this.bookID;
24 }
25
26 public void setBookID(Integer bookID) {
27 this.bookID = bookID;
28 }
29
30 public String getBookName() {
31 return this.bookName;
32 }
33
34 public void setBookName(String bookName) {
35 this.bookName = bookName;
36 }
37
38 public String getBookAuthor() {
39 return this.bookAuthor;
40 }
41
42 public void setBookAuthor(String bookAuthor) {
43 this.bookAuthor = bookAuthor;
44 }
45
46 public Integer getBookPrice() {
47 return this.bookPrice;
48 }
49
50 public void setBookPrice(Integer bookPrice) {
51 this.bookPrice = bookPrice;
52 }
53}
54
1.3dao层
一个接口IBookDAO
1
2
3
4
5
6
7
8
9
10
11
12
13 1package cn.dawn.day21jdbctemplate.dao;
2
3import cn.dawn.day21jdbctemplate.entity.Book;
4
5import java.util.List;
6
7/**
8 * Created by Dawn on 2018/3/13.
9 */
10public interface IBookDAO {
11 public List<Book> findAllBooks();
12}
13
它的实现类BookDAOImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1package cn.dawn.day21jdbctemplate.dao;
2
3import cn.dawn.day21jdbctemplate.entity.Book;
4import org.springframework.jdbc.core.RowMapper;
5import org.springframework.jdbc.core.support.JdbcDaoSupport;
6
7import java.sql.ResultSet;
8import java.sql.SQLException;
9import java.util.List;
10
11/**
12 * Created by Dawn on 2018/3/13.
13 */
14public class BookDAOImpl extends JdbcDaoSupport implements IBookDAO {
15 public List<Book> findAllBooks() {
16 String sql="select * from book";
17 List<Book> lists = this.getJdbcTemplate().query(sql, new RowMapper<Book>() {
18 public Book mapRow(ResultSet rs, int i) throws SQLException {
19 Book book=new Book();
20 book.setBookAuthor(rs.getString("bookAuthor"));
21 book.setBookID(rs.getInt("bookId"));
22 book.setBookName(rs.getString("bookName"));
23 book.setBookPrice(rs.getInt("bookPrice"));
24 return book;
25 }
26 });
27 return lists;
28 }
29}
30
这儿有几点要说的,他实现了JdbcTemplate,是为了一会给这个类注入数据源等操作,子类继承了父类的方法属性,所以这个类才能和数据库打交道
ResultSet就是数据库的单行记录,i就是此单行记录是符合你查出来的中的第几条,先把案例配通
1.4service层
一个接口IBookService
1
2
3
4
5
6
7
8
9
10
11
12
13 1package cn.dawn.day21jdbctemplate.service;
2
3import cn.dawn.day21jdbctemplate.entity.Book;
4
5import java.util.List;
6
7/**
8 * Created by Dawn on 2018/3/13.
9 */
10public interface IBookService {
11 public List<Book> findAllBooks();
12}
13
它的实现类BookService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1package cn.dawn.day21jdbctemplate.service;
2
3import cn.dawn.day21jdbctemplate.dao.IBookDAO;
4import cn.dawn.day21jdbctemplate.entity.Book;
5
6import java.util.List;
7
8/**
9 * Created by Dawn on 2018/3/13.
10 */
11public class BookServiceImpl implements IBookService{
12 private IBookDAO dao;
13
14 public List<Book> findAllBooks() {
15 return dao.findAllBooks();
16 }
17
18 public IBookDAO getDao() {
19 return dao;
20 }
21
22 public void setDao(IBookDAO dao) {
23 this.dao = dao;
24 }
25}
26
dao层的接口声明成成员变量,并且提供get/set方法,一会要注入进来dao层的实现
1.5我做了个一步到位的操作,将Driver,url,username,pwd这些jdbc连接数据库的条件提到了propertiess文件中
jdbc.properties
1
2
3
4
5 1jdbc.driver=com.mysql.jdbc.Driver
2jdbc.url=jdbc:mysql:///s2228
3jdbc.username=root
4jdbc.password=
5
1.6就是书写大配置文件了
起名随意,一会单测方法中能合的上就行,以.xml结尾
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10
11 <!--配置jdbc。properties-->
12 <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
13
14 <!--自带的持久化框架的数据源-->
15 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
16 <property name="driverClassName" value="${jdbc.driver}"></property>
17 <property name="url" value="${jdbc.url}"></property>
18 <property name="username" value="${jdbc.username}"></property>
19 <property name="password" value="${jdbc.password}"></property>
20 </bean>
21
22
23 <!--配置jdbctemplate-->
24 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
25 <property name="dataSource" ref="dataSource"></property>
26 </bean>
27
28 <!--dao-->
29 <bean id="bookDao" class="cn.dawn.day21jdbctemplate.dao.BookDAOImpl">
30 <property name="jdbcTemplate" ref="jdbcTemplate"></property>
31 </bean>
32
33 <!--service-->
34 <bean id="bookService" class="cn.dawn.day21jdbctemplate.service.BookServiceImpl">
35 <property name="dao" ref="bookDao"></property>
36 </bean>
37</beans>
38
1.7书写测试方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 1package cn.dawn.day21JdbcTemplate;
2
3
4import cn.dawn.day21jdbctemplate.entity.Book;
5import cn.dawn.day21jdbctemplate.service.IBookService;
6import org.junit.Test;
7import org.springframework.context.ApplicationContext;
8import org.springframework.context.support.ClassPathXmlApplicationContext;
9
10import java.util.List;
11
12/**
13 * Created by Dawn on 2018/3/3.
14 */
15public class test20180313 {
16 @Test
17 /*aop代理工厂bean异常增强*/
18 public void t01(){
19 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day21JdbcTemplate.xml");
20 IBookService service = (IBookService) context.getBean("bookService");
21 List<Book> allBooks = service.findAllBooks();
22 for (Book item:allBooks) {
23 System.out.println(item);
24 }
25
26 }
27}
28
案例完
下面说说另外三种数据源的配置,不同公司使用的不是完全一样,所以我提及几种,使用方法,在上方的案例中只要引入jar包,把xml中dateSource那个bean替换掉就好
1.dbcp
1.1引入jar包
1
2
3
4
5
6
7 1 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
2 <dependency>
3 <groupId>org.apache.commons</groupId>
4 <artifactId>commons-dbcp2</artifactId>
5 <version>2.1.1</version>
6 </dependency>
7
1.2xml文件中的dataSource
1
2
3
4
5
6
7 1 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
2 <property name="driverClassName" value="${jdbc.driver}"></property>
3 <property name="url" value="${jdbc.url}"></property>
4 <property name="username" value="${jdbc.username}"></property>
5 <property name="password" value="${jdbc.password}"></property>
6 </bean>
7
2.c3p0
2.1jar包
1
2
3
4
5
6
7 1 <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
2 <dependency>
3 <groupId>c3p0</groupId>
4 <artifactId>c3p0</artifactId>
5 <version>0.9.1.2</version>
6 </dependency>
7
2.2xml中
1
2
3
4
5
6
7 1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
2 <property name="driverClass" value="${jdbc.driver}"></property>
3 <property name="jdbcUrl" value="${jdbc.url}"></property>
4 <property name="user" value="${jdbc.username}"></property>
5 <property name="password" value="${jdbc.password}"></property>
6 </bean>
7
3.druid 这个是中国阿里巴巴开发的
3.1jar包
1
2
3
4
5
6
7 1 <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
2 <dependency>
3 <groupId>com.alibaba</groupId>
4 <artifactId>druid</artifactId>
5 <version>1.1.6</version>
6 </dependency>
7
3.2xml文件中
1
2
3
4
5
6
7
8 1 <!--阿里的Druid-->
2 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
3 <property name="driverClassName" value="${jdbc.driver}"></property>
4 <property name="url" value="${jdbc.url}"></property>
5 <property name="username" value="${jdbc.username}"></property>
6 <property name="password" value="${jdbc.password}"></property>
7 </bean>
8