SpringBoot整合Mybatis
1.创建项目springboot项目。
2.导入依赖。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency>
3.编写application.properties
配置文件
server.port=8085 #配置数据库连接 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://8.142.127.37/db_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&autoReconnect=true spring.datasource.username=root spring.datasource.password=123456 #整合mybatis #给实体类起别名 mybatis.type-aliases-package=com.test.springbootmybatisdemo.entity #接口的配置文件的位置 我这里接口配置文件是UserMapper.xml 如下 mybatis.mapper-locations=classpath*:com/test/springbootmybatisdemo/mapper/*.xml
4.编写entity包下的实体类User
package com.test.springbootmybatisdemo.entity; import lombok.Data; /** * @Author: laz * @CreateTime: 2022-07-07 10:24 * @Version: 1.0 * 用户信息 */ @Data public class User { private Integer id; private String name; private String sex; private Integer age; }
5.编写接口UserMapper
package com.test.springbootmybatisdemo.mapper; import com.test.springbootmybatisdemo.entity.User; import org.springframework.web.bind.annotation.RequestParam; /** * @Author: laz * @CreateTime: 2022-07-07 10:42 * @Version: 1.0 */ public interface UserMapper{ /** * 通过id获取所有用户信息 * @return */ User getById(@RequestParam("id") int id); }
6.编写接口对应的配置文件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.test.springbootmybatisdemo.mapper.UserMapper"> <select id="getById" resultType="com.test.springbootmybatisdemo.entity.User"> select * from user where id = #{id} </select> </mapper>
7.编写接口的Service
package com.test.springbootmybatisdemo.service; import com.test.springbootmybatisdemo.entity.User; /** * @Author: laz * @CreateTime: 2022-07-07 14:51 * @Version: 1.0 */ public interface IUserService { /** * 通过id获取所有用户信息 * @return */ User getById(int id); }
8.编写Service的实现类
package com.test.springbootmybatisdemo.service.impl; import com.test.springbootmybatisdemo.entity.User; import com.test.springbootmybatisdemo.mapper.UserMapper; import com.test.springbootmybatisdemo.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @Author: laz * @CreateTime: 2022-07-07 10:46 * @Version: 1.0 */ @Service public class UserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; @Override public User getById(int id) { return userMapper.getById(id); } }
9.编写controller
package com.test.springbootmybatisdemo.controller; import com.test.springbootmybatisdemo.service.IUserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author: laz * @CreateTime: 2022-07-07 14:52 * @Version: 1.0 */ @RestController @RequestMapping("/test") @Slf4j public class UserController { @Autowired private IUserService userService; @GetMapping("getById/{id}") public Object getById(@PathVariable("id")int id){ return userService.getById(id); } }
到此,整个业务代码编写完毕,以下是整个项目的目录
10.执行Springboot启动器
11.执行我们的getById方法
执行结果:
在调用接口的时候可能会遇到以下问题:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
具体内容如下:
解决方法:
在 pom 配置文件中键入 <build> 节点,并指明资源类型,这样在程序启动时,就可以正确加载配置文件了
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
至此,springboot整合mybatis的操作就全部结束了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!