zoukankan      html  css  js  c++  java
  • IntelliJ IDEA 2017版 spring-boot使用JdbcTemplate实例

    搭建总框架:

         (1)在pom.xml加入jdbcTemplate的依赖;

         (2)编写Dao类,声明为:@Repository,引入JdbcTemplate

         (3)编写Service类,引入Dao进行使用

         (4)编写Controller进行简单测试。

    1、搭建环境pom.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5 
     6     <groupId>com.spring</groupId>
     7     <artifactId>boot_jpa</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>boot_jpa</name>
    12     <url>http://maven.apache.org</url>
    13     <description>Demo project for Spring Boot</description>
    14 
    15     <parent>
    16         <groupId>org.springframework.boot</groupId>
    17         <artifactId>spring-boot-starter-parent</artifactId>
    18         <version>1.5.9.RELEASE</version>
    19         <relativePath/> <!-- lookup parent from repository -->
    20     </parent>
    21 
    22     <properties>
    23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    24         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    25         <java.version>1.8</java.version>
    26     </properties>
    27 
    28     <dependencies>
    29         <dependency>
    30             <groupId>org.springframework.boot</groupId>
    31             <artifactId>spring-boot-starter-web</artifactId>
    32         </dependency>
    33 
    34         <dependency>
    35             <groupId>org.springframework.boot</groupId>
    36             <artifactId>spring-boot-starter-test</artifactId>
    37             <scope>test</scope>
    38         </dependency>
    39 
    40         <!--自定义配置文件-->
    41         <dependency>
    42             <groupId>com.alibaba</groupId>
    43             <artifactId>fastjson</artifactId>
    44             <version>1.2.15</version>
    45         </dependency>
    46 
    47         
    48         <!-- 添加fastjson 依赖包. -->
    49         <dependency>
    50             <groupId>com.alibaba</groupId>
    51             <artifactId>fastjson</artifactId>
    52             <version>1.2.15</version>
    53         </dependency>
    54 
    55         <!-- spring boot devtools 依赖包. -->
    56         <dependency>
    57             <groupId>org.springframework.boot</groupId>
    58             <artifactId>spring-boot-devtools</artifactId>
    59             <optional>true</optional>
    60             <scope>true</scope>
    61         </dependency>
    62 
    63         <!-- 添加MySQL数据库驱动依赖包. -->
    64         <dependency>
    65             <groupId>mysql</groupId>
    66             <artifactId>mysql-connector-java</artifactId>
    67         </dependency>
    68 
    69         <!-- 添加Spring-data-jpa依赖. -->
    70         <dependency>
    71             <groupId>org.springframework.boot</groupId>
    72             <artifactId>spring-boot-starter-data-jpa</artifactId>
    73         </dependency>
    74 
    75 
    76     </dependencies>
    77 
    78     <build>
    79         <plugins>
    80             <plugin>
    81                 <groupId>org.springframework.boot</groupId>
    82                 <artifactId>spring-boot-maven-plugin</artifactId>
    83                 <configuration>
    84                     <!--fork :  如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
    85                     <fork>true</fork>
    86                 </configuration>
    87             </plugin>
    88         </plugins>
    89     </build>
    90 
    91 
    92 </project>
    View Code

    2、搭建数据库连接

     1 #######################################################
     2 ##datasource -- 指定mysql数据库连接信息.
     3 #######################################################
     4 spring.datasource.url = jdbc:mysql://localhost:3306/test
     5 spring.datasource.username = root
     6 spring.datasource.password = 123456
     7 spring.datasource.driverClassName = com.mysql.jdbc.Driver
     8 spring.datasource.max-active=20
     9 spring.datasource.max-idle=8
    10 spring.datasource.min-idle=8
    11 spring.datasource.initial-size=10
    12 
    13 
    14 ########################################################
    15 ### Java Persistence Api --  Spring jpa的配置信息.
    16 ########################################################
    17 # Specify the DBMS
    18 spring.jpa.database = MYSQL
    19 # Show or not log for each sql query
    20 spring.jpa.show-sql = true
    21 # Hibernate ddl auto (create, create-drop, update)
    22 spring.jpa.hibernate.ddl-auto = update
    23 # Naming strategy
    24 #[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]
    25 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    26 # stripped before adding them to the entity manager)
    27 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    View Code

    3、实体类

     1 package com.spring.boot.jap.perform.pojo;
     2 
     3 import javax.persistence.Entity;
     4 import javax.persistence.GeneratedValue;
     5 import javax.persistence.GenerationType;
     6 import javax.persistence.Id;
     7 
     8 /**
     9  * Created by liuya on 2018-01-26.
    10  */
    11 @Entity
    12 public class Cat {
    13 
    14     /**
    15      * 使用@Id指定主键.
    16      * <p>
    17      * 使用代码@GeneratedValue(strategy=GenerationType.AUTO)
    18      * 指定主键的生成策略,mysql默认的是自增长。
    19      */
    20     @Id
    21     @GeneratedValue(strategy = GenerationType.AUTO)
    22     private int id;//主键.
    23 
    24     private String catName;//姓名. cat_name
    25 
    26     private int catAge;//年龄. cat_age;
    27 
    28     public int getId() {
    29         return id;
    30     }
    31 
    32     public void setId(int id) {
    33         this.id = id;
    34     }
    35 
    36     public String getCatName() {
    37         return catName;
    38     }
    39 
    40     public void setCatName(String catName) {
    41         this.catName = catName;
    42     }
    43 
    44     public int getCatAge() {
    45         return catAge;
    46     }
    47 
    48     public void setCatAge(int catAge) {
    49         this.catAge = catAge;
    50     }
    51 
    52 }
    View Code

    4、dao层

     1 package com.spring.boot.jap.perform.dao;
     2 
     3 import com.spring.boot.jap.perform.pojo.Cat;
     4 import org.springframework.jdbc.core.BeanPropertyRowMapper;
     5 import org.springframework.jdbc.core.JdbcTemplate;
     6 import org.springframework.jdbc.core.RowMapper;
     7 import org.springframework.stereotype.Repository;
     8 
     9 import javax.annotation.Resource;
    10 
    11 /**
    12  * Created by liuya on 2018-01-27.
    13  * <p>
    14  * 使用@Repository注解,标注这是一个持久化操作对象.
    15  */
    16 
    17 
    18 @Repository
    19 public class CatDao {
    20 
    21     @Resource
    22     private JdbcTemplate jdbcTemplate;
    23 
    24     /**
    25      * 1、定义一个Sql语句;
    26      * 2、定义一个RowMapper.
    27      * 3、执行查询方法.
    28      */
    29     public Cat selectByCatName(String catName) {
    30 
    31         //查询的sql语句
    32         String sql = "select * from cat where cat_name=?";
    33         //封装数据的mapper
    34         RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
    35         //执行语句的位置
    36         Cat cat = jdbcTemplate.queryForObject(sql,new Object[]{catName},rowMapper);
    37 
    38         //返回封装类
    39         return cat;
    40     }
    41 
    42 }
    View Code

    5、service层

     1 package com.spring.boot.jap.perform.service;
     2 
     3 import com.spring.boot.jap.perform.dao.CatDao;
     4 import com.spring.boot.jap.perform.pojo.Cat;
     5 import org.springframework.stereotype.Service;
     6 
     7 import javax.annotation.Resource;
     8 import javax.transaction.Transactional;
     9 
    10 /**
    11  * Created by liuya on 2018-01-27.
    12  */
    13 
    14 
    15 @Service
    16 public class CatService {
    17 
    18     @Resource
    19     private CatDao catDao;
    20 
    21     /**
    22      * 通过名称查询用户的信息
    23      *
    24      * @param catName
    25      * @return
    26      */
    27     public Cat selectByCatName(String catName) {
    28         return catDao.selectByCatName(catName);
    29     }
    30 
    31 
    32 }
    View Code

    6、controller层

     1 package com.spring.boot.jap.perform.controller;
     2 
     3 import com.spring.boot.jap.perform.pojo.Cat;
     4 import com.spring.boot.jap.perform.service.CatService;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RestController;
     7 
     8 import javax.annotation.Resource;
     9 
    10 /**
    11  * Created by liuya on 2018-01-27.
    12  */
    13 
    14 
    15 @RestController
    16 @RequestMapping("/cat")
    17 public class CatController {
    18 
    19     @Resource
    20     private CatService catService;
    21 
    22     //新建用户信息
    23     //访问连接:http://127.0.0.1:8080/cat/selectByCatName?catName=catName(使用的名字必须是唯一的)
    24     @RequestMapping("/selectByCatName")
    25     public Cat selectByCatName(String catName) {
    26         return catService.selectByCatName(catName);
    27     }
    28 
    29 
    30 }
    View Code
  • 相关阅读:
    测试方案
    测试需求
    软件测试知识点总结
    自动化测试
    测试——缺陷报告包括那些内容,由什么组成
    测试——缺陷的类型
    软件测试工具简介
    测试工程师
    剑指offer系列21--二叉搜索树的后续遍历序列
    剑指offer系列20--从上到下打印二叉树
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/8367058.html
Copyright © 2011-2022 走看看