springboot、mybatis项目,使用@MapperScan,dao自动实现。

2 篇文章 0 订阅
1 篇文章 0 订阅

 

由于Dao通常没有什么逻辑,所以简化Dao应运而生

项目环境:springboot 2+

在项目启动类上使用@MapperScan("包路径")注解,可以指定要扫描的Dao接口包,使程序运行时自动实现接口。

然后通过接口路径与mapper.xml的namespace匹配,找到对应的mapper.xml,然后找到与Dao接口中方法名相同的ID所对应的数据库操作,完成数据库操作。

启动类:设置扫描包,多个包可以用@MapperScan({"包1","包2"})

@SpringBootApplication
@MapperScan("com.ls.contract.dao")
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}
}

Dao接口:

package com.ls.contract.dao;

public interface UserDao {
	User findById(Long id);
}

mapper.xml:namespace要与接口路径一致,Id要与方法名一致

<mapper namespace="com.ls.contract.dao.UserDao">
    <select id="findById" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from c_user where id = #{id,jdbcType=BIGINT}
	</select>
</mapper>

如果需要单独指定某个Dao接口,可以在接口上使用@Mapper注解

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值