由于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注解