news 2026/4/18 11:46:29

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/4/18 5:32:56

Emby个性化界面改造:三步打造专属媒体中心

Emby个性化界面改造&#xff1a;三步打造专属媒体中心 【免费下载链接】emby-crx Emby 增强/美化 插件 (适用于 Chrome 内核浏览器 / EmbyServer) 项目地址: https://gitcode.com/gh_mirrors/em/emby-crx 你是否厌倦了Emby媒体服务器千篇一律的默认界面&#xff1f;想要…

作者头像 李华
网站建设 2026/4/17 21:11:35

Matminer材料数据挖掘:从入门到精通的7天实战指南

Matminer材料数据挖掘&#xff1a;从入门到精通的7天实战指南 【免费下载链接】matminer Data mining for materials science 项目地址: https://gitcode.com/gh_mirrors/ma/matminer 在材料科学研究中&#xff0c;数据挖掘已成为加速新材料发现的关键技术。Matminer作为…

作者头像 李华
网站建设 2026/4/18 10:08:23

突破传统限制:Vue可视化打印的5大创新应用场景

突破传统限制&#xff1a;Vue可视化打印的5大创新应用场景 【免费下载链接】vue-plugin-hiprint hiprint for Vue2/Vue3 ⚡打印、打印设计、可视化设计器、报表设计、元素编辑、可视化打印编辑 项目地址: https://gitcode.com/gh_mirrors/vu/vue-plugin-hiprint 还在为V…

作者头像 李华
网站建设 2026/4/18 5:56:54

Mathtype公式转语音?用IndexTTS 2.0拓展无障碍阅读新场景

Mathtype公式转语音&#xff1f;用IndexTTS 2.0拓展无障碍阅读新场景 在视障学生尝试理解一份包含大量积分与矩阵的数学讲义时&#xff0c;屏幕阅读器却将“∑”读成“大写西格玛”&#xff0c;把“f(x)”念作“英文字母 f 括号 x”——这种语义断裂不仅影响理解效率&#xff0…

作者头像 李华
网站建设 2026/4/18 8:30:03

如何快速掌握Dalamud框架:面向FF14开发者的终极指南

如何快速掌握Dalamud框架&#xff1a;面向FF14开发者的终极指南 【免费下载链接】Dalamud FFXIV plugin framework and API 项目地址: https://gitcode.com/GitHub_Trending/da/Dalamud 在最终幻想14的冒险旅程中&#xff0c;你是否曾想过让游戏界面更智能、操作更便捷&…

作者头像 李华