springboot关于mybatis的mapper.xml文件问题

本文详细介绍了如何在SpringBoot项目中整合MyBatis,包括使用Java注解和XML文件两种方式定义Mapper,以及如何配置扫描路径和数据源。通过实例展示了创建User表、实体类及Mapper接口的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot关于mybatis的mapper.xml文件问题

例如有建一张user表

create table user(
id bigint auto_increment prmary key,
username varchar(50) not null,
password varchar(50) not null
)

实体类

Class User implements Serializable{
	private long id;
	private String username;
	private String password;
	//省略get set方法
}
  1. java注解形式
    springboot整合mybatis网上教程很多省略
    UserMapper.java根据id查询用户信息
@Select(select username,password from user where id=(#{id}))
User selectById(long id);

mapper文件写完后只需在springboot里添加一个扫描注解。

@MapperScan("cy.cms.application.dx.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
  1. xml文件形式
    使用逆向工程生成所需要的mapper.java mapper.xml以及pojo导入springboot项目中,在springboot的配置文件添加
mybatis.mapper-locations= classpath:mapper/*.xml!

项目内容
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值