学习SpringBoot初次整合SSM,后续需要不断优化

参考SpringBoot3教程[1]

导入依赖

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu</groupId>
<artifactId>boot3-05-ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot3-05-ssm</name>
<description>boot3-05-ssm</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<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>3.0.2</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build> </project>

修改配置文件

application.yml

spring:
# 配置数据源
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource # 配置MyBatis
mybatis:
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true #开启驼峰命名

Mapper及配置文件

UserMapper.java

package com.atguigu.boot3.ssm.mapper;

import com.atguigu.boot3.ssm.bean.TUser;
import org.apache.ibatis.annotations.Param; public interface UserMapper { TUser getUserById(@Param("id") Long id);
}

UserMapper.xml

可以使用MyBatisX插件生成该文件

<?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.atguigu.boot3.ssm.mapper.UserMapper">
<select id="getUserById" resultType="com.atguigu.boot3.ssm.bean.TUser">
select * from t_user where id=#{id}
</select>
</mapper>

编写Controller及启动类

UserController.java

package com.atguigu.boot3.ssm.controller;

import com.atguigu.boot3.ssm.bean.TUser;
import com.atguigu.boot3.ssm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController { @Autowired
UserMapper userMapper; @GetMapping("/user/{id}")
public TUser getUser(@PathVariable("id") Long id){
TUser user = userMapper.getUserById(id);
return user;
}
}

Boot305SsmApplication.java 启动类

这里添加了Mapper的包扫描@MapperScan

package com.atguigu.boot3.ssm;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan(basePackages = "com.atguigu.boot3.ssm.mapper")
@SpringBootApplication
public class Boot305SsmApplication { public static void main(String[] args) {
SpringApplication.run(Boot305SsmApplication.class, args);
} }

数据库

初始化的数据

CREATE TABLE `t_user`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '编号',
`login_name` VARCHAR(200) NULL DEFAULT NULL COMMENT '用户名称' COLLATE 'utf8_general_ci',
`nick_name` VARCHAR(200) NULL DEFAULT NULL COMMENT '用户昵称' COLLATE 'utf8_general_ci',
`passwd` VARCHAR(200) NULL DEFAULT NULL COMMENT '用户密码' COLLATE 'utf8_general_ci',
PRIMARY KEY (`id`)
);
insert into t_user(login_name, nick_name, passwd) VALUES ('zhangsan','张三','123456');

参考:


  1. https://www.yuque.com/leifengyang/springboot3/bi1crud5lggl2g8d#SIHzg

SpringBoot——SSM简单整合v0.1的更多相关文章

  1. SSM简单整合教程&测试事务

    自打来了博客园就一直在看帖,学到了很多知识,打算开始记录的学习到的知识点 今天我来写个整合SpringMVC4 spring4 mybatis3&测试spring事务的教程,如果有误之处,还请 ...

  2. RabbitMQ和SpringBoot的简单整合列子

    一 思路总结 1 主要用spring-boot-starter-amqp来整合RabbitMQ和SpringBoot 2 使用spring-boot-starter-test来进行单元测试 3编写配置 ...

  3. ssm简单整合

    pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  4. springboot vue简单整合

    1.vue项目 (1)修改config/index.js (2)执行 npm run build 生成静态文件,在dist目录 2.springboot项目 (1)在src/main/resource ...

  5. ssm简单整合(注释方法)

    1.创建web工程,选择web.xml文件,并导入相关jar包(使用的spring4.0以上版本,) 2.配置web.xml文件,包括spring.xml,springMVC.xml的加载映射,核心操 ...

  6. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

  7. Springboot与MyBatis简单整合

    之前搭传统的ssm框架,配置文件很多,看了几天文档才把那些xml的逻辑关系搞得七七八八,搭起来也是很麻烦,那时我完全按网上那个demo的版本要求(jdk和tomcat),所以最后是各种问题没成功跑起来 ...

  8. SpringBoot学习之整合Druid的简单应用

    一.Druid介绍 Druid简介 Druid是目前Java语言中最好的数据库连接池之一.结合了 C3P0.DBCP 等 DB 池的优点,同时加入了日志监控.Druid 是一个分布式的.支持实时多维 ...

  9. SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例

    简介: SSM框架是Spring,SpringMVC 和Mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,dao层四层. Spring实现 ...

  10. SpringBoot简单整合redis

    Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...

随机推荐

  1. MySQL主从复制-原理实战

    一.原理 主从复制架构图:主从复制原理: Mysql 中有一种日志叫做 bin 日志(二进制日志).这个日志会记录下所有修改了数据库的SQL 语句(insert,update,delete,creat ...

  2. 从SSH远程到Git Push:在Windows上一步到位实现免密码登录

    前言 我一直希望在Windows上能像在Linux系统中那样,通过SSH密钥实现免密码远程连接.每次远程连接到服务器时,手动输入密码既麻烦又不太安全,尤其是在我需要频繁操作的情况下. 之前的文章中已经 ...

  3. DeepSeek R1本地与线上满血版部署:超详细手把手指南

    一.DeepSeek R1本地部署 1.下载ollama下载地址 本人是Mac电脑,所以选第一项,下面都是以Mac环境介绍部署,下载好把ollama运行起来即可启动Ollama服务. Ollama默认 ...

  4. 交叉编译SQLite3

    交叉编译SQLite3 SQLite是一个进程内的库,实现了自给自足的.无服务器的.零配置的.事务性的SQL 数据库引擎. 它是一个零配置的数据库,这意味着与其他数据库不一样,您不需要在系统中配置. ...

  5. Vue 组件里添加键盘事件 keydown keyup不生效问题

    我在使用VueDraggableResizable制作一个窗口,然后需要点击esc关闭窗口. 但是键盘事件没有生效,写任何位置都不行. 解决方案 在需要触发esc事件的div或其他上给出 tabind ...

  6. 小米10至尊纪念版—官方稳定版一键 TWRE刷入+面具ROOT

    1.解锁BL http://www.miui.com/unlock/index.html 2.备份+关机(虽然不会清理数据,但是小心为上) 3.音量下+开机进入fastboot模式 4.解压压缩包,运 ...

  7. IDEA - 文件上方的文档注释如何自定义

    1.在设置中打开文件和代码模板,根据描述中的参考信息进行自定义配置 File > Settings > Editor > File and Code Templates 2.配置完成 ...

  8. ARC101E题解

    前言 此片题解大致按照笔者做题思路进行讲解. 简要题意 有一棵树,树上有偶数个节点.你需要给这些点两两配对,一组已经配对的点会将两点之间的树边进行一次覆盖.一组合法方案需要满足树上所有边都被覆盖至少一 ...

  9. 5. Nginx 负载均衡配置案例(附有详细截图说明++)

    5. Nginx 负载均衡配置案例(附有详细截图说明++) @ 目录 5. Nginx 负载均衡配置案例(附有详细截图说明++) 1. Nginx 负载均衡 配置实例 3. 注意事项和避免的坑 4. ...

  10. 读论文-基于序列模式的电子商务推荐系统综述(A Survey of Sequential Pattern Based E-Commerce Recommendation Systems)

    前言 今天读的论文为一篇于2023年10月3日发表在<算法>(Algorithms)的论文,这篇文章综述了基于序列模式的电子商务推荐系统,强调了通过整合用户购买和点击行为的序列模式来提高推 ...