Spring Boot可以帮助我们快速的搭建一个SSM框架,那建立spring boot项目之后怎样和SSM整合呢?
1.新建Model时,选择项目需要的核心依赖包
2.新建项目,建立相关对应的核心包,下图是成功之后的项目结构图
引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ssm</groupId>
<artifactId>springboot02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot02</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Students.java
package com.ssm.springboot02.model;
public class Students {
private Integer sid;
private String sname;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Students(Integer sid, String sname) {
this.sid = sid;
this.sname = sname;
}
public Students() {
}
}
StudentsServicesImpl.java
package com.ssm.springboot02.services.Impl;
import com.ssm.springboot02.mapper.StudentsMapper;
import com.ssm.springboot02.model.Students;
import com.ssm.springboot02.services.StudentsServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
//放入bean管理
@Service
public class StudentsServicesImpl implements StudentsServices {
//自动注入
@Autowired
private StudentsMapper studentsMapper;
@Override
public List<Students> getAllStudents() {
return studentsMapper.getAllStudents();
}
}
StudentsServices.java
package com.ssm.springboot02.services;
import com.ssm.springboot02.model.Students;
import java.util.List;
public interface StudentsServices {
public List<Students> getAllStudents();
}
mybatis:
StudentsMapper.java
package com.ssm.springboot02.mapper;
import com.ssm.springboot02.model.Students;
import java.util.List;
public interface StudentsMapper {
public List<Students> getAllStudents();
}
Students.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ssm.springboot02.mapper.StudentsMapper">
<select id="getAllStudents" resultType="Students">
select * from students
</select>
</mapper>
3.如果此时启动Springboot02Application文件,因为没有配置application.properties,所以会报错
此时需要配置项目启动的基本信息
第一种方式:xml方式
application.properties
#服务配置
server.port=8090
server.servlet.context-path=/ssm
#数据源配置
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/zs?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis配置
mybatis.type-aliases-package=com.ssm.springboot02.model
mybatis.mapper-locations=classpath:com/ssm/springboot02/model/*.xml
项目application.properties也可以将此文件改为application.yml
application.yml
selectStudentController.java
package com.ssm.springboot02.controller;
import com.ssm.springboot02.model.Students;
import com.ssm.springboot02.services.StudentsServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class selectStudentController {
@Autowired
private StudentsServices studentsServices;
@RequestMapping("/getStudents")
public List<Students> getAllStudents(){
System.out.println("学生集合长度:"+studentsServices.getAllStudents().size());
return studentsServices.getAllStudents();
}
}
Springboot02Application.java(一定要注意加入MapperScan)
package com.ssm.springboot02;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.ssm.springboot02.mapper")
@SpringBootApplication
public class Springboot02Application {
public static void main(String[] args) {
SpringApplication.run(Springboot02Application.class, args);
}
}