
DAO:提供了对JDBC 的支持,对JDBC进行了封装。
Spring的Dao模块是Spring框架中对应持久层的解决方式,提供了对JDBC、Hibernate、JDO等DAO层支持。
Spring框架对JDBC进行了封装,完全抛弃了JDBC API。数据库连接、事务等也交给了Spring打点,开发者只需要使用封装好的JdbcTemplate执行SQL语句,然后得到需要的结果。
Spring-JDBC 提供了 Jdbc 模板类,它移除了连接代码以帮你专注于 SQL 查询和相关參数。
你仅仅需给它配置一个 DataSource,dataSource 再set给JdbcTemplate;
JdbcTemplate 和 JdbcDaoSupport的不同
JdbcTemplate 相比较原生的jdbc:不用Connection了。
JdbcDaoSupport相比较JdbcTemplate更简单,不用new JdbcTemplate或@Autowired
package jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class Test extends JdbcDaoSupport {
//JdbcDaoSupport类已经有了public final void setDataSource(DataSource dataSource)了
//不用重写也不能重写
public void insert(User u) {
String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Test t = (Test) ctx.getBean("test");
User u = new User();
u.setName("dd");
u.setPassword("dd");
t.insert(u);
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
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">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!--扫描注解-->
<context:component-scan base-package="bb"/>
<!-- 2. 创建JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
userDao
package bb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class UserDao implements IUser {
//使用Spring的自动装配
@Autowired
private JdbcTemplate template;
@Override
public void save() {
String sql = "insert into user(name,password) values('zhoggucheng','123')";
template.update(sql);
}
}
测试:
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bb/bean.xml");
UserDao userDao = (UserDao) ac.getBean("userDao");
userDao.save();
}
网友评论