引入依赖 这里利用父项目定义大量依赖的版本 利用启动器引入大量依赖
<!-- 定义啦 大量依赖的版本号-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- 启动器 有大量依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- springBoot JPA的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql 连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置文件中配置当前jpa的连接属性 相关设置
hibernate.properties 配置文件
hibernate.dialect.storage_engine=innodb
application.yml配置文件
logging:
level:
com.atguigu.dao: debug # 配置日志
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.137.123:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database: mysql
show-sql: true
generate-ddl: true
hibernate:
# 1.表不在创建;2表在,类结构和表结构不一致会更新表;类结构和表结构一致,啥事不做。
ddl-auto: update
naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
server:
port: 8888
创建实体类
一个实体类就是映射一个表 根据实体类的设置 在服务器启动的时候正向创建数据表
import javax.persistence.*;
// JPA : Java Persistence API Java持久化规范 这里建议用 lombok
//声明表名
@Table(name = "t_user")
@Entity
@Data
public class User {
@Id //声明主键
@GeneratedValue(strategy = GenerationType.IDENTITY) // auto_increment 自动递增
@Column(name = "id")//对应的表字段
private Long id;
//字段名称 长度 不能为空 当前字段的值是唯一
@Column(name = "username",length = 64,nullable = false,unique = true)
private String username;
@Column(name = "password")
private String password;
//省略@Column注解,会自动映射。字段名称和成员变量名称一致。
//@Column(name = "name")
private String name;
@Transient //临时属性。不需要与表字段进行映射。表中不会创建此字段
private int age;
private String email ;
dao层代码编写
直接继承的接口 里面已经写好了基本的曾删改查
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserDao extends JpaRepository<User,Long> {
}
service层代码编写
//接口
import com.wang.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
//接口实现类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
}
controller层编写
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;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
@GetMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}