spring boot 创建web项目(IDEA)

220 阅读1分钟

项目地址: github.com/a851870/gir…

spring boot 项目建立(IDEA)

选中file->new->Project->Spring initializr 点击next->修改Group和Name相关资料 Dependencies 选中Web即可

链接数据库

pom.xml添加jpa

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

将application.properties文件名修改为application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver // com.mysql.jdbc.Driver 在spring boot 2.0 弃用
    url: jdbc:mysql://127.0.0.1:3306/dbgirl  // 本地数据库地址
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update // create的话每次都会重新创建数据库
    show-sql: true // 显示数据库语句在控制台

创建Girl类跟数据库保持一致的名称

@Entity
public class Girl {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    private Integer id;

    private String cupsize;

    @Min(value = 18, message = "未成年少女禁止入内")
    private Integer age;
    
    ...(getting and setting)

  • @Entity 对数据库表名的映射
@Entity
    public class UserEntity{...} 表名 user_entity  
@Entity(name="UE")  
    public class UserEntity{...} 表名 ue  
@Entity(name="UsEntity")  
    public class UserEntity{...} 表名 us_entity  

  • @Id 主键
  • @GeneratedValue(strategy = GenerationType.AUTO) id自动添加

下次主要实现,登陆保存功能的实现(cookie)