参考资料
IDEA搭建Springboot+SpringMVC+Mybatis+Mysql(详细、易懂)
创建项目




创建多个目录

把application.properties改成yml格式并补充配置
mysql中创建数据库和表
| create database db1; |
| use db1; |
| create table `user` ( |
| `id` INT UNSIGNED AUTO_INCREMENT, |
| `name` VARCHAR(100) NOT NULL, |
| PRIMARY KEY ( `id` ) |
| )ENGINE=InnoDB DEFAULT CHARSET=utf8; |
application.yml内容
| spring: |
| datasource: |
| url: jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai |
| username: root |
| password: root |
| driver-class-name: com.mysql.cj.jdbc.Driver |
| mybatis: |
| mapper-locations: classpath:/resources/mappers/*.xml |
| type-aliases-package: com.example.demo.dao |
| server: |
| port: 8080 |
| servlet: |
| context-path: /demo |
补充多个文件内容

pom.xml里面添加依赖
在dependencies中添加web依赖
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| <version>2.6.4</version> |
| </dependency> |
在build中添加mybatis的xml文件位置
| <resources> |
| <resource> |
| <directory>src/main/</directory> |
| |
| <includes> |
| <include>**/*.xml</include> |
| </includes> |
| </resource> |
| |
| <resource> |
| <directory>src/main/resources</directory> |
| </resource> |
| </resources> |
src/main/resources/mappers/UserMapper.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.demo.dao.UserMapper"> |
| <insert id="insertUserInfo" parameterType="com.example.demo.entity.User"> |
| INSERT INTO user |
| (name) |
| VALUES |
| (#{name,jdbcType=VARCHAR}) |
| </insert> |
| </mapper> |
src/main/java/com/example/demo/service/UserService.java
| package com.example.demo.service; |
| |
| import com.example.demo.dao.UserMapper; |
| import com.example.demo.entity.User; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Service; |
| |
| @Service |
| public class UserService { |
| @Autowired |
| private UserMapper userMapper; |
| |
| public int addUserInfo(String name) { |
| User user = new User(name); |
| int res = userMapper.insertUserInfo(user); |
| if (res > 0) { |
| return 1; |
| } |
| return 0; |
| } |
| } |
src/main/java/com/example/demo/entity/User.java
| package com.example.demo.entity; |
| |
| public class User { |
| private String name; |
| |
| public User(String name) { |
| this.name = name; |
| } |
| } |
src/main/java/com/example/demo/dao/UserMapper.java
| package com.example.demo.dao; |
| |
| import com.example.demo.entity.User; |
| |
| public interface UserMapper { |
| int insertUserInfo(User user); |
| } |
src/main/java/com/example/demo/controller/UserController.java
| package com.example.demo.controller; |
| |
| import com.example.demo.service.UserService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.web.bind.annotation.*; |
| |
| @Controller |
| public class UserController { |
| @Autowired |
| private UserService userService; |
| |
| @PostMapping("/user") |
| @ResponseBody |
| public int addUserInfo(@RequestParam("name") String name){ |
| int res = userService.addUserInfo(name); |
| return res; |
| } |
| |
| } |
Postman测试
POST http://localhost:8080/demo/user?name=wjq
插入成功返回1。

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具