spring mybatis获取mapper的方式有哪些(mapper,mybatis,spring,开发技术)

时间:2024-05-09 04:44:02 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    spring-mybatis获取mapper方式汇总

    项目背景:

    pojo下面有一个user实体类

    Dao包下面写了usermapper.xml 和usermapper.interface,其中只有一个方法查询数据库中所有的用户。

    1.用实现类获取这个用户

    <beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/><propertyname="username"value="root"/><propertyname="password"value="root"/></bean><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="configLocation"value="classpath:mybatis-config.xml"/></bean><beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate"><constructor-argindex="0"ref="sqlSessionFactory"/></bean> <!--注册实现类usermapperimpl--><beanid="userMapper"class="com.kuang.mapper.UserMapperImpl"><propertyname="sqlSession"ref="sqlSession"/></bean>

    实现类usermapperImpl:

    publicclassUserMapperImplimplementsUserMapper{privateSqlSessionTemplatesqlSession;publicList<User>selectAllUser(){returnsqlSession.getMapper(UserMapper.class).selectAllUser();}publicvoidsetSqlSession(SqlSessionTemplatesqlSession){this.sqlSession=sqlSession;}}

    test测试:

    @Testpublicvoidtest2(){ApplicationContextapp=newClassPathXmlApplicationContext("spring-dao.xml");UserMapperImpluserMapper=app.getBean(UserMapperImpl.class);List<User>users=userMapper.selectAllUser();for(Useruser:users){System.out.println(user);}}

    2.SqlSessionDaoSupport获取

    publicclassUserMapperImpl1extendsSqlSessionDaoSupportimplementsUserMapper{publicList<User>selectAllUser(){returngetSqlSession().getMapper(UserMapper.class).selectAllUser();}}

    bean的注册:

    <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="configLocation"value="classpath:mybatis-config.xml"/></bean><beanid="userimpl1"class="com.kuang.mapper.UserMapperImpl1"><propertyname="sqlSessionFactory"ref="sqlSessionFactory"/></bean>

    3.MapperFactoryBean

    <beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/><propertyname="username"value="root"/><propertyname="password"value="root"/></bean><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="configLocation"value="classpath:mybatis-config.xml"/></bean><beanid="userimpl"class="org.mybatis.spring.mapper.MapperFactoryBean"><propertyname="mapperInterface"value="com.kuang.mapper.UserMapper"/><propertyname="sqlSessionFactory"ref="sqlSessionFactory"/></bean>

    测试:

    @Testpublicvoidtest3(){ApplicationContextapp=newClassPathXmlApplicationContext("spring-dao.xml");UserMapperuserMapper=app.getBean(UserMapper.class);//UserMapperuserMapper=app.getBean("userimpl");List<User>users=userMapper.selectAllUser();for(Useruser:users){System.out.println(user);}}

    在使用这个MapperFactoryBean方式的时候,通过app有两种方式获取bean,一种是id然后强转,另一种是接口的类型class。

    4.MapperScannerConfigurer

    xml配置

    <beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/><propertyname="username"value="root"/><propertyname="password"value="root"/></bean><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="configLocation"value="classpath:mybatis-config.xml"/></bean><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.kuang.mapper"/><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/></bean>

    test:

    @Testpublicvoidtest4(){ApplicationContextapp=newClassPathXmlApplicationContext("spring-dao.xml");UserMapperbean=app.getBean(UserMapper.class);for(Useruser:bean.selectAllUser()){System.out.println(user);}}

    mybatis的mapper注解

    从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件(那个xml写的是真的无语。。。)。很恶心的一个事实是源码中并没有对于这个注解的详细解释

    在 Spring 程序中,Mybatis 需要找到对应的 mapper,在编译的时候动态生成代理类,实现数据库查询功能,所以我们需要在接口上添加 @Mapper 注解。

    @MapperpublicinterfaceUserDao{...}

    但是,仅仅使用@Mapper注解,我们会发现,在其他变量中依赖注入,IDEA 会提示错误,但是不影响运行(亲测~)。

    因为我们没有显式标注这是一个 Bean,IDEA 认为运行的时候会找不到实例注入,所以提示我们错误。

    如下图,会有红色波浪线。

    spring mybatis获取mapper的方式有哪些

    尽管这个错误提示并不影响运行,但是看起来很不舒服,所以我们可以在对应的接口上添加 bean 的声明,如下:

    @Repository//也可以使用@Component,效果都是一样的,只是为了声明为bean@MapperpublicinterfaceUserDao{ @Insert("insertintouser(account,password,user_name)"+"values(#{user.account},#{user.password},#{user.name})")intinsertUser(@Param("user")Useruser)throwsRuntimeException;}

    基于注解的开发也有其他手段帮助 Mybatis 找到 mapper,那就是 @MapperScan 注解,可以在启动类上添加该注解,自动扫描包路径下的所有接口。

    @SpringBootApplication@MapperScan("com.scut.thunderlearn.dao")publicclassUserEurekaClientApplication{publicstaticvoidmain(String[]args){SpringApplication.run(UserEurekaClientApplication.class,args);}}
     </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
    本文:spring mybatis获取mapper的方式有哪些的详细内容,希望对您有所帮助,信息来源于网络。
    上一篇:Confd和Consul使用场景是什么下一篇:

    2 人围观 / 0 条评论 ↓快速评论↓

    (必须)

    (必须,保密)

    阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18