spring JDBC的应用
原文地址:https://www.iteye.com/blog/chen106106-1574911
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&characterEncoding=gbk&user=$${jdbc.main.user}&password=$${jdbc.main.password}&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 查询结果返回对象、对象列表
首先,需要了解spring jdbc查询时,有三种回调方式来处理查询的结果集.可以参考 使用spring的JdbcTemplate进行查询的三种回调方式的比较,写得还不错. 1.返回对象(queryF ...
- spring jdbc获取插入记录的主键id
在JDBC3.0规范中,当新增记录时,允许将数据库自动产生的主键值绑定到Statement或PreparedStatement中.使用Statement时,可以通过以下方法绑定主键值: int exe ...
- Spring JDBC实现查询
1 db.properties jdbc.user=root jdbc.password=920614 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbc ...
- Spring JDBC
转载:博客主页:http://blog.csdn.NET/chszs 一.概述 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1)core即核心包,它包含了JDBC的核心功能.此包内 ...
- Spring学习进阶(四) Spring JDBC
Spring JDBC是Spring所提供的持久层技术.主要目的是降低使用JDBC API的门槛,以一种更直接,更简洁的方式使用JDBC API.在Spring JDBC里用户仅需要做哪些比不可少的事 ...
- Spring JDBC常用方法详细示例
Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...
- Spring JDBC 访问MSSQL
在Spring中对底层的JDBC做了浅层的封装即JdbcTemplate,在访问数据库的DAO层完全可以使用JdbcTemplate完成任何数据访问的操作,接下来我们重点说说Spring JDBC对S ...
- Spring JDBC主从数据库配置
通过昨天学习的自定义配置注释的知识,探索了解一下web主从数据库的配置: 背景:主从数据库:主要是数据上的读写分离: 数据库的读写分离的好处? 1. 将读操作和写操作分离到不同的数据库上,避免主服务器 ...
- Spring笔记——Spring+JDBC组合开发
使用Spring+JDBC集成步骤如下: 1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...
- Spring入门(10)-Spring JDBC
Spring入门(10)-Spring JDBC 0. 目录 JdbcTemplate介绍 JdbcTemplate常见方法 代码示例 参考资料 1. JdbcTemplate介绍 JdbcTempl ...
随机推荐
- C#实现高性能高并发Socket服务器
1.高并发服务器实现一 本文转载 转载地址 2.高并发服务器实现二 本文转载 转载内容在于学习C#实现的高并发服务器 以下个人观点 1 需要注意SocketAsyncEventArgs的使用 2 做到 ...
- C#根据流下载文件
C#从服务器下载文件可以使用下面4个方法:TransmitFile.WriteFile.WriteFile和流方式下载文件,并保存为相应类型,方法如下: .TransmitFile实现下载 prote ...
- 轻量级ORM《sqlcommon》第一个版本发布了!!!
一.sqlcommon的特色 1. 轻量级,整个包只有123kb. 2. 性能好,自测... 3. API和功能简单.代码简短.可维护性好基本都能看懂.这个点我认为很重要,你不用为了实现一个需求而四处 ...
- asp.net core 在centeros 7.x下创建服务
Netcore服务生成说明 如有个项目/opt/wwwroot/dpms.1633.com 启动为/usr/bin/dotnet /opt/wwwroot/dpms.1633.com/DPMS.Web ...
- Windows下VS Code打开黑屏解决办法(这样真的行)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_42421611/article/d ...
- Solr搜索器的特性及相关操作
一.搜索处理器简介 所有的请求处理器都实现一个Java类,本例实现了solr.SearchHandler.在运行时,solr.SearchHandler被解析为内置的Solr类org.apache.s ...
- OL8.0静默安装Oracle 19C
首先在edelivery中下载Oracle Linux 8.0 然后就默认安装系统 环境准备工具目前不支持OL8,所以需要手动安装,首先设置内核参数,在/etc/sysctl.conf追加 [root ...
- Django使用Mysql已存在数据表的方法
在mysql数据库中已经存在有数据的表,自己又不想删除,下面方法可以同步django中创建的表 1.最好将自己建的表名改为前缀和django自动创建表名前缀相同,不改也可以,但是后期表太多容易混乱 2 ...
- 使用docker搭建redis-cluster环境
目录 基础环境信息 搭建步骤 搭建中遇到的问题 其他参考 临时接到一个测试任务,而测试需要用到redis-cluster环境,却没有现成的环境可用,于是只能自力更生搭建测试环境.一开始想采用在 ...
- Linux操作系统安全-证书的申请原理
Linux操作系统安全-证书的申请原理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.APR的中间人攻击 如下图所示,如果在client和server端有一个中间人攻击就比较麻 ...