Spring boot 数据库增删改查

(IDEA+Spring boot)

1.创建springboot 项目.(选择web项即可)。

2.配置application.properties

server.port=8080

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/(数据库名称)?useUnicode=true&characterEncoding=utf-8&useSSL=false

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS

spring.jpa.database = MYSQL

# Show or not log for each sql query

spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)

spring.jpa.hibernate.ddl-auto = update

# Naming strategy

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

3.配置pom.xml

添加以下依赖

Spring boot 数据库增删改查_第1张图片
数据依赖

4.创建student类

@Entity

public class Student {

@Id

@GeneratedValue

    private Longid;

    private Stringname;

    private Longage;

    private Integersex;

    public LonggetId() {

return id;

    }

public void setId(Long id) {

this.id = id;

    }

public StringgetName() {

return name;

    }

public void setName(String name) {

this.name = name;

    }

public LonggetAge() {

return age;

    }

public void setAge(Long age) {

this.age = age;

    }

public void setSex(Integer sex) {

this.sex = sex;

    }

public IntegergetSex() {

return sex;

    }

}

5.创建StudentRepository

@Repository

public interface StudentRepositoryextends CrudRepository {

StudentfindStudentById(Long id);

}

6.然后就可以去创建controller 增删改查啦!

代码:https://github.com/mengdongdong/springboot.git

你可能感兴趣的:(Spring boot 数据库增删改查)