Mybatis如何使用@Mapper和@MapperScan注解實現(xiàn)映射關(guān)系
使用@Mapper和@MapperScan注解實現(xiàn)映射關(guān)系
MyBatis與Spring整合后需要實現(xiàn)實體和數(shù)據(jù)表的映射關(guān)系。
實現(xiàn)實體和數(shù)據(jù)表的映射關(guā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);
/**
* 根據(jù)用戶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 包內(nèi)并且匹配指定注解的映射器 Mapper 接口。factory - ref:當(dāng) Spring 上下文中有多個SqlSessionFactory實例時,需要指定某一特定的SqlSessionFactory 來創(chuàng)建映射器 Mapper 接口。正常情況下,只有應(yīng)用程序中有一個以上的數(shù)據(jù)源才會使用。marker - interface:掃描器將注冊在 base-package 包中的并且繼承了特定的接口類的映射器 Mapper 接口template - ref:當(dāng) Spring 上下文中有多個 SqlSessionTemplate 實例時,需要指定某一特定的SqlSessionTemplate 來創(chuàng)建映射器 Mapper 接口。 正常情況下,只有應(yīng)用程序中有一個以上的數(shù)據(jù)源才會使用。name-generator:BeannameGenerator 類的完全限定類名,用來命名檢測到的組件。
MapperScan
Spring 3.x+版本支持使用@Configuration 和@Bean 注解來提供基于 Java 的配置。如果使用基于java的配置,可以使用@MapperScan 注解來掃描映射器 Mapper 接口。 @MapperScan 和<mybatis:scan/>工作方式
相同,并且也提供了對應(yīng)的自定義選項。
@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 包內(nèi)并且匹配指定注解的映射器 Mapper 接口。markerInterface:掃描器將注冊在 base-package 包中的并且繼承了特定的接口類的映射器 Mapper 接口sqlSessionFactoryRef:當(dāng)Spring上下文中有一個以上的 SqlSesssionFactory時,用來指定特 SqlSessionFactorysqlSessionTemplateRef:當(dāng)Spring上下文中有一個以上的 sqlSessionTemplate時,用來指定特定sqlSessionTemplatenameGenerator:BeanNameGenerator 類用來命名在 Spring 容器內(nèi)檢測到的組件。basePackageClasses:basePackages() 的類型安全的替代品。包內(nèi)的每一個類都會被掃描。basePackages:掃描器掃描的基包,掃描器會掃描內(nèi)部的 Mapper 接口。注意包內(nèi)的至少有一個方法聲明的才會被注冊。具體類將會被忽略。
當(dāng)然還可以在 applicationContext.xml 配置如下
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis3.mappers" /> </bean>
使用 MapperScannerConfigurer 來掃描包 package ("com.mybatis3.mappers")下的所有 映射器 Mapper 接口,并自動地注冊
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java自定義類數(shù)組報null的相關(guān)問題及解決
這篇文章主要介紹了Java自定義類數(shù)組報null的相關(guān)問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
JAVA基于PDF box將PDF轉(zhuǎn)為圖片的實現(xiàn)方法
這篇文章主要介紹了JAVA基于PDF box將PDF轉(zhuǎn)為圖片的操作方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07
mybatis和mybatis-plus設(shè)置值為null不起作用問題及解決
Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查詢時對空值的處理策略,通過配置不同的策略類型,可以靈活地處理實體對象的空值問題2025-02-02
詳解將Eclipse代碼導(dǎo)入到AndroidStudio的兩種方式
本篇文章主要介紹了詳解將Eclipse代碼導(dǎo)入到AndroidStudio的兩種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

