Springboot创建数据库
并非是那种传授并分享知识的,只想在个人博客上把自己学的东西记录下来,也希望我记录的东西对各位看官有帮助。
今天记录的是学的一种很简单的SpringBoot创建MySQL数据库,建立的方法跟前面的一样。
大致目录如下,很简单的创建方式
首先添加必要的pom.xml依赖
- <span style="font-size:18px;"><strong><!--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></strong></span>
localhost:3306/test,username,password,对应的是数据库的名字,账号跟密码。
- <span style="font-size:18px;"><strong>########################################################
- ###datasource -- 指定mysql数据库连接信息
- ########################################################
- spring.datasource.url = jdbc:mysql://localhost:3306/test
- spring.datasource.username = root
- spring.datasource.password = 123
- spring.datasource.driverClassName = com.mysql.jdbc.Driver
- spring.datasource.max-active=20
- spring.datasource.max-idle=8
- spring.datasource.min-idle=8
- spring.datasource.initial-size=10
- ########################################################
- ### Java Persistence Api -- Spring jpa 的配置信息
- ########################################################
- # 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
- #[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
- </strong></span>
SpringBoot启动类
- <span style="font-size:18px;"><strong>@SpringBootApplication
- public class AppLication {
- public static void main(String[] args) {
- SpringApplication.run(AppLication.class, args);
- }
- }</strong></span>
然后是创建cat的实体类
- <span style="font-size:18px;"><strong>@Entity
- public 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;
- }
- }</strong></span>
Run-点击启动类
创建成功
下次是写简单的方法对Cat表进行简单的操作
有源码才能更好的理解,百度云盘