news 2026/6/15 18:49:25

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/6/15 12:20:40

中国行政区划数据库MySQL终极部署:从零到实战的完整指南

中国行政区划数据库MySQL终极部署&#xff1a;从零到实战的完整指南 【免费下载链接】Administrative-divisions-of-China 中华人民共和国行政区划&#xff1a;省级&#xff08;省份&#xff09;、 地级&#xff08;城市&#xff09;、 县级&#xff08;区县&#xff09;、 乡级…

作者头像 李华
网站建设 2026/6/15 11:24:21

2026网络安全学习路线,非常详细!推荐学习

关键词&#xff1a;网络安全入门、渗透测试学习、零基础学安全、网络安全学习路线 目录 学习的问题 1、打基础时间太长2、知识点掌握程度不清楚3、知识点分不清重点4、知识点学习不系统5、自己解决问题难6、实战水平不够7、内网学习困难较大 学习正确方向 方法 1 &#xff08;…

作者头像 李华
网站建设 2026/6/15 10:41:04

Dell笔记本风扇控制完全指南:从新手到高手的散热管理方案

Dell笔记本风扇控制完全指南&#xff1a;从新手到高手的散热管理方案 【免费下载链接】DellFanManagement A suite of tools for managing the fans in many Dell laptops. 项目地址: https://gitcode.com/gh_mirrors/de/DellFanManagement 戴尔笔记本用户经常会遇到散热…

作者头像 李华
网站建设 2026/6/15 10:43:00

【Dify Amplitude API Key 配置全指南】:手把手教你安全集成与高效调用

第一章&#xff1a;Dify Amplitude API Key 概述Dify 是一个支持低代码开发智能应用的平台&#xff0c;允许开发者集成多种第三方分析工具以追踪用户行为。Amplitude 作为一款强大的产品分析平台&#xff0c;常用于监控用户在应用中的交互路径。通过配置 Dify Amplitude API Ke…

作者头像 李华
网站建设 2026/6/15 10:38:24

Whisky完全指南:让Windows程序在macOS上流畅运行的终极解决方案

Whisky完全指南&#xff1a;让Windows程序在macOS上流畅运行的终极解决方案 【免费下载链接】Whisky A modern Wine wrapper for macOS built with SwiftUI 项目地址: https://gitcode.com/gh_mirrors/wh/Whisky 还在为macOS无法运行Windows专属软件而烦恼吗&#xff1f…

作者头像 李华