mybatis在springboot中的使用

在springboot中使用mybatis的注解版:

1、在springboot中添加mybatis的starter,该启动器会导入mybatis的自动配置类进行自动配置。

2、把mapper接口配上@Mapper注解放到springboot能扫描到的包路径下(@SpringBootApplication注解的类所在包下),自动配置会把这些@Mapper根据mapper内的@Select、@Delete、@Update等注解内容生成代理类。。。。或者在spring@Configuration类上使用@MapperScan指定mapper接口的包(相当于<mybatis-spring scan-packages=""/>)

3、在spring容器中哪里需要使用这些mapper,直接@Autowired就行。

4、如果要配置mybatis配置,只要实现mybatis的自定义接口然后添加到容器中即可(名字忘了)

在springboot中使用mybatis的XML版:

1、在springboot中添加mybatis的starter,该启动器会导入mybatis的自动配置类进行自动配置。

2、把mapper接口配上@Mapper注解放到springboot能扫描到的包路径下(@SpringBootApplication注解的类所在包下),或者在spring@Configuration类上使用@MapperScan指定mapper接口的包(相当于<mybatis-spring scan-packages=""/>)

3.对mapper接口创建对应的xml映射文件,以及mybatis配置文件。。。这些文件的路径在application.properties 或yaml中配置好即可(mybatis.config-locations  以及mybatis.mapper-locations配置)

注意:注解版与xml版可以同时使用,不影响

猜你喜欢

转载自blog.csdn.net/qq_36951116/article/details/83053801
今日推荐