Mybatis如何使用@Mapper和@MapperScan注解實現(xiàn)映射關系
使用@Mapper和@MapperScan注解實現(xiàn)映射關系
MyBatis與Spring整合后需要實現(xiàn)實體和數據表的映射關系。
實現(xiàn)實體和數據表的映射關系可以在Mapper類上添加@Mapper注解,如下代碼:
/** * 用戶信息Mapper動態(tài)代理接口 * @author pan_junbiao **/ @Mapper @Repository public interface UserMapper { /** * 新增用戶,并獲取自增主鍵 */ @Insert("INSERT INTO tb_user(user_account,user_password,blog_url,blog_remark) VALUES(#{userAccount},#{userPassword},#{blogUrl},#{blogRemark})") @Options(useGeneratedKeys = true, keyColumn = "user_id", keyProperty = "userId") //或者:@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyColumn = "user_id", keyProperty = "userId",before = false, resultType = Integer.class) public int insertUser(UserInfo userInfo); /** * 修改用戶 */ @Update("UPDATE tb_user SET user_account = #{userAccount} ,user_password = #{userPassword} ,blog_url=#{blogUrl} ,blog_remark=#{blogRemark} WHERE user_id = #{userId}") public int updateUser(UserInfo userInfo); /** * 刪除用戶 */ @Delete("DELETE FROM tb_user WHERE user_id = #{userId}") public int deleteUser(int userId); /** * 根據用戶ID,獲取用戶信息 */ @Select("SELECT * FROM tb_user WHERE user_id = #{userId}") public UserInfo getUserById(int userId); }
但是建議以后直接在SpringBoot啟動類中加 @MapperScan("com.pjb.mapper") 注解,這樣會比較方便,不需要對每個Mapper都添加@Mapper注解。
package com.pjb; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * SpringBoot啟動類 * @author pan_junbiao **/ @MapperScan("com.pjb.mapper") @SpringBootApplication public class SpringbootMybatisDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisDemoApplication.class, args); } }
Mybatis-@MapperScan和mybatis:scan分析
MyBatis-Spring-1.2.0 新增了兩種新的掃描映射器 Mapper 接口的方法:
- 使用<mybatis:scan/>元素
- 使用@MapperScan 注解(需要 Spring3.1+版本)
<mybatis:scan>
<mybatis:scan>元素將在特定的以逗號分隔的包名列表中搜索映射器 Mapper 接口。 使用這個新的 MyBatis-Spring 名空間你需要添加以下的 schema 聲明:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <mybatis:scan base-package="com.mybatis.x.mappers" /> </beans>
<mybatis:scan> 元素提供了下列的屬性來自定義掃描過程:
annotation
:掃描器將注冊所有的在 base-package 包內并且匹配指定注解的映射器 Mapper 接口。factory - ref
:當 Spring 上下文中有多個SqlSessionFactory實例時,需要指定某一特定的SqlSessionFactory 來創(chuàng)建映射器 Mapper 接口。正常情況下,只有應用程序中有一個以上的數據源才會使用。marker - interface
:掃描器將注冊在 base-package 包中的并且繼承了特定的接口類的映射器 Mapper 接口template - ref
:當 Spring 上下文中有多個 SqlSessionTemplate 實例時,需要指定某一特定的SqlSessionTemplate 來創(chuàng)建映射器 Mapper 接口。 正常情況下,只有應用程序中有一個以上的數據源才會使用。name-generator
:BeannameGenerator 類的完全限定類名,用來命名檢測到的組件。
MapperScan
Spring 3.x+版本支持使用@Configuration 和@Bean 注解來提供基于 Java 的配置。如果使用基于java的配置,可以使用@MapperScan 注解來掃描映射器 Mapper 接口。 @MapperScan 和<mybatis:scan/>工作方式
相同,并且也提供了對應的自定義選項。
@Configuration @MapperScan("com.mybatis.x.mappers") public class AppConfig { @Bean public DataSource dataSource() { return new PooledDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "root"); } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBeansessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); return sessionFactory.getObject(); } }
@MapperScan 注解有以下屬性供自定義掃描過程使用:
annotationClass
:掃描器將注冊所有的在 base-package 包內并且匹配指定注解的映射器 Mapper 接口。markerInterface
:掃描器將注冊在 base-package 包中的并且繼承了特定的接口類的映射器 Mapper 接口sqlSessionFactoryRef
:當Spring上下文中有一個以上的 SqlSesssionFactory時,用來指定特 SqlSessionFactorysqlSessionTemplateRef
:當Spring上下文中有一個以上的 sqlSessionTemplate時,用來指定特定sqlSessionTemplatenameGenerator
:BeanNameGenerator 類用來命名在 Spring 容器內檢測到的組件。basePackageClasses
:basePackages() 的類型安全的替代品。包內的每一個類都會被掃描。basePackages
:掃描器掃描的基包,掃描器會掃描內部的 Mapper 接口。注意包內的至少有一個方法聲明的才會被注冊。具體類將會被忽略。
當然還可以在 applicationContext.xml 配置如下
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis3.mappers" /> </bean>
使用 MapperScannerConfigurer 來掃描包 package ("com.mybatis3.mappers")下的所有 映射器 Mapper 接口,并自動地注冊
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決springboot自定義注解AOP在controller上導致controller注入失敗問題
這篇文章主要介紹了解決springboot自定義注解AOP在controller上導致controller注入失敗問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10