spring boot写java web和接口
流程:

Springboot开发过程

还有一个是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
数据库字段:

pojo
@Data
public class User {
private int id ;
private String name;
private int age;
private String email;
*****剩下的就是get和set方法自行完成
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 注入到容器中

数据成功拿到
相关文章
Java中ScheduledExecutorService介绍和使用案例(推荐)
ScheduledExecutorService是Java并发包中的接口,用于安排任务在给定延迟后运行或定期执行,它继承自ExecutorService,具有线程池特性,可复用线程,提高效率,本文主要介绍java中的ScheduledExecutorService介绍和使用案例,感兴趣的朋友一起看看吧2024-10-10
IntelliJ IDEA 好用插件之analyze inspect code详解
这篇文章主要介绍了IntelliJ IDEA 好用插件之analyze inspect code的相关知识,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2020-12-12
详解Mybatis逆向工程中使用Mysql8.0版本驱动遇到的问题
今天在使用 8.0.12 版的 mysql 驱动时遇到了各种各样的坑。这篇文章主要介绍了详解Mybatis逆向工程中使用Mysql8.0版本驱动遇到的问题,感兴趣的小伙伴们可以参考一下2018-10-10


最新评论