SpringBoot整合jdbcTemplate特详细教程

这里主要就是写一下自己是如何使用springboot来做的JdbcTemplate的整合操作的

先说一下自己的环境吧:系统是win10的,jdk1.8(64位),mysql 是Ver 14.14 Distrib 5.5.27, for Win32 (x86)

第一步先创建一个springboot的项目

先来看一下最终的目录结构吧

SpringBoot整合jdbcTemplate特详细教程_第1张图片

pom文件



	4.0.0
	com.baidu
	demo000
	0.0.1-SNAPSHOT
	jar
	demo000
	Demo project for Spring Boot
		
			org.springframework.boot
			spring-boot-starter-parent
			1.5.15.RELEASE
			 
		
		
			UTF-8
			UTF-8
			1.8
		
		
			
				maven-ali
				http://maven.aliyun.com/nexus/content/groups/public//
				
					true
				
				
					true
					always
					fail
				
			
		
		
			
				org.springframework.boot
				spring-boot-starter-web
			
			
				org.springframework.boot
				spring-boot-starter-test
				test
			
			
				org.mybatis.spring.boot
				mybatis-spring-boot-starter
				1.2.0
			
			
				mysql
				mysql-connector-java
			
			
				com.alibaba
				druid
				1.1.6
			
			
			
				org.springframework.boot
				spring-boot-starter-jdbc
			
		
		
			
				
					org.springframework.boot
					spring-boot-maven-plugin
				
			
		
	

下面是启动类HousesApplication.java

package com.baidu.demo000;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.baidu.demo000.mapper")
public class HousesApplication {

	public static void main(String[] args) {
		SpringApplication.run(HousesApplication.class, args);
	}
}

然后开始编写配置文件application.yml

server:
  port: 9010
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_user
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

这里开始在数据库中造数据,数据库的结构如图

SpringBoot整合jdbcTemplate特详细教程_第2张图片

造数据

DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `age` int(11) NOT NULL COMMENT '年龄',
  `ctm` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', '张三', '18', '2019-01-24 09:07:41');
INSERT INTO `tb_user` VALUES ('2', '李四', '20', '2019-01-24 09:07:41');
INSERT INTO `tb_user` VALUES ('3', '王五', '19', '2019-01-24 09:07:41');

下面开始创建实体类User.java

package com.baidu.demo000.model;

import java.util.Date;

public class User {

    private int id;
    private String username;
    private int age;
    private Date ctm;

    public User() {
    }

    public User(String username, int age) {
        this.username = username;
        this.age = age;
        this.ctm = new Date();
    }

    // Getter、Setter

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getCtm() {
        return ctm;
    }

    public void setCtm(Date ctm) {
        this.ctm = ctm;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", age=" + age +
                ", ctm=" + ctm +
                '}';
    }
}

首先编写的是dao层的代码UserDao.java

package com.baidu.demo000.dao;

import com.baidu.demo000.model.User;

import java.util.List;


public interface UserDao {

    User getUserById(Integer id);

    public List getUserList();

    public int add(User user);

    public int update(Integer id, User user);

    public int delete(Integer id);
}

然后编写他的实现类UserDaoImpl.java

package com.baidu.demo000.dao.impl;

import com.baidu.demo000.dao.UserDao;
import com.baidu.demo000.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public User getUserById(Integer id) {
        List list = jdbcTemplate.query("select * from tb_user where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class));
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public List getUserList() {
        List list = jdbcTemplate.query("select * from tb_user", new Object[]{}, new BeanPropertyRowMapper(User.class));
        if(list!=null && list.size()>0){
            return list;
        }else{
            return null;
        }
    }


    @Override
    public int add(User user) {
        return jdbcTemplate.update("insert into tb_user(username, age, ctm) values(?, ?, ?)",
                user.getUsername(),user.getAge(), new Date());
    }

    @Override
    public int update(Integer id, User user) {
        return jdbcTemplate.update("UPDATE tb_user SET username = ? , age = ? WHERE id=?",
                user.getUsername(),user.getAge(), id);
    }

    @Override
    public int delete(Integer id) {
        return jdbcTemplate.update("DELETE from tb_user where id = ? ",id);
    }

}

service层的编写 UserService.java

package com.baidu.demo000.service;

import com.baidu.demo000.model.User;

import java.util.List;


public interface UserService {

    User getUserById(Integer id);

    public List getUserList();

    public int add(User user);

    public int update(Integer id, User user);

    public int delete(Integer id);
}

实现类的编写 UserServiceImpl.java

package com.baidu.demo000.service.impl;

import com.baidu.demo000.dao.UserDao;
import com.baidu.demo000.model.User;
import com.baidu.demo000.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User getUserById(Integer id) {
        return userDao.getUserById(id);
    }

    @Override
    public List getUserList() {
        return userDao.getUserList();
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int update(Integer id, User user) {
        return userDao.update(id, user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }
}

最后是表现层

UserController.java
package com.baidu.demo000.controller;

import com.baidu.demo000.model.User;
import com.baidu.demo000.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据ID查询用户
     * @param id
     * @return
     */
    @RequestMapping(value = "user/{id}", method = RequestMethod.GET)
    public User getUserById (@PathVariable(value = "id") Integer id){
        User user = new User();
        try {
            user = userService.getUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 查询用户列表
     * @return
     */
    @RequestMapping(value = "users", method = RequestMethod.GET)
    public List getUserList (){
        List users = new ArrayList<>();
        try {
            users = userService.getUserList();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return users;
    }
    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping(value = "user", method = RequestMethod.POST)
    public String add (@RequestBody User user){

        int orderId = 0;
        try {
            //添加操作返回的是受影响的行数
            orderId = userService.add(user);
            if(orderId==0){
                //添加失败
                return "添加失败";
            }else{
                return "添加成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败";
        }

    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @RequestMapping(value = "user/{id}", method = RequestMethod.DELETE)
    public String delete (@PathVariable(value = "id") Integer id){

        try {
            int ret =0;
            ret = userService.delete(id);
            if (ret <= 0) {
               return "删除失败";
            } else {
                return "删除成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }

    /**
     * 根据id修改用户信息
     * @param user
     * @return
     */
    @RequestMapping(value = "user/{id}", method = RequestMethod.PUT)
    public String update (@PathVariable("id") Integer id, @RequestBody User user){
        try {
            int ret = 0;
            ret  = userService.update(id, user);
            if (ret <= 0) {
                return "修改失败";
            } else {
                return "修改成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "修改失败";
        }
    }

}

下面开始测试,首先开启启动类

"C:\Program Files\Java\jdk1.8.0_172\bin\java" -javaagent:E:\IDEA\lib\idea_rt.jar=50851:E:\IDEA\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_172\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\rt.jar;E:\ideaProject\demo000\speingboot\target\classes;C:\Users\luozhong\.m2\repository\org\springframework\boot\spring-boot-starter-web\1.5.15.RELEASE\spring-boot-starter-web-1.5.15.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\boot\spring-boot-starter\1.5.15.RELEASE\spring-boot-starter-1.5.15.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\boot\spring-boot\1.5.15.RELEASE\spring-boot-1.5.15.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.15.RELEASE\spring-boot-autoconfigure-1.5.15.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.5.15.RELEASE\spring-boot-starter-logging-1.5.15.RELEASE.jar;C:\Users\luozhong\.m2\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;C:\Users\luozhong\.m2\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;C:\Users\luozhong\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;C:\Users\luozhong\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\luozhong\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;C:\Users\luozhong\.m2\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;C:\Users\luozhong\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\1.5.15.RELEASE\spring-boot-starter-tomcat-1.5.15.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.32\tomcat-embed-core-8.5.32.jar;C:\Users\luozhong\.m2\repository\org\apache\tomcat\tomcat-annotations-api\8.5.32\tomcat-annotations-api-8.5.32.jar;C:\Users\luozhong\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.32\tomcat-embed-el-8.5.32.jar;C:\Users\luozhong\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.32\tomcat-embed-websocket-8.5.32.jar;C:\Users\luozhong\.m2\repository\org\hibernate\hibernate-validator\5.3.6.Final\hibernate-validator-5.3.6.Final.jar;C:\Users\luozhong\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\luozhong\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\luozhong\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\luozhong\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.8.11.2\jackson-databind-2.8.11.2.jar;C:\Users\luozhong\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;C:\Users\luozhong\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.11\jackson-core-2.8.11.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-web\4.3.18.RELEASE\spring-web-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-aop\4.3.18.RELEASE\spring-aop-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-beans\4.3.18.RELEASE\spring-beans-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-context\4.3.18.RELEASE\spring-context-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-webmvc\4.3.18.RELEASE\spring-webmvc-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-expression\4.3.18.RELEASE\spring-expression-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-core\4.3.18.RELEASE\spring-core-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.2.0\mybatis-spring-boot-starter-1.2.0.jar;C:\Users\luozhong\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\1.5.15.RELEASE\spring-boot-starter-jdbc-1.5.15.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\apache\tomcat\tomcat-jdbc\8.5.32\tomcat-jdbc-8.5.32.jar;C:\Users\luozhong\.m2\repository\org\apache\tomcat\tomcat-juli\8.5.32\tomcat-juli-8.5.32.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-jdbc\4.3.18.RELEASE\spring-jdbc-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\springframework\spring-tx\4.3.18.RELEASE\spring-tx-4.3.18.RELEASE.jar;C:\Users\luozhong\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.2.0\mybatis-spring-boot-autoconfigure-1.2.0.jar;C:\Users\luozhong\.m2\repository\org\mybatis\mybatis\3.4.2\mybatis-3.4.2.jar;C:\Users\luozhong\.m2\repository\org\mybatis\mybatis-spring\1.3.1\mybatis-spring-1.3.1.jar;C:\Users\luozhong\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\luozhong\.m2\repository\com\alibaba\druid\1.1.6\druid-1.1.6.jar" com.baidu.demo000.HousesApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.15.RELEASE)

2019-01-24 14:12:53.207  INFO 8500 --- [           main] com.baidu.demo000.HousesApplication      : Starting HousesApplication on CaiCaiWoShiShei with PID 8500 (E:\ideaProject\demo000\speingboot\target\classes started by luozhong in E:\ideaProject\demo000)
2019-01-24 14:12:53.212  INFO 8500 --- [           main] com.baidu.demo000.HousesApplication      : No active profile set, falling back to default profiles: default
2019-01-24 14:12:53.299  INFO 8500 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@56de5251: startup date [Thu Jan 24 14:12:53 CST 2019]; root of context hierarchy
2019-01-24 14:12:54.300  WARN 8500 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.baidu.demo000.mapper]' package. Please check your configuration.
2019-01-24 14:12:54.643  WARN 8500 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.baidu.demo000]' package. Please check your configuration.
2019-01-24 14:12:56.073  INFO 8500 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9010 (http)
2019-01-24 14:12:56.122  INFO 8500 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-24 14:12:56.122  INFO 8500 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.32
2019-01-24 14:12:56.288  INFO 8500 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-24 14:12:56.288  INFO 8500 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2992 ms
2019-01-24 14:12:56.499  INFO 8500 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2019-01-24 14:12:56.507  INFO 8500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-01-24 14:12:56.507  INFO 8500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-01-24 14:12:56.507  INFO 8500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-01-24 14:12:56.508  INFO 8500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-01-24 14:12:57.170  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@56de5251: startup date [Thu Jan 24 14:12:53 CST 2019]; root of context hierarchy
2019-01-24 14:12:57.273  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/user],methods=[POST]}" onto public java.lang.String com.baidu.demo000.controller.UserController.add(com.baidu.demo000.model.User)
2019-01-24 14:12:57.274  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/user/{id}],methods=[PUT]}" onto public java.lang.String com.baidu.demo000.controller.UserController.update(java.lang.Integer,com.baidu.demo000.model.User)
2019-01-24 14:12:57.275  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/user/{id}],methods=[DELETE]}" onto public java.lang.String com.baidu.demo000.controller.UserController.delete(java.lang.Integer)
2019-01-24 14:12:57.276  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/user/{id}],methods=[GET]}" onto public com.baidu.demo000.model.User com.baidu.demo000.controller.UserController.getUserById(java.lang.Integer)
2019-01-24 14:12:57.277  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users],methods=[GET]}" onto public java.util.List com.baidu.demo000.controller.UserController.getUserList()
2019-01-24 14:12:57.280  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-01-24 14:12:57.284  INFO 8500 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-01-24 14:12:57.322  INFO 8500 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-24 14:12:57.322  INFO 8500 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-24 14:12:57.374  INFO 8500 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-24 14:12:57.900  INFO 8500 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-01-24 14:12:57.901  INFO 8500 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
2019-01-24 14:12:57.908  INFO 8500 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.alibaba.druid.pool:name=dataSource,type=DruidDataSource]
2019-01-24 14:12:57.958  INFO 8500 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9010 (http)
2019-01-24 14:12:57.965  INFO 8500 --- [           main] com.baidu.demo000.HousesApplication      : Started HousesApplication in 5.373 seconds (JVM running for 6.102)

控制台正常开启

下面通过Postman来做测试

第一步查询全部的操作,请求方式选择get请求,因为controller.java中对应的方法上做了请求方式的限定,下面每一个方法的请求方式都是不一样的请注意!!!!!!!

SpringBoot整合jdbcTemplate特详细教程_第3张图片

第二步添加操作,选择post的请求方式,因为在congtroller中的添加方法上做了请求方式的限定!!!

SpringBoot整合jdbcTemplate特详细教程_第4张图片

返回的结果是

SpringBoot整合jdbcTemplate特详细教程_第5张图片

重新查询全部

SpringBoot整合jdbcTemplate特详细教程_第6张图片

接着是修改操作(先看自己数据库中有哪些数据)

SpringBoot整合jdbcTemplate特详细教程_第7张图片

下面我们修改id为8的数据

SpringBoot整合jdbcTemplate特详细教程_第8张图片

查看数据库结果

SpringBoot整合jdbcTemplate特详细教程_第9张图片

接着就是最后一步的操作,删除操作(注意选择请求方式)

SpringBoot整合jdbcTemplate特详细教程_第10张图片

查看结果

SpringBoot整合jdbcTemplate特详细教程_第11张图片

也可以通过查询全部来查看

SpringBoot整合jdbcTemplate特详细教程_第12张图片

 

不会使用Postman的同学请先自学一下该软件的使用方式!

转载请指明出处,https://blog.csdn.net/weixin_42139662/article/details/86623209

支持原创哦!

SpringBoot整合jdbcTemplate特详细教程_第13张图片

 

 

你可能感兴趣的:(springboot,springboot,jdbcTemplate,postman)