1. 程式人生 > >1112_超詳細圖文教程_用SpringBoot+Maven搭建SSM框架

1112_超詳細圖文教程_用SpringBoot+Maven搭建SSM框架

【超詳細圖文教程】用SpringBoot+Maven搭建SSM框架

2017年10月09日 11:31:51 零薄獄 閱讀數:10386 標籤: Spring SpringMVC intellij idea SpringBoot SSM 更多

個人分類: Spring

專案用Inteli做的,

第一步:新建專案

 

第二步:配置pom.xml

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <groupId>com.example</groupId>

  7. <artifactId>demo</artifactId>

  8. <version>0.0.1-SNAPSHOT</version>

  9. <packaging>jar</packaging>

  10.  
  11. <name>demo</name>

  12. <description>Demo project for Spring Boot</description>

  13.  
  14. <parent>

  15. <groupId>org.springframework.boot</groupId>

  16. <artifactId>spring-boot-starter-parent</artifactId>

  17. <version>1.5.7.RELEASE</version>

  18. <relativePath/> <!-- lookup parent from repository -->

  19. </parent>

  20.  
  21. <properties>

  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  24. <java.version>1.8</java.version>

  25. </properties>

  26.  
  27. <dependencies>

  28. <dependency>

  29. <groupId>org.springframework.boot</groupId>

  30. <artifactId>spring-boot-starter-aop</artifactId>

  31. </dependency>

  32. <dependency>

  33. <groupId>org.springframework.boot</groupId>

  34. <artifactId>spring-boot-starter-jdbc</artifactId>

  35. </dependency>

  36. <dependency>

  37. <groupId>org.mybatis.spring.boot</groupId>

  38. <artifactId>mybatis-spring-boot-starter</artifactId>

  39. <version>1.3.1</version>

  40. </dependency>

  41. <dependency>

  42. <groupId>org.springframework.boot</groupId>

  43. <artifactId>spring-boot-starter-web</artifactId>

  44. </dependency>

  45.  
  46. <dependency>

  47. <groupId>mysql</groupId>

  48. <artifactId>mysql-connector-java</artifactId>

  49. <scope>runtime</scope>

  50. </dependency>

  51. <dependency>

  52. <groupId>org.springframework.boot</groupId>

  53. <artifactId>spring-boot-starter-test</artifactId>

  54. <scope>test</scope>

  55. </dependency>

  56. </dependencies>

  57.  
  58. <build>

  59. <plugins>

  60. <plugin>

  61. <groupId>org.springframework.boot</groupId>

  62. <artifactId>spring-boot-maven-plugin</artifactId>

  63. </plugin>

  64. </plugins>

  65. </build>

  66.  
  67.  
  68. </project>

  69.  

 

 

第三步:建立資料庫

 

第四步:建立目錄


第五步:建立Student.java實體類

Student.java

 

 
  1. package com.example.models;

  2.  
  3. public class Student {

  4. private int id;

  5. private String name;

  6. private String sex;

  7. private int age;

  8.  
  9. public int getId() {

  10. return id;

  11. }

  12.  
  13. public void setId(int id) {

  14. this.id = id;

  15. }

  16.  
  17. public String getName() {

  18. return name;

  19. }

  20.  
  21. public void setName(String name) {

  22. this.name = name;

  23. }

  24.  
  25. public String getSex() {

  26. return sex;

  27. }

  28.  
  29. public void setSex(String sex) {

  30. this.sex = sex;

  31. }

  32.  
  33. public int getAge() {

  34. return age;

  35. }

  36.  
  37. public void setAge(int age) {

  38. this.age = age;

  39. }

  40.  
  41. @Override

  42. public String toString() {

  43. return "Student{" +

  44. "id=" + id +

  45. ", name='" + name + '\'' +

  46. ", sex='" + sex + '\'' +

  47. ", age=" + age +

  48. '}';

  49. }

  50. }

 

 


第六步:建立Mapper介面

StudentMapper.java

 
  1. package com.example.Dao;

  2.  
  3. import com.example.models.Student;

  4. import org.apache.ibatis.annotations.Select;

  5. import org.springframework.stereotype.Repository;

  6.  
  7. @Repository

  8. public interface StudentMapper {

  9. @Select("SELECT * FROM student WHERE id=#{id}")

  10. Student getStudentByID(int id);

  11. }

 

 

 

第七步:建立Controller

StudentController.java

 

 
  1. package com.example.Controller;

  2.  
  3. import com.example.Dao.StudentMapper;

  4. import com.example.models.Student;

  5. import org.springframework.beans.factory.annotation.Autowired;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.RestController;

  8.  
  9. @RestController

  10. public class StudentController {

  11. @Autowired

  12. private StudentMapper studentMapper;

  13.  
  14. @RequestMapping("/demo")

  15. public Student get(){

  16. Student student=studentMapper.getStudentByID(2);

  17. return student;

  18. }

  19.  
  20. @RequestMapping(value = "res")

  21. public String df(){

  22. return "Hello";

  23. }

  24. }

 

(注意這邊新建介面物件的時候會出錯誤提示,但是不影響正常執行,原因不明,如果有哪位知道怎麼解決請告知)

 

第八步:修改主程式

DemoApplication.java

 

 
  1. package com.example.demo;

  2.  
  3. import org.mybatis.spring.annotation.MapperScan;

  4. import org.springframework.boot.SpringApplication;

  5. import org.springframework.boot.autoconfigure.SpringBootApplication;

  6. import org.springframework.context.annotation.ComponentScan;

  7. import org.springframework.transaction.annotation.EnableTransactionManagement;

  8.  
  9. @SpringBootApplication

  10. @EnableTransactionManagement

  11. @ComponentScan("com.example.Controller")

  12. @MapperScan("com.example.Dao")

  13.  
  14. public class DemoApplication {

  15.  
  16. public static void main(String[] args) {

  17. SpringApplication.run(DemoApplication.class, args);

  18. }

  19. }


第九步:最後還有個SpringBoot的配置檔案

application.properties

 

 
  1. spring.datasource.url=jdbc:mysql://localhost:3306/world

  2. spring.datasource.username=root

  3. spring.datasource.password=a8996855439

  4. spring.datasource.driverClassName=com.mysql.jdbc.Driver

  5. mybatis.type-aliases-package=com.example.models


最後執行DemoApplication,在位址列輸入http://localhost:8080/demo

結果如下,就成功了