`
阅读更多
1:首先在类路径下面配置访问数据的一些基本信息,包括连接数据库的地址,用户,密码
jdbc.properties

jdbc.main.server=localhost:3306/test
jdbc.main.user=root
jdbc.main.password=123456


2:在spring的配置文件中配置NamedParameterJdbcTemplate,并且要注入DataSource,因为NamedParameterJdbcTemplate需要引用它来访问数据库

applicatonContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="$${" />
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>

<bean name="parentDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"
abstract="true">
<property name="maximumConnectionCount" value="40" />
<property name="minimumConnectionCount" value="2" />
<property name="simultaneousBuildThrottle" value="40" />
<property name="prototypeCount" value="2" />
<property name="trace" value="true" />
<property name="verbose" value="false" />
</bean>

<bean id="mainDataSource" parent="parentDataSource">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="driverUrl">
<value>jdbc:mysql://$${jdbc.main.server}?useUnicode=true&amp;characterEncoding=gbk&amp;user=$${jdbc.main.user}&amp;password=$${jdbc.main.password}&amp;zeroDateTimeBehavior=convertToNull</value>
</property>
<property name="user" value="$${jdbc.main.user}"/>
<property name="password" value="$${jdbc.main.password}"/>
<property name="alias" value="main"/>
<property name="maximumConnectionCount" value="200" />
<property name="simultaneousBuildThrottle" value="20"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg ref="mainDataSource" />
</bean>


<bean id="corporateEventDao" class="com.example.JdbcCorporateEventDao">
    <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>


<context:property-placeholder location="jdbc.properties"/>

</beans>

3:配置需要持久化的对象实体JAVA bean

public class Actor {

    private Long id;
    private String firstName;
    private String lastName;
    private int age;
    //专业
    private String specialty;
    public String getFirstName() {
        return this.firstName;
    }
   
    public String getLastName() {
        return this.lastName;
    }
   
    public Long getId() {
        return this.id;
    }
   
    // setters omitted...

}


4:定义DAO对实体对象需要的操作

public interface CorporateEventDao{

public int countOfActors(Actor exampleActor);


public long addActor(Actor exampleActor);

public boolean updateActor(long userId);

public Actor findActorById(long userId);

public List<Actor> getAllUser();

}

5:实现DAO,并且将NamedParameterJdbcTemplate注入到DAO中。

public class CorporateEventDaoImpl implements CorporateEventDao {


private NamedParameterJdbcTemplate jdbcTemplate;

public void setAppJdbc(NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private ParameterizedBeanPropertyRowMapper<Actor> rowMapper=ParameterizedBeanPropertyRowMapper.newInstance(Actor.class);


@Override
public int countOfActors(Actor exampleActor) {

String sql = "select count(*) from t_actor where first_name =fastName and last_name=:lastName";
SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor);

return this.jdbcTemplate.queryForInt(sql, namedParameters);
}

@Override
public long addActor(Actor exampleActor) {
// TODO Auto-generated method stub
String sql = "insert into t_actor(first_name,last_name,age,specialty) values(?,?,?,?)";
SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor);
return this.jdbcTemplate.update(sql, namedParameters);
}

@Override
public boolean updateActor(long userId) {
// TODO Auto-generated method stub
String sql = "update t_actor set id=?";
return this.jdbcTemplate.getJdbcOperations().update(sql,userId)>0;
}

@Override
public Actor findActorById(long userId) {
// TODO Auto-generated method stub
String sql = "select first_name,last_name,age,specialty from t_actor where userId=?";

return this.jdbcTemplate.getJdbcOperations().queryForObject(sql, new Object[]{userId}, rowMapper);
}

@Override
public List<Actor> getAllUser() {
// TODO Auto-generated method stub
String sql = "select first_name,last_name,age,specialty from t_actor";
return this.jdbcTemplate.getJdbcOperations().queryForList(sql, null, rowMapper);
}

public Actor findActor(String specialty, int age) {

    String sql = "select id, first_name, last_name from T_ACTOR" +
            " where specialty = ? and age = ?";
   
    // notice the wrapping up of the argumenta in an array
    return (Actor) jdbcTemplate.getJdbcOperations().queryForObject(sql, new Object[] {specialty, age}, rowMapper);
}

}
分享到:
评论

相关推荐

    Spring JDBC应用实例讲解

    Spring JDBC应用实例讲解 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45477185

    基于java的企业级应用开发:Spring Jdbc.ppt

    * * * * * * * Spring Jdbc Spring JDBC模块有什么作用? Spring JDBC Spring的JDBC模块负责数据库资源管理和错误处理,大大简化了开发人员对数据库的操作,使得开发人员可以从繁琐的数据库操作中解脱出来,从而将更...

    Spring+JDBC组合开发

    Spring+JDBC组合开发

    spring Security+Ehcache+quartz+swagger2+Mysql5.6+springjdbc

    以SpringBoot 为中心,模块化开发系统,用户可以随意删减除权限...复用,组装性强主要应用技术:spring Security+Ehcache+quartz+swagger2+Mysql5.6+springjdbc+druid+spring social+spring session + layerui+vue.js

    spring整合quartz使用jdbc存储

    spring整合quartz使用jdbc存储任务,并配置为quartz集群应用

    前期项目 Spring+Spring MVC+JDBC Template 增删改查

    前期项目 Spring+Spring MVC+JDBC Template 增删改查,包含建库,前端加后台

    SpringJDBC+myeclipse6+事例+工程源码.rar

    介绍Spring技术和JDBC数据库技术的应用的源代码,在myeclipse6下编译通过.

    一款极其轻量的JAVA持久层框架,基于 SpringJdbc + freemarker 实现.zip

    一款极其轻量的JAVA持久层框架,基于 SpringJdbc + freemarker 实现,具备Mybatis一样的SQL分离和逻辑标签能力,可无缝集成springboot项目。 软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发C++...

    《精通Spring2.X企业应用开发详解》随书源码1-15章

    Spring容器高级主题 第6章 Spring AOP基础 第7章 基于@AspectJ和Schema的 第7章 AOP 第3篇 数据库访问 第8章 Spring对DAO的支持 第9章 Spring的事务管理 第10章 使用Spring JDBC访问数据库 ...

    spring-jdbc sample

    Spring框架所提供的JDBC支持与其它Spring部分并非是紧耦合的,这极有利于代码的可维护性。本程序将向你展示任何直接使用JDBC(也即是,不通过一些O/R映射框架本身使用JDBC)的应用程序是如何从Spring中受益的。

    spring+jdbc整合

    spring +jdbch整合,该代码可以应用于你的项目中,只需改一下对象名字就OK了 ,好的我也不多说了,肯定对你有用,!行不行,你试试!

    Spring+3.x企业应用开发实战光盘源码(全)

     第11章:讲解了如何使用Spring JDBC进行数据访问操作,我们还重点讲述了LOB字段处理、主键产生和获取等难点知识。  第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到ORM...

    Spring-JDBC-Session:使用JDBC将Spring会话存储在数据库中的Spring Boot应用程序

    Spring-JDBC会话使用JDBC将Spring会话存储在数据库中的Spring Boot应用程序

    spring-petclinic-data-jdbc:Spring Boot Petclinic的Spring Data JDBC版本

    使用Spring Data JDBC构建的Spring PetClinic示例应用程序 这是官方应用程序的一个分支,该应用程序具有使用而非构建的域和持久层。 另外: 使用在集成测试期间启动MySQL 使用进行监视检查原始项目以了解项目简介,...

    Spring+MYBatis企业应用实战源码示例

    《Spring+MyBatis企业应用实战》适合有较好的Java 编程基础,JSP、Servlet、JDBC 基础,Spring 框架基础的读者,尤其适合于对Spring MVC 和MyBatis 了解不够深入,或对Spring MVC+MyBatis 整合开发不太熟悉的开发...

    spring 实践学习案例

    - Spring 数据库操作,包括SpringJDBC、JPA、Mybatis、MongoDB - spring-caches - Spring 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - spring-docs - Spring 文档...

    Spring插件安装图解

    具体描述 Spring: 轻量级:Spring 是非侵入性的 - ...一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)

    Struts2+spring+jdbc+mysql搭建

    Struts2+spring+jdbc+mysql环境搭建,适用于小型的web项目开发,舍弃hibernate,直接对mysql数据库进行应用,整个文档详细的介绍环境的搭建步骤

    spring4.0.zip

    Spring 是一个轻量级的开源框架, 为简化企业级应用开发而生...一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)

    小码农的代码(三)----------SpringJDBC多数据源应用

    NULL 博文链接:https://675990021.iteye.com/blog/2265799

Global site tag (gtag.js) - Google Analytics