点击next就可创建成功
进入页面
,
进入页面,配置文件,application.yml(我的自动生成的是application.properties,我把他换成了yml的形式),比较喜欢这种风格,
唯一的配置文件:
spring:
freemarker:
content-type: text/html
#可以在freemarker中${request.contextPath}
request-context-attribute: request
charset: UTF-8
enabled: true
cache: false #缓存配置
#默认配置
prefix:
# suffix: .html #后缀名
template-loader-path: classpath:/templates/
settings:
#左边的'0'的个数代表整数部分最少为多少位,右边'#"的个数代表小数部分最多为多少位(四舍五入)
number_format: 0.#
boolean_format: yes,no
#识别java.sql.date
#date_format: yyyy-_MM-dd HH:mm:ss
datasource:
driver-class-name: com.mysql.jdbc.Driver
password: 1234
username: root
url: jdbc:mysql:///s
jpa:
database: mysql
database-platform: org.hibernate.dialect.MySQL57Dialect
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
然后在pom.xml中加入相应依赖,
<?xml version="1.0" encoding="UTF-8"?>
<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>lianxi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>lianxi</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<shiro-version>1.4.0</shiro-version>
<poi-version>4.0.0</poi-version>
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--JsonObject-->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro-version}</version>
<exclusions>
<exclusion>
<artifactId>ehcache-core</artifactId>
<groupId>net.sf.ehcache</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro-version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro-version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
<!--引入freemarker依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools-->
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- 属性值为true,devtools才会生效 -->
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<!-- 文件下载-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--在线预览-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 如果没有该配置,devtools不会生效 -->
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
此时可以创建实体类了:
package com.example.lianxi.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Stu {
private Integer id;
private String name;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后运行@SpringBootApplication 在创建的数据库中就可以自动生成student这个表了,同时也有了字段。
这是为什么呐??
这里用到了SpringData JPA 的技术,两处比较好用
首先在设置好数据库的连接后,根据实体类可以自动创建表和字段,在这里用到了相关的注解。还有一点特别好用,在DAO层,持久化数据层,直接继承JpaRepository<类名,主键类型>这个接口,可以省略一些方法的数据库代码,只需要声明。
项目搭建完成后,尝试写方法进行测试
dao 层
package com.example.lianxi.dao; import com.example.lianxi.entity.Stu; import org.springframework.data.jpa.repository.JpaRepository; public interface StuDao extends JpaRepository<Stu,Integer> { }
service层
package com.example.lianxi.service; import com.example.lianxi.dao.StuDao; import com.example.lianxi.entity.Stu; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class StuService { @Autowired private StuDao stuDao; public List<Stu> find(){ List<Stu> list = stuDao.findAll(); return list; } }
测试
package com.example.lianxi; import com.example.lianxi.entity.Stu; import com.example.lianxi.service.StuService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class TestStu { @Autowired private StuService stuService; @Test public void testFind(){ List<Stu> stus = stuService.find(); for (Stu s: stus) { System.out.println(s); } } }