Springboot创建数据库

来源:互联网 发布:centos 挂载硬盘到www 编辑:程序博客网 时间:2024/06/06 04:32

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


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



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

[html] view plain copy
 print?
  1. <span style="font-size:18px;"><strong><!--spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。-->  
  2.   <parent>  
  3.     <groupId>org.springframework.boot</groupId>  
  4.     <artifactId>spring-boot-starter-parent</artifactId>  
  5.     <version>1.4.0.RELEASE</version>  
  6.   </parent>  
  7.   
  8.   <properties>  
  9.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  10.     <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->  
  11.     <java.version>1.8</java.version>  
  12.   </properties>  
  13.   
  14.   <dependencies>  
  15.   
  16.     <!--spring-boot-starter-web: MVC,AOP的依赖包....-->  
  17.     <dependency>  
  18.       <groupId>org.springframework.boot</groupId>  
  19.       <artifactId>spring-boot-starter-web</artifactId>  
  20.       <!--<version></version>由于我们在上面指定了 parent(spring boot)-->  
  21.     </dependency>  
  22.   
  23.     <!-- 添加fastjson 依赖包. -->  
  24.     <dependency>  
  25.       <groupId>com.alibaba</groupId>  
  26.       <artifactId>fastjson</artifactId>  
  27.       <version>1.2.15</version>  
  28.     </dependency>  
  29.   
  30.     <!-- 添加MySQL数据库驱动依赖包. -->  
  31.     <dependency>  
  32.       <groupId>mysql</groupId>  
  33.       <artifactId>mysql-connector-java</artifactId>  
  34.     </dependency>  
  35.   
  36.     <!-- 添加Spring-data-jpa依赖. -->  
  37.     <dependency>  
  38.       <groupId>org.springframework.boot</groupId>  
  39.       <artifactId>spring-boot-starter-data-jpa</artifactId>  
  40.     </dependency>  
  41.   
  42.   </dependencies></strong></span>  

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

[html] view plain copy
 print?
  1. <span style="font-size:18px;"><strong>########################################################  
  2. ###datasource -- 指定mysql数据库连接信息  
  3. ########################################################  
  4. spring.datasource.url = jdbc:mysql://localhost:3306/test  
  5. spring.datasource.username = root  
  6. spring.datasource.password = 123  
  7. spring.datasource.driverClassName = com.mysql.jdbc.Driver  
  8. spring.datasource.max-active=20  
  9. spring.datasource.max-idle=8  
  10. spring.datasource.min-idle=8  
  11. spring.datasource.initial-size=10  
  12.   
  13.   
  14. ########################################################  
  15. ### Java Persistence Api --  Spring jpa 的配置信息  
  16. ########################################################  
  17. # Specify the DBMS  
  18. spring.jpa.database = MYSQL  
  19. # Show or not log for each sql query  
  20. spring.jpa.show-sql = true  
  21. # Hibernate ddl auto (create, create-drop, update)  
  22. spring.jpa.hibernate.ddl-auto = update  
  23. # Naming strategy  
  24. #[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]  
  25. spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy  
  26. # stripped before adding them to the entity manager)  
  27. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect  
  28.   
  29. </strong></span>  

SpringBoot启动类

[html] view plain copy
 print?
  1. <span style="font-size:18px;"><strong>@SpringBootApplication  
  2. public class AppLication {  
  3.     public static void main(String[] args) {  
  4.         SpringApplication.run(AppLication.class, args);  
  5.     }  
  6. }</strong></span>  

然后是创建cat的实体类

[html] view plain copy
 print?
  1. <span style="font-size:18px;"><strong>@Entity  
  2. public class Cat {  
  3.   
  4.     //使用@Id指定主键.使用代码@GeneratedValue(strategy=GenerationType.AUTO)  
  5.     //指定主键的生成策略,mysql默认的是自增长  
  6.     @Id  
  7.     @GeneratedValue(strategy = GenerationType.AUTO)  
  8.     private int id;//主键  
  9.     private String catName;//姓名  
  10.     private int catAge;//年龄  
  11.   
  12.     public int getId() {  
  13.         return id;  
  14.     }  
  15.   
  16.     public void setId(int id) {  
  17.         this.id = id;  
  18.     }  
  19.   
  20.     public String getCatName() {  
  21.         return catName;  
  22.     }  
  23.   
  24.     public void setCatName(String catName) {  
  25.         this.catName = catName;  
  26.     }  
  27.   
  28.     public int getCatAge() {  
  29.         return catAge;  
  30.     }  
  31.   
  32.     public void setCatAge(int catAge) {  
  33.         this.catAge = catAge;  
  34.     }  
  35. }</strong></span>  

Run-点击启动类



创建成功




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

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