SpringBoot--03.SpringBoot2.0整合JdbcTemplate

 

开发环境:jdk1.8  tomcat8.5 maven工程  SpringBoot2.0

项目结构:

SpringBoot--03.SpringBoot2.0整合JdbcTemplate_第1张图片

1、pom.xml文件引入



    4.0.0

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
    


    com.day01springBoot
    SpringBoot01
    1.0-SNAPSHOT
    war

    
    
        1.8
    

    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
        
            org.springframework.boot
            spring-boot-starter-tomcat
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        

        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

2、application.properties新增配置

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=sswqzx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3、创建相关类

Application.java

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:52 2018/11/27
 */

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

controller/AddUserContorller.java

package com.springboot.controller;

import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:51 2018/11/28
 */
@Controller
public class AddUserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/adduser")
    public String createUser(){
        userService.createUser("ssw","pswd");
        return "addu";
    }
}

service/Impl/UserServiceImpl.java

package com.springboot.service.Impl;

import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:19 2018/11/28
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void createUser(String name, String password) {
        jdbcTemplate.update("insert into user values(null, ?,?);",name,password);
    }
}

resources/templates/addu.ftl




    
    首页


添加成功

4、测试

http://localhost:8080/adduser

SpringBoot--03.SpringBoot2.0整合JdbcTemplate_第2张图片

数据库记录: 

SpringBoot--03.SpringBoot2.0整合JdbcTemplate_第3张图片

你可能感兴趣的:(SpringBoot)