【超详细图文教程】用SpringBoot+Maven搭建SSM框架

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

第一步:新建项目






第二步:配置pom.xml



	4.0.0

	com.example
	demo
	0.0.1-SNAPSHOT
	jar

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.7.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-aop
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.1
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	




第三步:创建数据库



第四步:创建目录



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

Student.java

package com.example.models;

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}



第六步:创建Mapper接口

StudentMapper.java

package com.example.Dao;

import com.example.models.Student;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentMapper {
    @Select("SELECT * FROM student WHERE id=#{id}")
     Student getStudentByID(int id);
}



第七步:创建Controller

StudentController.java

package com.example.Controller;

import com.example.Dao.StudentMapper;
import com.example.models.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/demo")
    public Student get(){
        Student student=studentMapper.getStudentByID(2);
        return  student;
    }

    @RequestMapping(value = "res")
    public String df(){
        return "Hello";
    }
}

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


第八步:修改主程序

DemoApplication.java

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
@ComponentScan("com.example.Controller")
@MapperScan("com.example.Dao")

public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

第九步:最后还有个SpringBoot的配置文件

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/world
spring.datasource.username=root
spring.datasource.password=a8996855439
spring.datasource.driverClassName=com.mysql.jdbc.Driver
mybatis.type-aliases-package=com.example.models

最后运行 DemoApplication,在地址栏输入http://localhost:8080/demo

结果如下,就成功了



你可能感兴趣的:(Spring)