Idea下Spring Boot、thymeleaf、Jpa項目整合實例html
1、建立項目java
JDK選擇1.8便可。mysql
選中Web下的web、Template Engines下的Thymeleaf、SQL下的JPA和MySQLweb
而後點擊Next,下一界面中,直接點擊Finishspring
2、修改Mydemo2Applicationsql
package com.example.mydemo2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class Mydemo2Application { public static void main(String[] args) { SpringApplication.run(Mydemo2Application.class, args); } }
3、編寫控制器數據庫
1.在com.example.mydemo2下新建package,名爲controller瀏覽器
2.在controller新建控制器(Java類),命名爲HelloController,其代碼以下:app
package com.example.mydemo2.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") public String hello(){ return "Hello Spring-Boot"; } }
3.執行程序ide
4.打開瀏覽器,在地址欄中輸入:http://localhost:8080,出現以下頁面
代表,環境配置成功
4、配置數據庫鏈接信息
1.打開application.properties,該文件在resources下,配置內容以下:
####datasource############### spring.datasource.url = jdbc:mysql://localhost:3306/db_student spring.datasource.username = root spring.datasource.password = 123456 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 #################################
2.修改MyDemo2Application文件,作以下修改.
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //將上句修改成@SpringBootApplication
5、訪問數據庫
1.在src、java下建立package,「com.example.entity",並建立實體類"StudentInfo"代碼以下:
import javax.persistence.Table; @Entity @Table(name="tb_studentinfo") public class StudentInfo { @Id private int id; private String name; private int age; private String address; public StudentInfo(){} //此處省略了其get和set方法
2.建立數據訪問層Dao層,在 src、java下新建package,"com.example.dao",並建立接口StudentDao,代碼以下:
package com.example.dao; import com.example.entity.StudentInfo; import org.springframework.data.jpa.repository.JpaRepository; public interface StudentDao extends JpaRepository<StudentInfo,Integer>{ }
3.建立事務層接口StudentService
package com.example.mydemo2.service; import com.example.mydemo2.entity.StudentInfo; import java.util.List; public interface StudentService { List<StudentInfo> getAllStudents(); }
3.建立事務層StudentServiceImpl類,實現StudentService接口
package com.example.mydemo2.service.impl; import com.example.mydemo2.dao.StudentDao; import com.example.mydemo2.entity.StudentInfo; import com.example.mydemo2.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; @Override public List<StudentInfo> getAllStudents() { return studentDao.findAll(); } }
6、建立控制器,測試數據庫訪問
1.在controller下新建StudentController控制器,代碼以下:
package com.example.mydemo2.controller; import com.example.mydemo2.entity.StudentInfo; import com.example.mydemo2.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/showInfo") public String showInfo(){ List<StudentInfo> stus= studentService.getAllStudents(); String name="姓名:"; for(StudentInfo stu:stus){ name=name+stu.getName(); } return name; } }
2.打開瀏覽器,輸入localhost:8080/showInfo,結果以下
7、使用thymeleaf視圖層
1.在resources下的templates下建立文件夾(Directory)student,並在其中建立StudentsInfo.html文件,內容以下:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>顯示全部學生信息</title> <style> table,tr,td{ border: 1px solid #115588; } </style> </head> <body> <table> <thead> <tr> <td>編號</td> <td>姓名</td> <td>年齡</td> <td>地址</td> </tr> </thead> <tbody> <tr th:each="stu:${stus}"> <td th:text="${stu.getId()}"></td> <td th:text="${stu.getName()}"></td> <td th:text="${stu.getAge()}"></td> <td th:text="${stu.getAddress()}"></td> </tr> </tbody> </table> </body> </html>
2.修改StudentController控制器以下所示
package com.example.mydemo2.controller; import com.example.mydemo2.entity.StudentInfo; import com.example.mydemo2.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Controller public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/showInfo") public String showInfo(Model model){ List<StudentInfo> stus= studentService.getAllStudents(); model.addAttribute("stus",stus); return "/student/StudentsInfo"; } }
3.運行程序,打開瀏覽器,輸入localhost:8080/showInfo,結果以下所示