温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot怎么写java web和接口

发布时间:2022-01-11 13:42:39 来源:亿速云 阅读:160 作者:iii 栏目:开发技术

本篇内容主要讲解“spring boot怎么写java web和接口”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring boot怎么写java web和接口”吧!

流程:

spring boot怎么写java web和接口

Springboot开发过程

spring boot怎么写java web和接口

还有一个是mybatis的依赖

测试接口

@RestController

public class Hello {
    @RequestMapping("/hello")
    public String hello(){
        return "helloworld";
    }

}

***.yml文件配置

spring:
      datasource:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/student?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
            username: root
            password: 123456
mybatis:
      mapper-locations: classpath:mapper/*.xml

数据库字段:

spring boot怎么写java web和接口

pojo

@Data
public class User {
    private  int id ;
    private String name;
    private int age;
    private String email;
    *****

剩下的就是getset方法自行完成

mapper

@Mapper
public interface UserMapper {
    List<User> findAll();
}

如果是springboot,在启动类中使用@MapperScan(“mapper接口所在包全名”)即可,不用一个一个的在Mapper接口中加@Mapper注解。@Mapper注解是识别他为mybatis的mapper接口,会自动的把 加@Mapper 注解的接口生成动态代理类。
springboot认识你的mapper层,也可以在启动类上面加MapperScan(“mapper层所在包的全名”)

mapper.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.liuyang.mapper.UserMapper">
    <select id="findAll" resultType="com.liuyang.entity.User">
        SELECT * FROM user
    </select>
</mapper>

controller

@RestController
public class UserController {

    @Autowired
    //把userService实例化
    private UserService userService;
    @RequestMapping("/user")
    public List<User> getUser(){

        return userService.findAll();

    }

}

注意一定要把userService 注入到容器中

spring boot怎么写java web和接口

数据成功拿到

到此,相信大家对“spring boot怎么写java web和接口”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI