创建Spring Boot+Spring MVC+Mybatis Demo项目

参考资料

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依赖

<!--提供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>
<!--识别mybatis的Mapper.xml-->
<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。

posted on   王景迁  阅读(61)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示