spring-jdbcTemplet 连接数据源(基础)
1,我的数据库
2.我的java代码段
entity实体类
package cn.happy.spring05jdbctemplate.entity; /** * Created by Happy on 2017-08-02. */ public class Book { private Integer bookid; private String bookname; private Integer bookprice; public Integer getBookid() { return bookid; } public void setBookid(Integer bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public Integer getBookprice() { return bookprice; } public void setBookprice(Integer bookprice) { this.bookprice = bookprice; } }
我的dao层 IBookDAO
package cn.happy.spring05jdbctemplate.dao; import cn.happy.spring05jdbctemplate.entity.Book; import java.util.List; /** * Created by Happy on 2017-08-02. */ public interface IBookDAO { //01.查询素有图书 public List<Book> findAll(); }
我的dao层 IBookDAO的实现类
package cn.happy.spring05jdbctemplate.dao.impl; import cn.happy.spring05jdbctemplate.dao.IBookDAO; import cn.happy.spring05jdbctemplate.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 Happy on 2017-10-17 * dao层实现类:BookDAOImpl 先继承JdbcDaoSupport 在实现dao层接口 */ public void BookDAOImpl extends JdbcDaoSupport implements IBookDAO { public List<Book> findAll() { //需要植入 jdbctemplet String sql="select * from book"; //匿名内部类 List<Book> list=this.getJdbcTemplate().query(sql, new RowMapper<Book>() { /** * @param rs:单条记录读取器 * @param i 索引 * @return * @throws SQLException */ public Book mapRow(ResultSet rs, int i) throws SQLException { Book book=new Book(); book.setBookid(rs.getInt("bookid")); book.setBookname(rs.getString("bookname")); book.setBookprice(rs.getInt("bookprice")); return book; } }); return list; } }
我的service 接口
package cn.happy.spring05jdbctemplate.service; import cn.happy.spring05jdbctemplate.entity.Book; import java.util.List; /** * Created by Happy on 2017-08-02. */ public interface IBookService { //01.查询素有图书 public List<Book> findAll(); }
我的service的实现类
package cn.happy.spring05jdbctemplate.service.impl; import cn.happy.spring05jdbctemplate.dao.IBookDAO; import cn.happy.spring05jdbctemplate.entity.Book; import cn.happy.spring05jdbctemplate.service.IBookService; import java.util.List; /** * Created by Happy on 2017-08-02. */ public class BookServiceImpl implements IBookService { //植入dao private IBookDAO dao; public List<Book> findAll() { return dao.findAll(); } public IBookDAO getDao() { return dao; } public void setDao(IBookDAO dao) { this.dao = dao; } }
我的大配置applicationContextSpring05jdbcTemplet.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--spring的内置配置--> <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=""></property> </bean>
<!--jdbcTemplet-->
<bean id="jdbcTomplet" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--dao-->
<bean id="dao" class="cn.happy.spring05jdbctemplate.dao.impl.BookDAOImpl">
<property name="jdbcTemplate" ref="jdbcTomplet"/>
</bean>
<!--service-->
<bean id="service" class="cn.happy.spring05jdbctemplate.service.impl.BookServiceImpl">
<property name="dao" ref="dao"/>
</bean>
</beans>
我的测试类
package cn.happy.jdbcteplet; import cn.happy.spring05jdbctemplate.entity.Book; import cn.happy.spring05jdbctemplate.service.IBookService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class Test { // aspectjxml @org.junit.Test public void test04(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextSpring05jdbcTemplet.xml"); IBookService service = (IBookService) ctx.getBean("service"); List<Book> list = service.findAll(); for (Book book:list ) { System.out.println(book.getBookname()); } } }
以上写的是spring内置的Datasource。
一下还有其他的方法可以在大配置上可以连接数据源。
你只要把applicationContextSpring05jdbcTemplet.xml的配置文件中 的
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
class="org.springframework.jdbc.datasource.DriverManagerDataSource"里头的class属性改一下好就行。
!--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=""></property> </bean>--> <!--阿里巴巴的配置--> <!-- <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=""></property> </bean>--> <!--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=""></property> </bean>-->
2.还有使用jdbc.properties文件连接数据库 在把两个配置文件连接起来就行。
如下代码:
我的jdbc.properties代码:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///t14 jdbc.username=root jdbc.password=
(1)使用<!--使用contextual:property-placeholder-->
记住 在头文件里头不要忘了加入
xmlns:context="http://www.springframework.org/schema/context"和
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
由于时间原因没有详细了解,如果有时间 你们可以验证一下。
<!--使用contextual:property-placeholder--> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
使用bean节点连接jdbc.properties
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--jdbcTemplet--> <bean id="jdbcTomplet" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--dao--> <bean id="dao" class="cn.happy.spring05jdbctemplate.dao.impl.BookDAOImpl"> <property name="jdbcTemplate" ref="jdbcTomplet"/> </bean> <!--service--> <bean id="service" class="cn.happy.spring05jdbctemplate.service.impl.BookServiceImpl"> <property name="dao" ref="dao"/> </bean>
我的测试结果: