SpringBoot--使用JDBC連接mysql


1、導入包
    導入mysql和springJDBC的關系依賴包
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    

2、添加數據庫配置
 
 
3、創建表
 
CREATE TABLE `t_user` (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT '主鍵自增',
  `username` varchar(50) NOT NULL COMMENT '用戶名',
  `password` varchar(50) NOT NULL COMMENT '密碼',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用戶表';

4、創建對象實體及操作數據庫對象
 
  (1)實體對象
  
package com.example.demo.entity;

import lombok.Data;

@Data
public class User {
    private Long id;
    private String username;
    private String password;


}

  (2)操作數據庫對象

  

package com.example.demo.dto;


import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDto {
    @Autowired
    private final JdbcTemplate jdbcTemplate;


    public UserDto(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public int AddUser(User user){
        String sql = "insert into t_user(username, password) values(?, ?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
    }
}

 5、添加Controller和Service

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/test")
@Api(value = "SpringBoot測試接口")
public class UserTestController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping(value ="/jdbc")
    @ApiOperation(value="整合jdbc測試")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "username", value = "用戶ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "password", value = "舊密碼", required = true, dataType = "String")
    })
    public String test1(String username, String password){
        User u = new User();
        u.setUsername(username);
        u.setPassword(password);
        return userService.AddUser(u) + "";

    }

}
package com.example.demo.service;

import com.example.demo.entity.User;

public interface UserService {
    int AddUser(User user);
}
package com.example.demo.dto;


import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDto {
    @Autowired
    private final JdbcTemplate jdbcTemplate;


    public UserDto(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public int AddUser(User user){
        String sql = "insert into t_user(username, password) values(?, ?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
    }
}

 

 

6、測試

  

 

 

 

    

 

  說明:測試使用的是swagger,關於swagger的使用,請見 https://www.cnblogs.com/liconglong/p/11477401.html

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM