标签:专题 dem 用户 生成 depend project origin lombok 定义
注意:数据库采用的是本机数据库,下面是建表语句及初始化数据:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT ‘主键ID‘,
`dpt_id` bigint(0) NULL DEFAULT NULL COMMENT ‘部门id‘,
`name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT ‘姓名‘,
`age` int(0) NULL DEFAULT NULL COMMENT ‘年龄‘,
`email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT ‘邮箱‘,
`head_img` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT ‘头像‘,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = ‘用户类‘ ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 1, ‘Jone‘, 18, ‘test1@baomidou.com‘, ‘e‘);
INSERT INTO `user` VALUES (2, 1, ‘Jack‘, 20, ‘test2@baomidou.com‘, ‘d‘);
INSERT INTO `user` VALUES (3, 1, ‘Tom‘, 28, ‘test3@baomidou.com‘, ‘c‘);
INSERT INTO `user` VALUES (4, 1, ‘Sandy‘, 21, ‘test4@baomidou.com‘, ‘b‘);
INSERT INTO `user` VALUES (5, 1, ‘Billie‘, 24, ‘test5@baomidou.com‘, ‘a‘);
SET FOREIGN_KEY_CHECKS = 1;
在idea中配置数据源:idea-->view-->tool windows-->database
创建工程,添加如下依赖
<!--springboot web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--springboot 操作数据库使用jpa的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql数据库连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok 一款神器,后面专题介绍-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
在创建的工程下的application.yml添加如下数据库配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo?charset=UTF-8&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
@Entity//标识这个是一个与数据库表对应的entity类
@Data//lombok神器的一个注解,后面专题介绍
@Table ( name ="user" )//这个类是与数据库的哪个表对应的
public class User implements Serializable {
private static final long serialVersionUID = 829933141479418804L;
/**
* 主键ID
*/
@Id//这个字段是数据库表的主键
@Column(name = "id" )//这个属性对应表的哪个字段
@GeneratedValue(strategy= GenerationType.IDENTITY)//主键采用数据库自增方式
private Long id;
/**
* 部门id
*/
@Column(name = "dpt_id" )
private Long dptId;
/**
* 姓名
*/
@Column(name = "name" )
private String name;
/**
* 年龄
*/
@Column(name = "age" )
private Long age;
/**
* 邮箱
*/
@Column(name = "email" )
private String email;
/**
* 头像
*/
@Column(name = "head_img" )
private String headImg;
}
@GeneratedValue
注解的strategy属性提供四种值:
@Repository//表示这个是一个操作数据库的Repository类
public interface UserJpa extends JpaRepository<User,Long> {
}
@RestController
@RequestMapping("user")
public class UserController {
@Resource
private UserJpa userJpa;
@GetMapping("findAll")//查找所有数据
public List<User> findAll(){
return this.userJpa.findAll();
}
@GetMapping("get")//按主键查找数据
public User get(@RequestParam("id")Long id){
return this.userJpa.findById(id).get();
}
}
启动工程,然后用postman测试:查询所有:
查询指定id的数据
全程没写一条sql,数据就这么查询出来了。好了,本节就到这里了。下一节介绍保存,删除,更新,自定义sql
更多原创阅读:java原创
使用Spring Data JPA 访问 Mysql 数据库-配置项
标签:专题 dem 用户 生成 depend project origin lombok 定义
原文地址:https://www.cnblogs.com/dashengjava/p/14923208.html