SSM-Spring-19:Spring中JdbcTemplate
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
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
<!--spring jdbc--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency>
1.3分层开发开始,先是entity实体类层
Book
package cn.dawn.day21jdbctemplate.entity; public class Book { private Integer bookID; private String bookName; private String bookAuthor; private Integer bookPrice; @Override public String toString() { return "Book{" + "bookID=" + bookID + ", bookName='" + bookName + '\'' + ", bookAuthor='" + bookAuthor + '\'' + ", bookPrice=" + bookPrice + '}'; } public Book() { } public Integer getBookID() { return this.bookID; } public void setBookID(Integer bookID) { this.bookID = bookID; } public String getBookName() { return this.bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookAuthor() { return this.bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public Integer getBookPrice() { return this.bookPrice; } public void setBookPrice(Integer bookPrice) { this.bookPrice = bookPrice; } }
1.3dao层
一个接口IBookDAO
package cn.dawn.day21jdbctemplate.dao; import cn.dawn.day21jdbctemplate.entity.Book; import java.util.List; /** * Created by Dawn on 2018/3/13. */ public interface IBookDAO { public List<Book> findAllBooks(); }
它的实现类BookDAOImpl
package cn.dawn.day21jdbctemplate.dao; import cn.dawn.day21jdbctemplate.entity.Book; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; /** * Created by Dawn on 2018/3/13. */ public class BookDAOImpl extends JdbcDaoSupport implements IBookDAO { public List<Book> findAllBooks() { String sql="select * from book"; List<Book> lists = this.getJdbcTemplate().query(sql, new RowMapper<Book>() { public Book mapRow(ResultSet rs, int i) throws SQLException { Book book=new Book(); book.setBookAuthor(rs.getString("bookAuthor")); book.setBookID(rs.getInt("bookId")); book.setBookName(rs.getString("bookName")); book.setBookPrice(rs.getInt("bookPrice")); return book; } }); return lists; } }
这儿有几点要说的,他实现了JdbcTemplate,是为了一会给这个类注入数据源等操作,子类继承了父类的方法属性,所以这个类才能和数据库打交道
ResultSet就是数据库的单行记录,i就是此单行记录是符合你查出来的中的第几条,先把案例配通
1.4service层
一个接口IBookService
package cn.dawn.day21jdbctemplate.service; import cn.dawn.day21jdbctemplate.entity.Book; import java.util.List; /** * Created by Dawn on 2018/3/13. */ public interface IBookService { public List<Book> findAllBooks(); }
它的实现类BookService
package cn.dawn.day21jdbctemplate.service; import cn.dawn.day21jdbctemplate.dao.IBookDAO; import cn.dawn.day21jdbctemplate.entity.Book; import java.util.List; /** * Created by Dawn on 2018/3/13. */ public class BookServiceImpl implements IBookService{ private IBookDAO dao; public List<Book> findAllBooks() { return dao.findAllBooks(); } public IBookDAO getDao() { return dao; } public void setDao(IBookDAO dao) { this.dao = dao; } }
dao层的接口声明成成员变量,并且提供get/set方法,一会要注入进来dao层的实现
1.5我做了个一步到位的操作,将Driver,url,username,pwd这些jdbc连接数据库的条件提到了propertiess文件中
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///s2228 jdbc.username=root jdbc.password=
1.6就是书写大配置文件了
起名随意,一会单测方法中能合的上就行,以.xml结尾
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置jdbc。properties--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--自带的持久化框架的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置jdbctemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--dao--> <bean id="bookDao" class="cn.dawn.day21jdbctemplate.dao.BookDAOImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--service--> <bean id="bookService" class="cn.dawn.day21jdbctemplate.service.BookServiceImpl"> <property name="dao" ref="bookDao"></property> </bean> </beans>
1.7书写测试方法
package cn.dawn.day21JdbcTemplate; import cn.dawn.day21jdbctemplate.entity.Book; import cn.dawn.day21jdbctemplate.service.IBookService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * Created by Dawn on 2018/3/3. */ public class test20180313 { @Test /*aop代理工厂bean异常增强*/ public void t01(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day21JdbcTemplate.xml"); IBookService service = (IBookService) context.getBean("bookService"); List<Book> allBooks = service.findAllBooks(); for (Book item:allBooks) { System.out.println(item); } } }
案例完
下面说说另外三种数据源的配置,不同公司使用的不是完全一样,所以我提及几种,使用方法,在上方的案例中只要引入jar包,把xml中dateSource那个bean替换掉就好
1.dbcp
1.1引入jar包
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency>
1.2xml文件中的dataSource
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
2.c3p0
2.1jar包
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
2.2xml中
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
3.druid 这个是中国阿里巴巴开发的
3.1jar包
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency>
3.2xml文件中
<!--阿里的Druid--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>