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

添加以下依赖

数据依赖

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 Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,869评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,758评论 18 399
  • 0. 前言 本文参考的主要是Spring Boot 1.5.8官方文档,写这篇文章的原因是,在做Spring Bo...
    SYFHEHE阅读 22,516评论 0 13
  • 岸柳婀娜舞蹁跹 桃花十里却无言 风吹碧水起涟漪 一缕暗香满荷塘 霜近红枫仍争艳 冬临大地草犹新 不与群芳争娇艳 独...
    心璐依旧阅读 213评论 0 4
  • 首先接受自己的不完美。 印象最深刻的就是有时候愤怒是可以拿来保护自己的。 害怕别人对自己好,只希望自己一味的付出来...
    亲爱的吴小仙阅读 156评论 1 0