软件编程
位置:首页>> 软件编程>> java编程>> spring boot写java web和接口

spring boot写java web和接口

作者:捕風捉影  发布时间:2022-10-02 09:52:02 

标签:spring,boot,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(&ldquo;mapper接口所在包全名&rdquo;)即可,不用一个一个的在Mapper接口中加@Mapper注解。@Mapper注解是识别他为mybatis的mapper接口,会自动的把 加@Mapper 注解的接口生成 * 类。
springboot认识你的mapper层,也可以在启动类上面加MapperScan(&ldquo;mapper层所在包的全名&rdquo;)

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和接口

数据成功拿到

来源:https://blog.csdn.net/qq_44833327/article/details/122390608

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com