new不能执行autowired_接口不能被实例化,Mybatis的Mapper/Dao为什么却可以@Autowired注入?...

在Java中,接口不能直接实例化,但Mybatis的Mapper接口却可以通过@Autowired注解注入。本文深入探讨了Mybatis如何实现这一功能。从SpringBoot的@MapperScan注解入手,分析了MapperScannerRegistrar类的角色,以及FactoryBean接口如何在实例化过程中生成Mapper接口的代理对象。在扫描mapper接口后,Mybatis将它们包装为MapperProxyFactory并利用Proxy.newProxyInstance创建代理对象,最终在@Autowired时调用FactoryBean的getObject方法来获取Mapper接口的实例。
摘要由CSDN通过智能技术生成

对于我们 java 来说,接口是不能被实例化的。而且接口的所有方法都是public的。

可是为什么 Mybaits 的mapper 接口,可以直接 @Autowired 注入 使用?

接下来看看Mybatis 是如何做的。

基于SpringBoot 的 @MapperScan 注解入手,分析。

带着问题分析代码:Mybatis 的mapper接口,是怎么被扫描的?

mapper接口是如何被实例化,然后可以使用@Autowired注入?

@MapperScan

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@Documented

@Import(MapperScannerRegistrar.class)

@Repeatable(MapperScans.class)

public @interface MapperScan {

// 在使用MapperScan中,扫描包的路径。 // 填写的是 mapper 接口所在包名,对该value值下的所有文件进行扫描 String[] value() default {};

String[] basePackages() default {};

Class>[] basePackageClasses() default {};

Class extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

Class extends Annotation> annotationClass() default Annotation.class;

Class> markerInterface() default Class.class;

String sqlSessionTemplateRef() default "";

String sqlSessionFactoryRef() default "";

Class extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;

}

@SpringBootApplication

@MapperScan("cn.thisforyou.core.blog.mapper")

public class App

{

public static void main( String[] args )

{

System.out.println( "Hello World!" );

SpringApplication.run(App.class,args);

}

}

在SpringBoot中使用mybatis,那它的入口就在 @MapperScan中。@MapperScan注解,是在SpringBoot的启动类中。

@MapperScan中有个 @Import 注解。

@Import 注解,可以加载某个类,放到Spring的IOC中管理

在Spring中,要将Bean放到IOC容器中管理的话,有几种方式。@Import 此种方法

@Configuration 与 @Bean 注解结合使用

@Controller @Service @Repository @Component

@ComponentScan 扫描。

重写BeanFactoryPostProcessor 的postProcessBeanFactory()方法,也可以实现Bean的注入

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,@Mapper注解是Mybatis框架中用于标识数据访问层接口的注解,用于告诉Spring容器将该接口实例化注入到其他Bean中。其使用步骤如下: 1. 在Spring Boot项目中引入MybatisMybatis-Spring的依赖 2. 在配置文件中配置数据源和Mybatis的相关属性 3. 创建一个数据访问层接口,使用@Mapper注解标识该接口 4. 在该数据访问层接口中定义需要操作的数据库方法 5. 在Service或Controller中注入该数据访问层接口的实例,并调用其中的方法 下面是一个示例: 1. 在pom.xml中添加MybatisMybatis-Spring的依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> ``` 2. 在application.properties中配置数据源和Mybatis的相关属性: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 mybatis.type-aliases-package=com.example.demo.entity mybatis.mapper-locations=classpath:mapper/*.xml ``` 3. 创建一个数据访问层接口UserMapper,使用@Mapper注解标识该接口: ```java @Mapper public interface UserMapper { User selectByPrimaryKey(Integer id); int insert(User record); int updateByPrimaryKey(User record); int deleteByPrimaryKey(Integer id); } ``` 4. 在mapper目录下创建UserMapper.xml,定义需要操作的数据库方法: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select * from user where id = #{id,jdbcType=INTEGER} </select> <insert id="insert" parameterType="com.example.demo.entity.User" useGeneratedKeys="true" keyProperty="id"> insert into user (username, password) values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}) </insert> <update id="updateByPrimaryKey" parameterType="com.example.demo.entity.User"> update user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where id = #{id,jdbcType=INTEGER} </delete> </mapper> ``` 5. 在Service或Controller中注入UserMapper的实例,并调用其中的方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } @Override public int insert(User user) { return userMapper.insert(user); } @Override public int updateByPrimaryKey(User user) { return userMapper.updateByPrimaryKey(user); } @Override public int deleteByPrimaryKey(Integer id) { return userMapper.deleteByPrimaryKey(id); } } ``` 这就是使用@Mapper注解的基本步骤,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值