Spring JDBC
Posted 春眠不觉笑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring JDBC相关的知识,希望对你有一定的参考价值。
Spring jdbc的三种连接方式
分别引入不同的jar包。
实体类
import java.util.Date; public class Category { private Integer id; private String name; private Date createDate; public Category() { } public Category(Integer id, String name, Date createDate) { this.id = id; this.name = name; this.createDate = createDate; } @Override public String toString() { return "Category{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", createDate=" + createDate + ‘}‘; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } }
-dao层
public interface ICategoryDao { public List<Category> findAll(); }
impl
public class ICategoryDaoImpl extends JdbcDaoSupport implements ICategoryDao { public List<Category> findAll() { String sql="select * from news_category"; List<Category> list=getJdbcTemplate().query(sql, new RowMapper<Category>() { /** * * @param resultSet 结果集 * @param i 索引 * @return * @throws SQLException */ public Category mapRow(ResultSet rs, int i) throws SQLException { Category category=new Category(); category.setId(rs.getInt("id")); category.setName(rs.getString("name")); category.setCreateDate(rs.getDate("createDate")); return category; } }); return list; } }
service
public interface ICategoryService { public List<Category> findAll(); }
serviceimpl
public class CategoryServiceImpl implements ICategoryService { private ICategoryDao dao; public ICategoryDao getDao() { return dao; } public void setDao(ICategoryDao dao) { this.dao = dao; } public List<Category> findAll() { return dao.findAll(); } }
ApplicationContext.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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--DataSource 数据源 提供链接,配置数据库的四大属性--> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="jdbc:mysql:///news"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean>--> <!--c3p0--> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql:///news"></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:///news"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="username" value="root"></property> <property name="password" value=" "></property> </bean> <!--jdbcTemplate--> <bean id="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--Dao--> <bean id="categoryDao" class="cn.kitty.dao.impl.ICategoryDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--service--> <bean id="CategoryService" class="cn.kitty.service.impl.CategoryServiceImpl"> <property name="dao" ref="categoryDao"></property> </bean> </beans>
test
public class Test20171017 { @Test public void jdbc(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml"); ICategoryService service = (ICategoryService) context.getBean("CategoryService"); List<Category> list = service.findAll(); for ( Category c: list) { System.out.println(list); } } }
以上是关于Spring JDBC的主要内容,如果未能解决你的问题,请参考以下文章
Spring对JDBC的模板支持——JdbcTemplate
mysql jdbc源码分析片段 和 Tomcat's JDBC Pool
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段