現在創建個項目:
勾上 自已 需要東西。(這里作為演示)
1 maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
關閉項目,重新打開。
等待,依賴下載完成。
在 templates 文件夾 中 加入 一個 index.html 的文件
到這里,還要配置一下 數據庫連接(剛剛加了 jpa),我這里作為演示使用的是 Mariadb數據庫
增加,依賴...
1 implementation('org.springframework.boot:spring-boot-starter-actuator') 2 implementation('org.springframework.boot:spring-boot-starter-data-jpa') 3 implementation('org.springframework.boot:spring-boot-starter-web') 4 testImplementation('org.springframework.boot:spring-boot-starter-test') 5 implementation('org.springframework.boot:spring-boot-starter-thymeleaf')//視圖引擎 6 compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.3.0'
1 # 配置 Tomcat 端口號 2 server.port=8881 3 # 數據庫驅動 4 spring.datasource.driver-class-name=org.mariadb.jdbc.Driver 5 # 連接數據庫 6 spring.datasource.url=jdbc:mariadb://localhost:3306/test 7 # 用戶名 8 spring.datasource.username=oukele 9 # 密碼 10 spring.datasource.password=oukele 11 12 # 要標注它是那個一個數據庫,如果不標注它,它會使用MySQL的,因為我們是創建MySQL數據 13 spring.jpa.database-platform=org.hibernate.dialect.MariaDB102Dialect
在 templates 文件夾 中 新建一個 index.html 頁面
然后,啟動。。
啟動,成功
OK啦。
現在,我們去訪問數據庫,拿到數據吧。
項目結構:
entity包中的User 類
1 package com.oukele.springboot.springboot_demo2.entity; 2 3 import javax.persistence.*; 4 5 @Entity 6 @Table(name = "user")//數據庫的表名 7 public class User { 8 9 @Id 10 @GeneratedValue(strategy = GenerationType.IDENTITY)//自動增長主鍵 11 private int id; 12 13 @Column(name = "username")//數據庫的字段名,數據庫 不區分大小寫 這個 要注意 14 private String name; 15 16 private String password; 17 18 public int getId() { 19 return id; 20 } 21 22 public void setId(int id) { 23 this.id = id; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public String getPassword() { 35 return password; 36 } 37 38 public void setPassword(String password) { 39 this.password = password; 40 } 41 }
dao 包中的 UserMapper 接口
package com.oukele.springboot.springboot_demo2.dao; import com.oukele.springboot.springboot_demo2.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; public interface UserMapper extends JpaRepository<User,Integer> { }
service包中的 UserService 接口
package com.oukele.springboot.springboot_demo2.service; import com.oukele.springboot.springboot_demo2.entity.User; import java.util.List; public interface UserService { List<User> listAll(); }
serviceImp 包中的 UserServiceImp 類
package com.oukele.springboot.springboot_demo2.serviceImp; import com.oukele.springboot.springboot_demo2.dao.UserMapper; import com.oukele.springboot.springboot_demo2.entity.User; import com.oukele.springboot.springboot_demo2.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImp implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> listAll() { return userMapper.findAll(); } }
controller 包 中 的 UserController 類
1 package com.oukele.springboot.springboot_demo2.controller; 2 3 import com.oukele.springboot.springboot_demo2.entity.User; 4 import com.oukele.springboot.springboot_demo2.serviceImp.UserServiceImp; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import java.util.List; 11 12 @RestController 13 public class UserController { 14 15 @Autowired 16 private UserServiceImp userServiceImp; 17 18 @RequestMapping(path = "list",method = RequestMethod.GET) 19 public List<User> getList(){ 20 return userServiceImp.listAll(); 21 } 22 23 }
重新啟動,運行結果:
。這樣就快速完成了一個 SpringBoot項目。