springboot+jdbcTemplate的整合实现crud

本文详细介绍了如何使用Spring Boot进行一个简单的项目搭建,包括项目结构、依赖配置、Controller、Service、DAO和Model的实现,适合初学者快速上手。

一 搭建

可以参考另一种方式:https://blog.youkuaiyun.com/u011066470/article/details/88086589

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 测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值