springboot -整合ssm

 

1. 准备工作

SpringBoot开门三件事:依赖、配置、引导类

 

1.1 创建工程导入依赖

<?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.itheima</groupId>
    <artifactId>day06_springboot01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Mybatis的起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--提供连接池和事务管理-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
		<!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

1.2 创建配置文件

src\main\resources里创建配置文件 application.yaml,内容暂时为空

1.3 创建引导类

src\main\java里创建引导类:com.itheima.SsmApplication

package com.itheima;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@MapperScan("com.itheima.dao")
public class UserApplication {
    public static void main(String[] args) {
      SpringApplication.run(UserApplication.class,args);

    }
}

1.4 创建JavaBean

package com.itheima.domain;

import java.util.Date;

public class User {
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Integer age;
    private Integer sex;
    private Date birthday;
    private String note;
    private Date created;
    private Date updated;

    public User() {
    }

    public User(Long id, String userName, String password, String name, Integer age, Integer sex, Date birthday, String note, Date created, Date updated) {
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.birthday = birthday;
        this.note = note;
        this.created = created;
        this.updated = updated;
    }

    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Integer getSex() {
        return sex;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", note='" + note + '\'' +
                ", created=" + created +
                ", updated=" + updated +
                '}';
    }
}

 

2. 整合SpringMVC

创建UserController

package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @RequestMapping("/{id}")
    public User findById(@PathVariable("id") Integer id){
        System.out.println(id);
        return null;
    }
}

静态资源

说明

在SpringBoot项目里,静态资源可以放在以下位置:

  • classpath:/static/,实际开发中,通常放到这里

  • classpath:/public/

  • classpath:/resources/

  • classpath:/META-INF/resources/

但是要注意:

  • SpringBoot适合前后端分离的开发方式;

  • 前端页面部分由前端人员开发,部署到nginx里

  • 使用SpringBoot开发服务端部分,所以项目里通常不需要有静态资源,也不要有JSP

添加拦截器

说明

拦截器是SpringMVC的九大组件之一,每个拦截器类都要实现HandlerInterceptor接口,然后配置拦截范围。以前我们有springmvc.xml可以配置,那么在SpringBoot里该如何配置呢?

  1. 创建一个拦截器类,实现HandlerInterceptor接口

  2. 创建一个配置类

    • 配置类要实现WebMvcConfigurer接口

    • 配置类里要重写addInterceptors方法:这个方法是用于注册拦截器的

    • package com.itheima.interceptors;
      
      import org.springframework.context.annotation.Configuration;
      import org.springframework.stereotype.Component;
      import org.springframework.web.servlet.HandlerInterceptor;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      @Component
      public class MyInterceptors implements HandlerInterceptor {
          @Override
          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
              System.out.println("MyInterceptor.preHandle====");
              return true;
          }
      }
      

      配置类

      package com.itheima.config;
      
      import com.itheima.interceptors.MyInterceptors;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
      import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
      @Configuration
      public class WebConfig implements WebMvcConfigurer {
          @Autowired
          private MyInterceptors myInterceptors;
      
          @Override
          public void addInterceptors(InterceptorRegistry registry) {
              registry.addInterceptor(myInterceptors).addPathPatterns("/**");
      
          }
      }
      

      3. 整合Spring

      3.1 创建UserService接口

package com.itheima.service;

import com.itheima.domain.User;

import java.util.List;

public interface UserSercice {
    User finById(Integer id);

    List<User> QueryAll();

}

3.2 创建UserServiceImpl实现类

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserSercice {
    @Autowired
    private UserDao userDao;
    @Override
    public User finById(Integer id) {
      User user = userDao.finById(id);

        return user;
    }

    @Override
    public List<User> QueryAll() {
        return userDao.QueryAll();
    }
}

4. 整合Mybatis

4.1 创建映射器接口

package com.itheima.dao;

import com.itheima.domain.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserDao {
    @Select("select * from tb_user where id=#{id}")
    User finById(Integer id);
   @Select("select * from tb_user")
    List<User> QueryAll();
}

 

 

2.2 创建映射文件

src\main\resources里创建文件夹mapper,在mappers里创建映射文件UserDao.xml

注意:SpringBoot整合Mybatis后,不要求映射器和映射配置文件同名同位置。但是我们仍然建议同名

<?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.itheima.dao.UserDao">
    <select id="findById" resultType="User">
        select * from tb_user where id = #{id}
    </select>
</mapper>

2.3 配置Mybatis   yaml文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///springboot_db?useSSL=false
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
server:
  port: 80
  servlet:
    context-path: /
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.itheima.domain
  configuration:
    map-underscore-to-camel-case: true # 下划线字段名  和  驼峰式属性名的自动转换

2.4 开启映射器扫描

要在引导类上添加@MapperScan注解,用于扫描映射器接口

package com.itheima;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@MapperScan("com.itheima.dao")
public class UserApplication {
    public static void main(String[] args) {
      SpringApplication.run(UserApplication.class,args);

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值