2019独角兽企业重金招聘Python工程师标准>>>

1. pom.xml,需要的spring jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jd.myspring</groupId><artifactId>myspring</artifactId><version>0.0.1-SNAPSHOT</version><name>myspring</name><description>myspring</description><properties><spring-framework.version>3.2.3.RELEASE</spring-framework.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.dom4j</groupId><artifactId>com.springsource.org.dom4j</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.apache.log4j</groupId><artifactId>com.springsource.org.apache.log4j</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-framework.version}</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-aspects</artifactId>  <version>${spring-framework.version}</version>  </dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring-framework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring-framework.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version></dependency></dependencies>
</project>

2. application.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:c="http://www.springframework.org/schema/c"xmlns:cache="http://www.springframework.org/schema/cache"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:lang="http://www.springframework.org/schema/lang"xmlns:p="http://www.springframework.org/schema/p"xmlns:task="http://www.springframework.org/schema/task"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!-- 开启注解 --><context:annotation-config/><context:component-scan base-package="com.jd"/><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 加载jdbc配置文件 --><!-- <beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath*:/*.properties</value></list></property></bean> --><!-- 加载jdbc配置文件 --><context:property-placeholder location="jdbc.properties"/><!-- 定义dataSource --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><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><!-- 定义jdbcTemplate --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource" /></bean><!-- 定义 studentDao bean,注入jdbcTemplate--><bean id="studentDao" class="com.jd.dao.impl.StudentDaoImpl"><property name="template" ref="jdbcTemplate"></property></bean>
</beans>

3. StudentDao.java

package com.jd.dao;import java.util.List;public interface StudentDao<T> {public void saveStudent();public T queryStudent(int id);public List<T> queryStudentList();
}

4. StudentDaoImpl.java

package com.jd.dao.impl;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;import javax.annotation.Resource;import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;import com.jd.dao.StudentDao;
import com.jd.vo.Student;public class StudentDaoImpl<T> implements StudentDao<T>{//private DataSource dataSource;private JdbcTemplate template;/*public DataSource getDataSource() {return dataSource;}*//*** 注入dataSource* @param dataSource*//*@Resourcepublic void setDataSource(DataSource dataSource) {this.dataSource = dataSource;this.template = new JdbcTemplate(dataSource);}*/public JdbcTemplate getTemplate() {return template;}/*** 注解注入* @param template*/@Resourcepublic void setTemplate(JdbcTemplate template) {this.template = template;}public void saveStudent() {/*try {Connection conn = dataSource.getConnection();Student s = new Student();s.setId(4);s.setName("张四");String sql = "insert into student values(?,?)";PreparedStatement state = conn.prepareStatement(sql);state.setInt(1, s.getId());state.setString(2, s.getName());state.execute();System.out.println("保存学生成功");} catch (SQLException e) {e.printStackTrace();}*/Student s = new Student();s.setId(7);s.setName("张七");template.update("insert into student values(?,?)", new Object[]{s.getId(),s.getName()}, new int[]{java.sql.Types.INTEGER,java.sql.Types.VARCHAR});System.out.println("保存学生成功");}@SuppressWarnings({ "rawtypes", "unchecked" })public T queryStudent(int id) {//回调函数RowMapper rowMapper = new RowMapper(){public Object mapRow(ResultSet rs, int rowNum) throws SQLException {Student stu = new Student();stu.setId(rs.getInt(1));stu.setName(rs.getString(2));return stu;}};System.out.println("查询学生id:" + id);    return (T)template.queryForObject("select * from Student where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER}, rowMapper);}@SuppressWarnings({ "unchecked", "rawtypes" })public List<T> queryStudentList() {RowMapper rowMapper = new RowMapper(){public Object mapRow(ResultSet rs, int rowNum) throws SQLException {Student stu = new Student();stu.setId(rs.getInt(1));stu.setName(rs.getString(2));return stu;}};System.out.println("查询所有学生");    return (List<T>)template.query("select * from Student", null, null, rowMapper);}}

JdbcTemplate主要提供以下五类方法:

  • execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

  • update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;

  • query方法及queryForXXX方法:用于执行查询相关语句;

  • call方法:用于执行存储过程、函数相关语句。

    5. 测试类

  • import java.util.List;import junit.framework.TestCase;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jd.dao.StudentDao;
    import com.jd.vo.Student;public class TestSpringJdbcTemplate extends TestCase {public void testSpringJdbc(){ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");StudentDao dao =  (StudentDao)ctx.getBean("studentDao");dao.saveStudent();Student stu = (Student) dao.queryStudent(5);System.out.println("学号:" + stu.getId() + ", 姓名:" + stu.getName());List<Student> students = dao.queryStudentList();for(Student stu:students){System.out.println("学号:" + stu.getId() + ", 姓名:" + stu.getName());}}
    }

转载于:https://my.oschina.net/u/2311010/blog/403459

Spring JdbcTemplate配置相关推荐

  1. Spring JdbcTemplate + transactionTemplate 简单示例 (零配置)

    jdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...

  2. Spring 数据源配置与应用

    Spring 数据源配置与应用 Spring对数据库操作都依赖数据源. Spring有默认的数据源实现org.springframework.jdbc.datasource.DriverManager ...

  3. 转载:为什么使用ibatis而不用spring jdbcTemplate

    第一:ibatis仅仅是对jdbc薄薄的一层封装,完全不丧失sql的灵活性 第二:ibatis所有的sql都可以放在配置文件中,这样有利于sql的集中管理,特别是在sql tuning是很容易把得到所 ...

  4. 【SSM框架系列】Spring - JdbcTemplate声明式事务

    JdbcTemplate概述 以往使用jdbc时,每次都需要自己获取PreparedStatement,执行sql语句,关闭连接等操作.操作麻烦冗余,影响编码的效率. Spring把对数据库的操作在j ...

  5. Spring JdbcTemplate Curd

    curd 1. 实现步骤 2. maven dependency 3. curd代码 database: oracle dataSource: alibaba druid 1. 实现步骤 1. 导入s ...

  6. spring JdbcTemplate数据库查询实例

    使用JdbcTemplate查询数据库的例子 配置等可以看前一篇文章: Spring JdbcTemplate实例 创建数据库 可以使用下面的SQL create table A( `id` INT ...

  7. Spring JdbcTemplate实例

    简介 Spring JdbcTemplate类是Spring提供的简化数据库操作的一个类,这个类使用了模板方法模式,可以减少一些重复代码.这里主要演示一下 JdbcTemplate 的使用. 完整的代 ...

  8. Spring JdbcTemplate示例

    Spring JdbcTemplate示例 Spring JdbcTemplate是Spring JDBC包中最重要的类. 目录[ 隐藏 ] 1 Spring JdbcTemplate 1.1 Spr ...

  9. 6、Spring事务配置上篇

    一.事务简介 1.概述 1.事务在逻辑上一组操作,要么都执行(成功),要么都不执行(失败),主要是针对数据库而言的,比如MySQL.Oracle等. 2.事务是数据库提供的特性,因此可以直接通过操作数 ...

最新文章

  1. Oracle 原理:DML触发器和数据库触发器
  2. ASP.Net Mvc 发布网站 (样式+图片问题)
  3. Spring AOP 讲解(Pointcut、Before、Around、AfterReturning、After)
  4. Django 实现文件下载
  5. 凤凰系统运行linux,凤凰系统率先升级内核到Linux4.9,支持更多新硬件
  6. VGA显示原理、时序标准及相关参数
  7. 栅格模型数据编码方式
  8. Steve Hui:云联云是中国云计算的最好切入点(4月刊推荐)
  9. transform:scale实现大屏自适应
  10. 03SpringMVC的使用
  11. SpringBoot启动流程简要
  12. thinking in uml 大象 用例
  13. 实现a标签中的各种点击(onclick)事件的方法
  14. 技术干货:工欲善其事,必先利其器 阿里云数据库系列谈之一
  15. python画圣诞树代码解读_python画,圣诞树,花,爱,Python,绘图,爱心,Turtle,篇
  16. java as2_使用AS2(http)协议实现 B2B 商用数据交换 (二) [译]
  17. 生物膜仿生纳米颗粒|HEK293胚肾细胞膜复合纳米脂质体|293T胚肾细胞膜修饰纳米囊泡具有靶向功能
  18. 用自建kinetics-skeleton行为识别数据集训练st-gcn网络流程记录
  19. windows10上运行magic keyboard和magic mouse
  20. 教你正确选择光伏组件与逆变器!

热门文章

  1. Struts2返回JSON对象的方法总结
  2. Rating Methodology – Bank Loan / Facility Rating(CRISL)
  3. velocity用法简单实例说明 .
  4. Liferay门户与CAS实现SSO单点登录
  5. 积跬步,聚小流------Bootstrap学习记录(3)
  6. php 扩展apc 参数优化
  7. windows批量添加用户
  8. android 框架LoonAndroid,码农偷懒专用(2014/8/6更新)
  9. thinkphp路由的作用
  10. [BZOJ1643][Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪