Maven中使用spring boot开发的Web应用,使用MySql数据库,创建数据库表结构
项目很简单,不啰嗦,直接上源码
首先第一步:配置Maven中的pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 继承默认的 Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<!-- 为一个web应用程序添加典型的依赖关系 -->
<dependencies>
<!-- 添加spring boot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 对"Java持久化API"的支持,包括spring-data-jpa,spring-orm和Hibernate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 添加mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 添加Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
</dependencies>
<!-- 作为一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:配置属性文件application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
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
第三步:写实体类Eventpackage com.auqf.entity;
package com.auqf.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
/**
* 实体类
* @author auqf
*
*/
@Entity
@Table(name = "event")
public class Event implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;// id
@NotNull
private String title;// 标题
@NotNull
private String img;// 图片
@NotNull
private String file;// 文件
@NotNull
private Date date;// 日期
public Event() {
// super();
// TODO Auto-generated constructor stub
}
public Event(int id, String title, String img, String file, Date date) {
super();
this.id = id;
this.title = title;
this.img = img;
this.file = file;
this.date = date;
}
//getter、setter方法
}
最后启动应用类
package com.auqf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 应用启动类
*
* @author auqf
*
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
项目参考地址:http://www.qiyadeng.com/post/spring-boot-mysql-jpa
第一次写项目博客,有没解释清楚的地方可以留言提问,有不足的地方问题希望小伙伴多多指教
最后再为小伙伴们附上源码下载地址:http://download.csdn.net/detail/auqf_zy/9499444