news 2026/8/7 12:44:27

mybatis搭建项目框架

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
mybatis搭建项目框架

现在给大家搭建一个mybatis的简单框架。

package org.example.mybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("org.example.mybatis") @SpringBootApplication public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } }
package org.example.mybatis; public class User { private Long id; private String name; private Integer age; // getter & setter public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
package org.example.mybatis; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/user") public class UserController { private final UserMapper userMapper; public UserController(UserMapper userMapper) { this.userMapper = userMapper; } @GetMapping("/list") public List<User> list() { return userMapper.findAll(); } @GetMapping("/{id}") public User get(@PathVariable Long id) { return userMapper.findById(id); } @PostMapping("/add") public String add(@RequestBody User user) { userMapper.insert(user); return "success"; } }
package org.example.mybatis; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { List<User> findAll(); User findById(Long id); int insert(User user); }
<?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="org.example.mybatis.UserMapper"> <select id="findAll" resultType="org.example.mybatis.User"> select * from user </select> <select id="findById" resultType="org.example.mybatis.User"> select * from user where id = #{id} </select> <insert id="insert"> insert into user(name, age) values(#{name}, #{age}) </insert> </mapper>
<?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.2.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis</name> <description>mybatis</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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> <!-- MyBatis Spring Boot Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> </annotationProcessorPaths> </configuration> </plugin> <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>
server: port: 8084 spring: datasource: url: jdbc:mysql://localhost:3306/testmybatis?useSSL=false&serverTimezone=Asia/Shanghai username: root password: driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:*.xml type-aliases-package: org.example.mybatis

代码目录结构如下:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/4 13:49:48

PyCharm激活码永久免费?不!但你可以免费使用IndexTTS 2.0做配音

PyCharm激活码永久免费&#xff1f;不&#xff01;但你可以免费使用IndexTTS 2.0做配音 在短视频、虚拟主播和AI内容创作爆发的今天&#xff0c;一个现实问题困扰着无数创作者&#xff1a;如何低成本、高质量地完成专业级配音&#xff1f; 你或许曾为找不到合适的声优而焦虑&am…

作者头像 李华
网站建设 2026/8/3 0:28:47

AB下载管理工具完整配置与使用手册

在日常文件下载过程中&#xff0c;您是否遇到过下载速度缓慢、大文件传输中断后需要重新开始、多个下载任务难以统一管理等困扰&#xff1f;AB下载管理工具正是为解决这些问题而设计的专业文件下载助手。&#x1f680; 【免费下载链接】ab-download-manager A Download Manager…

作者头像 李华
网站建设 2026/8/3 12:47:20

视频配音不再难!IndexTTS 2.0精准对齐音画,解决不同步问题

视频配音不再难&#xff01;IndexTTS 2.0精准对齐音画&#xff0c;解决不同步问题 在短视频日活破十亿、虚拟主播席卷直播平台的今天&#xff0c;一个被广泛忽视却极其关键的问题正困扰着无数内容创作者&#xff1a;为什么我生成的AI语音总是和画面对不上&#xff1f; 你精心剪…

作者头像 李华
网站建设 2026/7/31 9:26:07

Arctium启动器完整使用指南:魔兽世界私服连接终极方案

Arctium启动器完整使用指南&#xff1a;魔兽世界私服连接终极方案 【免费下载链接】WoW-Launcher A game launcher for World of Warcraft that allows you to connect to custom servers. 项目地址: https://gitcode.com/gh_mirrors/wo/WoW-Launcher Arctium启动器是一…

作者头像 李华
网站建设 2026/7/30 4:25:08

PlotDigitizer:图像数据提取的智能解决方案

PlotDigitizer&#xff1a;图像数据提取的智能解决方案 【免费下载链接】PlotDigitizer A Python utility to digitize plots. 项目地址: https://gitcode.com/gh_mirrors/pl/PlotDigitizer 在数据科学和工程领域&#xff0c;我们经常面临一个共同挑战&#xff1a;如何从…

作者头像 李华
网站建设 2026/7/31 5:35:07

CentOS-WSL:在Windows上搭建企业级Linux开发环境

CentOS-WSL&#xff1a;在Windows上搭建企业级Linux开发环境 【免费下载链接】CentOS-WSL 项目地址: https://gitcode.com/gh_mirrors/ce/CentOS-WSL 想要在Windows系统上体验原汁原味的企业级CentOS Linux环境吗&#xff1f;CentOS-WSL项目为你提供了完美的解决方案&a…

作者头像 李华