保姆级教程,小白的最爱,一个简易的Springboot项目

1.新建spring boot项目 

最后Finish就创建好了一个springboot项目  

2 .项目结构

新建一个controller层写一个测试接口:TestConterller.java

package com.hang.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping("/hello")
    public String hello(){
        return "hello world";
    }
}

运行主程序结果如下,出现hello world则项目没有问题:

3.连接数据库 

 

之后会弹出如下图界面,按从上到下顺序操作: 

完成上面操作后会出来一个数据源如下图:

 

 点击上图的0 of 8 选择你的数据库:

 

这里看到数据库内容,这边我在数据库建了一个user表,里面只有三个字段:id、姓名、密码

 点击user表可以看到表里面的内容

 现在编写application.yml配置:

spring:
  datasource:
    username: root            # 数据库账号
    password: root            # 数据库密码
    url: jdbc:mysql://localhost:3306/mybatis?serverTimeone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

4.编写接口代码

 

代码放在下面: 

UserController.java:
package com.hang.controller;

import com.hang.entity.User;
import com.hang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/query")
    public User queryUserById(){
        return userService.queryUserById(1);
    }
}
User.java:
package com.hang.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String nmae;
    private String pwd;
}
UserMapper.java:
package com.hang.mapper;

import com.hang.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface UserMapper {
    User queryUserById(int id);
}
UserService.java:
package com.hang.service;

import com.hang.entity.User;

public interface UserService {
    User queryUserById(int id);
}
UserServiceImpl.java:
package com.hang.service.impl;

import com.hang.entity.User;
import com.hang.mapper.UserMapper;
import com.hang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User queryUserById(int id) {
        return userMapper.queryUserById(id);
    }
}
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hang.mapper.UserMapper">
    <select id="queryUserById" resultType="User">
    select * from user where id = #{id};
  </select>
</mapper>

  application.yml:

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/mybatis?serverTimeone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  type-aliases-package: com.hang.entity
  mapper-locations: classpath:mapper/*.xml

后面用了mybatis和lombok需要在pom.xml添加依赖:

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

5.效果

运行主程序效果如下:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值