java学习

java学习

 

spring的jdbc的几种实现方式

1、配置spring文件:
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 
读取配置文件
-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="classesDao" class="cn.itheima03.spring.jdbc.ClassesDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
<bean id="classesDao2" class="cn.itheima03.spring.jdbc.ClassesDaoImpl2">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
<bean id="itheimaTemplate" class="cn.itheima03.spring.itheima03db.ItHeimaTemplate">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
<bean id="classesDao4" class="cn.itheima03.spring.itheima03db.ClassesDaoImpl4">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
<bean id="classesDao3" class="cn.itheima03.spring.jdbc.ClassesDaoImpl3">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
</beans>
2、第一种方式:
public class ClassesDaoImpl extends JdbcDaoSupport implements ClassesDao{
public void saveClasses() {
this.getJdbcTemplate().execute("insert into classes(cname,description) values('a','a')");
}
@Override
public List<Classes> getClasses() {
// TODO Auto-generated method stub
return this.getJdbcTemplate().query("select * from classes", new ClassesRowMapper());
}
}
3、第二种方式:
public class ClassesDaoImpl2 implements ClassesDao{
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveClasses() {
// TODO Auto-generated method stub
this.jdbcTemplate.execute("insert into classes(cname,description) values('a','a')");
}
@Override
public List<Classes> getClasses() {
// TODO Auto-generated method stub
return null;
}
}
4.第三种方式:
public class ClassesDaoImpl3 extends JdbcTemplate implements ClassesDao{
public ClassesDaoImpl3(DataSource dataSource){
super(dataSource);
}
public void saveClasses() {
this.execute("insert into classes(cname,description) values('a','a')");
}
@Override
public List<Classes> getClasses() {
return null;
}
}
5、自定义数据库操作类:
public class ItHeimaTemplate {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public ItHeimaTemplate(){}
public ItHeimaTemplate(DataSource dataSource){
this.dataSource = dataSource;
}
public void insert(String sql){
try {
Connection conn = this.dataSource.getConnection();
Statement statement = conn.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
}
}
}
public class ClassesDaoImpl4 extends ItHeimaTemplate{
public ClassesDaoImpl4(DataSource dataSource){
super(dataSource);
}
public void insert(){
this.insert("insert into classes(cname,description) values('a','a')");
}
}

posted on 2017-08-15 16:38 杨军威 阅读(144) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜