SpringBoot搭建SSM




项目用Inteli做的,Eclipse的同学可以做参考

第一步:新建项目

SpringBoot搭建SSM_第1张图片

SpringBoot搭建SSM_第2张图片

SpringBoot搭建SSM_第3张图片

SpringBoot搭建SSM_第4张图片


第二步:配置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. <groupId>com.example groupId>
  6. <artifactId>demo artifactId>
  7. <version>0.0.1-SNAPSHOT version>
  8. <packaging>jar packaging>
  9. <name>demo name>
  10. <description>Demo project for Spring Boot description>
  11. <parent>
  12. <groupId>org.springframework.boot groupId>
  13. <artifactId>spring-boot-starter-parent artifactId>
  14. <version>1.5.7.RELEASE version>
  15. <relativePath/>
  16. parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8 project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8 project.reporting.outputEncoding>
  20. <java.version>1.8 java.version>
  21. properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot groupId>
  25. <artifactId>spring-boot-starter-aop artifactId>
  26. dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot groupId>
  29. <artifactId>spring-boot-starter-jdbc artifactId>
  30. dependency>
  31. <dependency>
  32. <groupId>org.mybatis.spring.boot groupId>
  33. <artifactId>mybatis-spring-boot-starter artifactId>
  34. <version>1.3.1 version>
  35. dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot groupId>
  38. <artifactId>spring-boot-starter-web artifactId>
  39. dependency>
  40. <dependency>
  41. <groupId>mysql groupId>
  42. <artifactId>mysql-connector-java artifactId>
  43. <scope>runtime scope>
  44. dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot groupId>
  47. <artifactId>spring-boot-starter-test artifactId>
  48. <scope>test scope>
  49. dependency>
  50. dependencies>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot groupId>
  55. <artifactId>spring-boot-maven-plugin artifactId>
  56. plugin>
  57. plugins>
  58. build>
  59. project>


第三步:创建数据库

SpringBoot搭建SSM_第5张图片


第四步:创建目录

SpringBoot搭建SSM_第6张图片


第五步:创建Student.java实体类

Student.java


   
   
   
   
  1. package com.example.models;
  2. public class Student {
  3. private int id;
  4. private String name;
  5. private String sex;
  6. private int age;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public String getSex() {
  20. return sex;
  21. }
  22. public void setSex(String sex) {
  23. this.sex = sex;
  24. }
  25. public int getAge() {
  26. return age;
  27. }
  28. public void setAge(int age) {
  29. this.age = age;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Student{" +
  34. "id=" + id +
  35. ", name='" + name + '\'' +
  36. ", sex='" + sex + '\'' +
  37. ", age=" + age +
  38. '}';
  39. }
  40. }



第六步:创建Mapper接口

StudentMapper.java


   
   
   
   
  1. package com.example.Dao;
  2. import com.example.models.Student;
  3. import org.apache.ibatis.annotations.Select;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface StudentMapper {
  7. @Select( "SELECT * FROM student WHERE id=#{id}")
  8. Student getStudentByID(int id);
  9. }



第七步:创建Controller

StudentController.java


   
   
   
   
  1. package com.example.Controller;
  2. import com.example.Dao.StudentMapper;
  3. import com.example.models.Student;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class StudentController {
  9. @Autowired
  10. private StudentMapper studentMapper;
  11. @RequestMapping( "/demo")
  12. public Student get(){
  13. Student student=studentMapper.getStudentByID( 2);
  14. return student;
  15. }
  16. @RequestMapping(value = "res")
  17. public String df(){
  18. return "Hello";
  19. }
  20. }

(注意这边新建接口对象的时候会出错误提示,但是不影响正常运行,原因不明,如果有哪位知道怎么解决请告知)


第八步:修改主程序

DemoApplication.java


   
   
   
   
  1. package com.example.demo;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.transaction.annotation.EnableTransactionManagement;
  7. @SpringBootApplication
  8. @EnableTransactionManagement
  9. @ComponentScan( "com.example.Controller")
  10. @MapperScan( "com.example.Dao")
  11. public class DemoApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(DemoApplication.class, args);
  14. }
  15. }

第九步:最后还有个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

结果如下,就成功了

SpringBoot搭建SSM_第7张图片





你可能感兴趣的:(Spring)