好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Mybatis如何使用@Mapper和@MapperScan注解实现映射关系

使用@Mapper和@MapperScan注解实现映射关系

MyBatis与Spring整合后需要实现实体和数据表的映射关系。

实现实体和数据表的映射关系可以在Mapper类上添加@Mapper注解,如下代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

/**

  * 用户信息Mapper动态代理接口

  * @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注解。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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 声明:

?

1

2

3

4

5

6

7

8

9

< 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 来创建映射器 Mapper 接口。正常情况下,只有应用程序中有一个以上的数据源才会使用。 marker - interface :扫描器将注册在 base-package 包中的并且继承了特定的接口类的映射器 Mapper 接口 template - ref :当 Spring 上下文中有多个 SqlSessionTemplate 实例时,需要指定某一特定的SqlSessionTemplate 来创建映射器 Mapper 接口。 正常情况下,只有应用程序中有一个以上的数据源才会使用。 name-generator :BeannameGenerator 类的完全限定类名,用来命名检测到的组件。

MapperScan

Spring 3.x+版本支持使用@Configuration 和@Bean 注解来提供基于 Java 的配置。如果使用基于java的配置,可以使用@MapperScan 注解来扫描映射器 Mapper 接口。 @MapperScan 和<mybatis:scan/>工作方式

相同,并且也提供了对应的自定义选项。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@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时,用来指定特 SqlSessionFactory sqlSessionTemplateRef :当Spring上下文中有一个以上的 sqlSessionTemplate时,用来指定特定sqlSessionTemplate nameGenerator :BeanNameGenerator 类用来命名在 Spring 容器内检测到的组件。 basePackageClasses :basePackages() 的类型安全的替代品。包内的每一个类都会被扫描。 basePackages :扫描器扫描的基包,扫描器会扫描内部的 Mapper 接口。注意包内的至少有一个方法声明的才会被注册。具体类将会被忽略。

当然还可以在 applicationContext.xml 配置如下

?

1

2

3

< bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >

< property name = "basePackage" value = "com.mybatis3.mappers" />

</ bean >

使用 MapperScannerConfigurer 来扫描包 package ("com.mybatis3.mappers")下的所有 映射器 Mapper 接口,并自动地注册

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/pan_junbiao/article/details/106729912

查看更多关于Mybatis如何使用@Mapper和@MapperScan注解实现映射关系的详细内容...

  阅读:15次