spring boot 搭建ssm

1,新建一个project (springboot-demo)

1)new一个Module(ssm项目), 用 spring boot搭建(主要是须要
application.properties配置文件 和 测试类SshApplication),需要连网

SshApplication类如下:

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

@SpringBootApplication
public class SshApplication {

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

}


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2,配置ssm项目的jar依赖(删除自动建立的依赖,删除继承的)

	<!--继承的父类,不需要,自己编写-->
    <!--<parent>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-parent</artifactId>-->
        <!--<version>2.1.1.RELEASE</version>-->
        <!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    <!--</parent>-->
    
	<!--定义变量-->
    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.1.0.RELEASE</spring.boot.version>
        <mybatis.spring.boot.version>1.3.2</mybatis.spring.boot.version>
        <mysql.driver>5.1.46</mysql.driver>
    </properties>
    
1)spring
	<!--spring 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        
2)mysql驱动包
	<!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.driver}</version>
        </dependency>
        
3)mybatis,mybatis-spring,spring-jdbc
	<!--mybatis,mybatis-spring,spring-jdbc-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.version}</version>
        </dependency>
        
4)spring web,spring mvc
	<!--spring mvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        
5)spring boot测试包
	<!--测试包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

3,编写dao,service,控制器
1)控制器编写:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//服务器启动访问页面
@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(){
        return "login";
    }
}

4,在spring boot配置文件(application.properties或者application.vml)完成配置

1)dataSource(驱动,URL,用户名,密码)
	
	#dataSource
	spring.datasource.driver-class-name=com.mysql.jdbc.Driver
	spring.datasource.url=jdbc:mysql://localhost:3306/dubbo-smbms
	spring.datasource.username=root
	spring.datasource.password=123456
	
2)@Mapper,@Service,@Controller等注解spring boot会自动扫描,不做任何配置,
	测试类(SsmApplication)的@SpringBootApplication注解里面的
	@ComponentScan注解会自动扫描同级或者低级的所有类
	
3)spring mvc配置(视图解析器),注解驱动不配置
	
	#spring mvc
	spring.mvc.view.prefix=/WEB-INF/view/              //前缀
	spring.mvc.view.suffix=.jsp						//后缀
	
4)web.xml

	#spring字符集
	spring.http.encoding.charset=UTF-8
	spring.http.encoding.force=true
	spring.http.encoding.enabled=true
	
5)tomcat服务器
	
	#服务器
	server.port=8080						//端口
	server.tomcat.uri-encoding=UTF-8		//字符集

全部:
	#dataSource
	spring.datasource.driver-class-name=com.mysql.jdbc.Driver
	spring.datasource.url=jdbc:mysql://localhost:3306/dubbo-smbms
	spring.datasource.username=root
	spring.datasource.password=123456
	#spring mvc
	spring.mvc.view.prefix=/WEB-INF/view/
	spring.mvc.view.suffix=.jsp
	#spring字符集
	spring.http.encoding.charset=UTF-8
	spring.http.encoding.force=true
	spring.http.encoding.enabled=true
	#服务器
	server.port=8080
	server.tomcat.uri-encoding=UTF-8

5,编写页面(如果不是WEB项目,改成WEB项目)
1)在pom.xml添加:

<packaging>war</packaging>

在这里插入图片描述

2)建立webapp文件包,新建jsp页面:

在这里插入图片描述
在这里插入图片描述

6,测试

1)添加jar包

<!--ideal内置tomcat不支持jsp,需要增加 jsp jar包-->
<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
	<version>9.0.12</version>
	<!--测试用-->
	<scope>provided</scope>
</dependency>

2)启动:

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zengshangchun/article/details/85264295