快速搭建基于springboot的ssm框架

 1.首先创建一个新的工程,选择Spring Initializr

2.next,输入项目名

 

3.next,选择要用到的依赖

 

4.next,然后finish 

5.等依赖下载好之后,application.properties改为application.yml,并配置文件

server:
  port: 8080


spring:
  http:
    encoding:
      charset: utf-8
      enabled: true
      force: true

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/personnal?useUnicode=true&charaterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*.xml

 

6.创建三个包

 7.在Pojo中创建实体类Student

package com.example.test.Pojo;
public class Student {
    private int id;
    private String sname;
    private int sage;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public int getSage() {
        return sage;
    }
    public void setSage(int sage) {
        this.sage = sage;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", sname='" + sname + '\'' +
                ", sage=" + sage +
                '}';
    }
}

8.在mapper中创建接口StudentMapper

package com.example.test.Mapper;

import com.example.test.Pojo.Student;

public interface StudentMapper {

        Student selectAllStudent();
}

 9.在service中创建接口StudentService

package com.example.test.Service;

import com.example.test.Pojo.Student;

public interface StudentService {

    Student selectAllStudent();
}

10.在StudentService中建包ServiceImp,并在ServiceImp创建实现类实现接口StudentService

import com.example.test.Mapper.StudentMapper;
import com.example.test.Pojo.Student;
import com.example.test.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImp implements StudentService {
    @Autowired
   private StudentMapper studentMapper;

    @Override
    public Student selectAllStudent() {
        return studentMapper.selectAllStudent();
    }
}

11.在controller中创建StudentController类

import com.example.test.Service.StudentService;
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 StudentService studentService;
    
    @RequestMapping("selectAllStudent")
    public Student selectAllStudent(){
        return studentService.selectAllStudent();
    }
}

12.在mapper中加上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.example.test.Mapper.StudentMapper">

    <!-- 查-->
      <select id="selectAllStudent"  resultType="com.example.test.Pojo.Student">
        SELECT *
        FROM student
    </select>
</mapper>

13.在TestApplication中加上注解MapperScan,表明存放mapper的包

package com.example.test;

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

@SpringBootApplication
@MapperScan("com.example.test.mapper")
public class TestApplication {

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

}

 14.pom.xml中依赖和build:

特别是resources中的配置,才能找到mapper中的xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

15.最后运行

如果数据中有时间类型,在数据源application.yml配置中 的url后加上serverTimezone=UTC

 datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/park?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值