2017.05.12-1 Springboot创建数据库

来源:互联网 发布:淘宝网齐峰堂足宝粉 编辑:程序博客网 时间:2024/06/06 17:37

并非是那种传授并分享知识的,只想在个人博客上把自己学的东西记录下来,也希望我记录的东西对各位看官有帮助。


今天记录的是学的一种很简单的SpringBoot创建MySQL数据库,建立的方法跟前面的一样。
大致目录如下,很简单的创建方式



首先添加必要的pom.xml依赖

<!--spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。-->  <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.4.0.RELEASE</version>  </parent>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->    <java.version>1.8</java.version>  </properties>  <dependencies>    <!--spring-boot-starter-web: MVC,AOP的依赖包....-->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>      <!--<version></version>由于我们在上面指定了 parent(spring boot)-->    </dependency>    <!-- 添加fastjson 依赖包. -->    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>fastjson</artifactId>      <version>1.2.15</version>    </dependency>    <!-- 添加MySQL数据库驱动依赖包. -->    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>    </dependency>    <!-- 添加Spring-data-jpa依赖. -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-data-jpa</artifactId>    </dependency>  </dependencies>

然后是application.properties。
localhost:3306/test,username,password,对应的是数据库的名字,账号跟密码。

###########################################################datasource -- 指定mysql数据库连接信息########################################################spring.datasource.url = jdbc:mysql://localhost:3306/testspring.datasource.username = rootspring.datasource.password = 123spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10########################################################### Java Persistence Api --  Spring jpa 的配置信息######################################################### Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sql queryspring.jpa.show-sql = true# Hibernate ddl auto (create, create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategy#[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]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

SpringBoot启动类

@SpringBootApplicationpublic class AppLication {    public static void main(String[] args) {        SpringApplication.run(AppLication.class, args);    }}

然后是创建cat的实体类

@Entitypublic class Cat {    //使用@Id指定主键.使用代码@GeneratedValue(strategy=GenerationType.AUTO)    //指定主键的生成策略,mysql默认的是自增长    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private int id;//主键    private String catName;//姓名    private int catAge;//年龄    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getCatName() {        return catName;    }    public void setCatName(String catName) {        this.catName = catName;    }    public int getCatAge() {        return catAge;    }    public void setCatAge(int catAge) {        this.catAge = catAge;    }}

Run-点击启动类



创建成功




下次是写简单的方法对Cat表进行简单的操作

有源码才能更好的理解,百度云盘
插眼传送
0 0
原创粉丝点击