一、使用Spring框架jdbcTemplate实现数据库的增删改查

  1.数据库

  1. /*
  2. SQLyog Ultimate v8.32
  3. MySQL - 5.7.19-log : Database - infosm
  4. *********************************************************************
  5. */
  6.  
  7. /*!40101 SET NAMES utf8 */;
  8.  
  9. /*!40101 SET SQL_MODE=''*/;
  10.  
  11. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  12. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  13. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  14. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  15. CREATE DATABASE /*!32312 IF NOT EXISTS*/`infosm` /*!40100 DEFAULT CHARACTER SET utf8 */;
  16.  
  17. USE `infosm`;
  18.  
  19. /*Table structure for table `infosm_news` */
  20.  
  21. DROP TABLE IF EXISTS `infosm_news`;
  22.  
  23. CREATE TABLE `infosm_news` (
  24. `newsid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '咨询ID',
  25. `newstitle` varchar(50) DEFAULT NULL COMMENT '咨询标题',
  26. `newscontent` varchar(1000) DEFAULT NULL COMMENT '资讯内容',
  27. `clickcount` bigint(20) DEFAULT NULL COMMENT '点击数',
  28. PRIMARY KEY (`newsid`)
  29. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='新闻资讯表';
  30.  
  31. /*Data for the table `infosm_news` */
  32.  
  33. insert into `infosm_news`(`newsid`,`newstitle`,`newscontent`,`clickcount`) values (1,'大家一起来学JAVA吧','JAVA是不可或缺的程序语言',9),(2,'好好学习','天天向上',3),(3,'我最喜欢的歌曲','黯然销魂',2),(4,'哈哈哈','色调风格的',0),(5,'谁狗带','电饭锅',NULL);
  34.  
  35. /*Table structure for table `infosm_talk` */
  36.  
  37. DROP TABLE IF EXISTS `infosm_talk`;
  38.  
  39. CREATE TABLE `infosm_talk` (
  40. `tid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '评论编号',
  41. `content` varchar(200) DEFAULT NULL COMMENT '评论内容',
  42. `talktime` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '评论时间',
  43. `nid` bigint(20) DEFAULT NULL COMMENT '咨询ID',
  44. PRIMARY KEY (`tid`)
  45. ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COMMENT='评论表';
  46.  
  47. /*Data for the table `infosm_talk` */
  48.  
  49. insert into `infosm_talk`(`tid`,`content`,`talktime`,`nid`) values (1,'很不错,我想学','2017-09-26 12:54:15',1),(2,'大家一起来学吧','2017-09-26 12:54:37',1),(3,'今天考试','2017-09-26 16:15:12',2),(4,'天气很不错哦','2017-09-26 16:15:36',1),(5,'哈哈哈','2017-09-26 17:01:24',2),(6,'国庆节放假啦','2017-09-29 12:08:22',3),(7,'中秋节放假啦','2017-09-29 12:38:58',3),(8,'中秋节放假啦','2017-09-29 13:56:21',3),(9,'中秋节放假啦','2017-09-29 13:56:22',3),(10,'中秋节放假啦','2017-09-29 13:56:23',3),(11,'中秋节放假啦','2017-09-29 13:56:23',3),(12,'中秋节放假啦','2017-09-29 13:56:23',3),(13,'中秋节放假啦','2017-09-29 13:56:23',3),(14,'中秋节放假啦','2017-09-29 13:56:23',3),(15,'中秋节放假啦','2017-09-29 13:56:24',3),(16,'中秋节放假啦','2017-09-29 13:56:39',1),(17,'中秋节放假啦','2017-09-29 13:56:40',1),(18,'中秋节放假啦','2017-09-29 13:56:40',1),(19,'中秋节放假啦','2017-09-29 13:56:40',1),(20,'中秋节放假啦','2017-09-29 13:56:40',1),(21,'中秋节放假啦','2017-09-29 13:56:40',1),(22,'中秋节放假啦','2017-09-29 13:56:40',1),(23,'中秋节放假啦','2017-09-29 13:56:41',1),(24,'中秋节放假啦','2017-09-29 13:56:41',1),(25,'中秋节放假啦','2017-09-29 13:56:41',1),(26,'中秋节放假啦','2017-09-29 13:56:50',5),(27,'中秋节放假啦','2017-09-29 13:56:50',5),(28,'中秋节放假啦','2017-09-29 13:56:51',5),(29,'中秋节放假啦','2017-09-29 13:56:51',5),(30,'中秋节放假啦','2017-09-29 13:56:51',5),(31,'中秋节放假啦','2017-09-29 13:56:57',4),(32,'中秋节放假啦','2017-09-29 13:56:57',4),(33,'中秋节放假啦','2017-09-29 13:56:57',4),(34,'中秋节放假啦','2017-09-29 13:56:57',4);
  50.  
  51. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  52. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  53. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  54. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

  2.使用maven导入相关Jar包

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.3</version>
  6. <scope>test</scope>
  7. </dependency>
  8.  
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-webmvc</artifactId>
  12. <version>4.2.0.RELEASE</version>
  13. </dependency>
  14.  
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-jdbc</artifactId>
  18. <version>4.2.0.RELEASE</version>
  19. </dependency>
  20.  
  21. <dependency>
  22. <groupId>mysql</groupId>
  23. <artifactId>mysql-connector-java</artifactId>
  24. <version>5.1.43</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.alibaba</groupId>
  28. <artifactId>druid</artifactId>
  29. <version>1.0.18</version>
  30. </dependency>
  31. <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
  32. <dependency>
  33. <groupId>c3p0</groupId>
  34. <artifactId>c3p0</artifactId>
  35. <version>0.9.1.2</version>
  36. </dependency>
  37. <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
  38. <dependency>
  39. <groupId>commons-dbcp</groupId>
  40. <artifactId>commons-dbcp</artifactId>
  41. <version>1.4</version>
  42. </dependency>
  43.  
  44. </dependencies>

  3.项目框架结构

  4.接口和类的代码

    bean:

  1. public class News {
  2. private Integer newsid;
  3. private String newstitle;
  4. private String newscontent;
  5. private Integer clickcount;
  6.  
  7. public Integer getNewsid() {
  8. return newsid;
  9. }
  10.  
  11. public void setNewsid(Integer newsid) {
  12. this.newsid = newsid;
  13. }
  14.  
  15. public String getNewstitle() {
  16. return newstitle;
  17. }
  18.  
  19. public void setNewstitle(String newstitle) {
  20. this.newstitle = newstitle;
  21. }
  22.  
  23. public String getNewscontent() {
  24. return newscontent;
  25. }
  26.  
  27. public void setNewscontent(String newscontent) {
  28. this.newscontent = newscontent;
  29. }
  30.  
  31. public Integer getClickcount() {
  32. return clickcount;
  33. }
  34.  
  35. public void setClickcount(Integer clickcount) {
  36. this.clickcount = clickcount;
  37. }
  38. }
  1. import java.util.Date;
  2.  
  3. public class Talk {
  4. private Integer tid;
  5. private String content;
  6. private Date talktime;
  7. private Integer nid;
  8.  
  9. public Integer getTid() {
  10. return tid;
  11. }
  12.  
  13. public void setTid(Integer tid) {
  14. this.tid = tid;
  15. }
  16.  
  17. public String getContent() {
  18. return content;
  19. }
  20.  
  21. public void setContent(String content) {
  22. this.content = content;
  23. }
  24.  
  25. public Date getTalktime() {
  26. return talktime;
  27. }
  28.  
  29. public void setTalktime(Date talktime) {
  30. this.talktime = talktime;
  31. }
  32.  
  33. public Integer getNid() {
  34. return nid;
  35. }
  36.  
  37. public void setNid(Integer nid) {
  38. this.nid = nid;
  39. }
  40. }

    dao:

  1. import java.util.List;
  2.  
  3. public interface INewsDao {
  4. public List<News> findAll();
  5. }
  1. import cn.infosm.bean.News;
  2. import cn.infosm.dao.INewsDao;
  3. import org.springframework.jdbc.core.RowMapper;
  4. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  5.  
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.List;
  9.  
  10. public class NewsDaoImpl extends JdbcDaoSupport implements INewsDao {
  11. @Override
  12. public List<News> findAll() {
  13. String sql = "SELECT * FROM infosm_news";
  14. List<News> list = getJdbcTemplate().query(sql, new RowMapper<News>() {
  15. @Override
  16. public News mapRow(ResultSet rs, int rowNum) throws SQLException {
  17. News news = new News();
  18. news.setNewsid(rs.getInt("newsid"));
  19. news.setNewstitle(rs.getString("newstitle"));
  20. news.setNewscontent(rs.getString("newscontent"));
  21. news.setClickcount(rs.getInt("clickcount"));
  22. return news;
  23. }
  24. });
  25. return list;
  26. }
  27. }

    service:

  1. import java.util.List;
  2.  
  3. public interface INewsService {
  4. public List<News> findAll();
  5. }
  1. import java.util.List;
  2.  
  3. public class NewsServiceImpl implements INewsService {
  4. private INewsDao dao;
  5.  
  6. public INewsDao getDao() {
  7. return dao;
  8. }
  9.  
  10. public void setDao(INewsDao dao) {
  11. this.dao = dao;
  12. }
  13.  
  14. @Override
  15. public List<News> findAll() {
  16. return dao.findAll();
  17. }
  18. }

    applicationContext.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--通过bean元素生命需要Spring创建的实例。该实例的类型通过class属性指定,
  7. 并通过id属性为该实例制定一个名称,以便于访问-->
  8. <!--DataSource jdbc-->
  9. <bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  10. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  11. <property name="url" value="jdbc:mysql:///infosm"/>
  12. <property name="username" value="root"/>
  13. <property name="password" value="tengyu"/>
  14. </bean>
  15. <!--jdbcTemplate-->
  16. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  17. <property name="dataSource" ref="dateSource"/>
  18. </bean>
  19. <!--newsDao-->
  20. <bean id="newsDao" class="cn.infosm.dao.impl.NewsDaoImpl">
  21. <property name="jdbcTemplate" ref="jdbcTemplate"/>
  22. </bean>
  23. <!--newsService-->
  24. <bean id="newsService" class="cn.infosm.service.impl.NewsServiceImpl">
  25. <property name="dao" ref="newsDao"/>
  26. </bean>
  27. <!--talkDao-->
  28. <bean id="talkDao" class="cn.infosm.dao.impl.TalkDaoImpl">
  29. <property name="jdbcTemplate" ref="jdbcTemplate"/>
  30. </bean>
  31. <!--talkService-->
  32. <bean id="talkService" class="cn.infosm.service.impl.TalkServiceImpl">
  33. <property name="dao" ref="talkDao"/>
  34. </bean>
  35. </beans>

    测试类:

  1. import java.util.List;
  2.  
  3. public class NewsMapperTest {
  4. @Test
  5. public void findAllNews(){
  6. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. INewsService newsService = (INewsService) context.getBean("newsService");
  8. List<News> list = newsService.findAll();
  9. for (News news :list) {
  10. System.out.println(news.getNewstitle());
  11. }
  12.  
  13. }
  14.  
  15. @Test
  16. public void findAllTalks(){
  17. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  18. ITalkService talkService = (ITalkService) context.getBean("talkService");
  19. List<Talk> list = talkService.findAll();
  20. for (Talk talk :list) {
  21. System.out.println(talk.getTid()+talk.getContent()+talk.getTalktime());
  22. }
  23.  
  24. }
  25. }

二、修改数据源,多种数据源配置方法(applicationContext.xml中替换jdbc数据源)

  1、dbcp数据源

  1. <!--DataSource dbcp-->
  2. <!--<bean id="dateSource" class="org.apache.commons.dbcp.BasicDataSource">
  3. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  4. <property name="url" value="jdbc:mysql:///infosm"/>
  5. <property name="username" value="root"/>
  6. <property name="password" value="tengyu"/>
  7. </bean>-->

  2、c3p0数据源

  1. <!--DataSource c3p0-->
  2. <!--<bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  3. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  4. <property name="jdbcUrl" value="jdbc:mysql:///infosm"/>
  5. <property name="user" value="root"/>
  6. <property name="password" value="tengyu"/>
  7. </bean>-->

  3、druid数据源(alibaba)

  1. <!--DataSource druid-->
  2. <!--<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
  3. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  4. <property name="url" value="jdbc:mysql:///infosm"/>
  5. <property name="username" value="root"/>
  6. <property name="password" value="tengyu"/>
  7. </bean>-->

三、修改数据源时需要引入相应的架包(注意)

  maven引入依赖

Spring学习笔记:jdbcTemplate和数据源配置的更多相关文章

  1. Spring学习笔记之六(数据源的配置)

    1.前言 上一篇博客分析了,Spring中实现AOP的两种动态代理的机制,以下这篇博客.来解说一下Spring中的数据源的配置.  2.DAO支持的模板类 Spring提供了非常多关于Dao支持的模板 ...

  2. Spring Boot之JdbcTemplate多数据源配置与使用

    之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...

  3. Spring学习笔记(2)——Bean的配置

    要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...

  4. spring学习笔记一 入门及配置

    Spring是一个开源框架,为了解决企业应用开发的复杂性而创建的.主要优势之一就是其分层架构.Spring的核心是控制反转和面向切面.简单来说,Spring是一个分层的一站式轻量级开源框架. 使用Sp ...

  5. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  6. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  7. SpringBoot学习笔记:动态数据源切换

    SpringBoot学习笔记:动态数据源切换 数据源 Java的javax.sql.DataSource接口提供了一种处理数据库连接的标准方法.通常,DataSource使用URL和一些凭据来建立数据 ...

  8. Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置

    上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...

  9. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  10. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

随机推荐

  1. SFML从入门到放弃(0) 配置环境

    SFML从入门到放弃(0) 配置环境 恩..开始划水..学sfml的时候顺便做点笔记什么的.. 安装 在linux里面打开终端 然后输入 sudo apt-get install libsfml-de ...

  2. django数据模型中关于on_delete的使用

    django数据模型中关于on_delete的使用 class BookModel(models.Model): """ 书籍表 """ b ...

  3. Easyui里面动态设置输入框的可见性

    JQuery EasyUI 动态隐藏   一.隐藏datagrid某一列 $('#dg').datagrid('hideColumn', 'field'); 二.隐藏html的lable.input标 ...

  4. UIView-frame-VS-bounds

    分享链接

  5. 函数新特性、内联函数、const详解

    一.函数回顾与后置返回类型 函数定义中,形参如果在函数体内用不到的话,则可以不给形参变量名字,只给其类型. 函数声明时,可以只有形参类型,没有形参名 把函数返回类型放到函数名字之前,这种写法,叫前置返 ...

  6. 9012年,我终于找到了Pypi稳定的源....

    前情提要 近日听说华为也做了一个华为开源镜像站所以去体验了一波然后此处做个总结吧. 既然是体验当然是从直观的第一感觉UI开始说起. 界面体验 一点进去挺好的界面很清爽. 最难能可贵的是终于没有再使用列 ...

  7. CSS 两个行内块元素,宽度相加刚好等于父盒子容器的元素,但第二个元素掉在第二行解决办法

    我们可以发现:两个行内块元素,宽度相加刚好等于父盒子容器的元素,但第二个元素掉在第二行,这是什么问题呢? 我们先来看一下效果: <!DOCTYPE html> <html lang= ...

  8. [NodeJS]Jenkins-cli

    使用npm 包nestor 触发jenkins job, 达到命令行管理Jenkins功能. 1. install nestor : npm install -g nestor 2. set JENK ...

  9. history.back返回是浏览器错误码:ERR_CACHE_MISS

    解决方法: 如果访问的是php文件中添加:header("Cache-control: private"); 如果使用的是模板引擎(tp5):{php}header("C ...

  10. 导出excel设置样式(Aspose.Cells)

    Aspose.Cells.Style style = xlBook.Styles[xlBook.Styles.Add()];style1.Pattern = Aspose.Cells.Backgrou ...