https://github.com/qidasheng2012/springboot2.x_ssm
【init.sql】
执行初始化脚本
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(20) COMMENT '姓名',
`age` int(3) COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
INSERT INTO `USER`VALUES (1, '张三', 23);
INSERT INTO `USER`VALUES (2, '李四', 24);
【pom.xml】
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.0.RELEASEversion>
<relativePath/>
parent>
<groupId>com.springbootgroupId>
<artifactId>springboot2.x_ssmartifactId>
<version>1.0.0version>
<description>Spring Boot2.x 搭建 SSM 项目description>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
【application.yml】
#服务器配置
server:
port: 8080
spring:
#数据源
datasource:
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai&characterEncoding=utf8
username: root
password: 123456
#视图层控制
mvc:
static-path-pattern: /static/**
view:
prefix: classpath:/templates/
suffix: .html
#Mybatis扫描
mybatis:
mapper-locations: classpath*:mapper/*.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
type-aliases-package: com.springboot.ssm.domain
# 日志
logging:
level:
com.springboot.ssm.mapper: debug # 显示执行sql
【User】
package com.springboot.ssm.domain;
import lombok.Data;
@Data
public class User {
// 主键id
private Integer id;
// 姓名
private String name;
// 年龄
private Integer age;
}
【UserMapper】
package com.springboot.ssm.mapper;
import com.springboot.ssm.domain.User;
import java.util.List;
public interface UserMapper {
// 查询所有学生信息
List<User> getAll();
}
<mapper namespace="com.springboot.ssm.mapper.UserMapper">
<select id="getAll" resultType="User">
SELECT
id,name,age
FROM user
select>
mapper>
【UserService】
package com.springboot.ssm.service;
import com.springboot.ssm.domain.User;
import java.util.List;
public interface UserService {
// 查询所有学生信息
List<User> getAll();
}
【UserServiceImpl】
package com.springboot.ssm.service.impl;
import com.springboot.ssm.domain.User;
import com.springboot.ssm.mapper.UserMapper;
import com.springboot.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAll() {
return userMapper.getAll();
}
}
【UserController】
package com.springboot.ssm.controller;
import com.springboot.ssm.domain.User;
import com.springboot.ssm.service.UserService;
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.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/toUserListPage")
public String toUserListPage() {
return "user/userList";
}
@RequestMapping("/getAll")
@ResponseBody
public List<User> getAll() {
return userService.getAll();
}
}
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>用户列表页title>
head>
<body>
<h1>用户列表h1>
body>
html>
1、访问 http://localhost:8080/user/getAll
2、访问 http://localhost:8080/user/toUserListPage