1. 程式人生 > >SpringBoot--03.SpringBoot2.0整合JdbcTemplate

SpringBoot--03.SpringBoot2.0整合JdbcTemplate

 

開發環境:jdk1.8  tomcat8.5 maven工程  SpringBoot2.0

專案結構:

1、pom.xml檔案引入

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <!--springBoot父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>


    <groupId>com.day01springBoot</groupId>
    <artifactId>SpringBoot01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <!--jdk版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--web啟動器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入freeMarker的依賴包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!--jsp渲染檢視-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- jdbcTemplate 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mysql 依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 測試 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

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

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>首頁</title>
</head>
<body>
<h1>新增成功</h1>
</body>
</html>

4、測試

http://localhost:8080/adduser

資料庫記錄: