springboot+jdbcTemplate的整合实现crud

Posted 健康平安的活着

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot+jdbcTemplate的整合实现crud相关的知识,希望对你有一定的参考价值。

一 搭建

1.1 项目结构

1.2 pom文件

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
    <!-- 以下是>spring boot依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- springboot-jdbc-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- springboot-mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>

1.3 controller

package com.ljf.spring.boot.demo.controller;

import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/18 09:31:48 
 * @Version: V1.0
 **/
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/query")
    public Object query(String name){
       return  userService.queryUser(name);
    }
}

1.4 service

1.接口层

package com.ljf.spring.boot.demo.service;

import com.ljf.spring.boot.demo.dao.UserDao;
import com.ljf.spring.boot.demo.model.UserDto;

public interface UserService {
    public UserDto queryUser(String name);
}

2.实现层

package com.ljf.spring.boot.demo.service.impl;

import com.ljf.spring.boot.demo.dao.UserDao;
import com.ljf.spring.boot.demo.model.UserDto;
import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/18 09:37:56 
 * @Version: V1.0
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Override
    public UserDto queryUser(String name) {
      return   userDao.getUserByUsername(name);
    }
}

1.5 dao层

package com.ljf.spring.boot.demo.dao;

import com.ljf.spring.boot.demo.model.UserDto;
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.List;

/**
 * @ClassName: UserDao
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/18 09:33:06 
 * @Version: V1.0
 **/
@Repository
public class UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    //根据账号查询用户信息
    public UserDto getUserByUsername(String username){
        String sql = "select id,username,password,fullname,mobile from t_user where username = ?";
        //连接数据库查询用户
        List<UserDto> list = jdbcTemplate.query(sql, new Object[]{username}, new BeanPropertyRowMapper<>(UserDto.class));
        if(list !=null && list.size()==1){
            return list.get(0);
        }
        return null;
    }
}

1.6 controller层

package com.ljf.spring.boot.demo.controller;

import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/18 09:31:48 
 * @Version: V1.0
 **/
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/query")
    public Object query(String name){
       return  userService.queryUser(name);
    }
}

1.7 model层

package com.ljf.spring.boot.demo.model;


/**
 * @author Administrator
 * @version 1.0
 **/
public class UserDto {
    private String id;
    private String username;
    private String password;
    private String fullname;
    private String mobile;

    public String getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    public String toString() {
        return "UserDto{" +
                "id='" + id + '\\'' +
                ", username='" + username + '\\'' +
                ", password='" + password + '\\'' +
                ", fullname='" + fullname + '\\'' +
                ", mobile='" + mobile + '\\'' +
                '}';
    }
}

1.8 启动类

package com.ljf.spring.boot.demo;

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

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class);
        System.out.println( "Hello World!" );
        System.out.println("springboot的template模块启动完成!!!");
    }
}

1.9 测试

 

以上是关于springboot+jdbcTemplate的整合实现crud的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot使用JdbcTemplate

springboot 整合jdbcTemplate

SpringBoot2.0 基础案例(06):引入JdbcTemplate,和多数据源配置

springboot编写持久化接口之集成jdbctemplate

SpringBoot整合使用JdbcTemplate

Springboot JdbcTemplate