当前位置: 首页 > news >正文

津南做网站的公司哪个公司的网站制作

津南做网站的公司,哪个公司的网站制作,免费网站论坛,如何判断一个网站的关键词是否难做大家好,我是王有志。 今天给大家带来的是一道来自京东的 MyBatis 面试题:MyBatis 中有几种加载映射器(Mapper.xml)的方式? 常见加载 MyBatis 映射器的方式有 5 种,可以根据不同的使用方式来进行具体区分&…

大家好,我是王有志。

今天给大家带来的是一道来自京东的 MyBatis 面试题:MyBatis 中有几种加载映射器(Mapper.xml)的方式?

常见加载 MyBatis 映射器的方式有 5 种,可以根据不同的使用方式来进行具体区分:

  • MyBatis 原生应用,即不与 Spring 或 Spring Boot 集成,可以通过配置文件和 Java 编码的方式加载映射器;
  • MyBatis 与 Spring 集成,可以通过加载 Spring Bean 的方式完成 MyBatis 映射器的加载;
  • MyBatis 与 Spring Boot 集成,可以通过@MapperScan@Mapper注解完成 MyBatis 映射器的加载。

下面,我们来具体看下每种方式是如何加载 MyBatis 映射器的。

原生 MyBatis 应用

原生 MyBatis 应用中(即不与 Spring 或 Spring Boot 集成的 MyBatis 应用),可以通过两种方式加载映射器(Mapper.xml):

  • 通过 myabtis-config.xml 加载映射器;
  • 通过 Java 编码的方式加载映射器。

通过 mybatis-config.xml 加载映射器

与 MyBatis入门中实现的简单例子一样,只需要在 mybatis-config.xml 中配置映射器即可:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 省略环境配置 --><mappers><mapper resource="mapper/UserMapper.xml"/><mapper resource="mapper/CompanyMapper.xml"/></mappers>
</configuration>

通过 Java 编码的方式加载映射器

MyBatis入门中的附录中同样有相关的例子:

DataSource dataSource = new PooledDataSource("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3306/mybatis", "root", "123456");
TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);// 加载映射器
configuration.addMapper(UserMapper.class);

如果希望加载某个包下的全部 Mapper.xml,可以使用Configuration#addMappers进行加载:

configuration.addMappers("com.wyz.mapper");

MyBatis 与 Spring 集成

MyBatis 与 Spring 集成后,可以通过加载 Bean 的方式加载 MyBatis 的映射器(Mapper.xml),我们还是按照 MyBatis入门中的步骤,先完成 Java 对象 UserDO,Java 接口 UserMapper 的创建,接着是编写 MyBatis 的映射器 UserMapper.xml,最后我们还需要引入相关依赖:

<dependencies><!-- 省略 MySQL,junit等依赖 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>3.0.3</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>6.1.4</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>6.1.4</version></dependency>
</dependencies>

接下来我们通过注入 Spring Bean 的方式来加载 Mapper.xml,这里我们创建 spring-beans.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="123456"/></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="mapper/UserMapper.xml"/></bean><!-- 通过 Spring Bean 的方式加载 MyBatis 映射器--><bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="com.wyz.mapper.UserMapper"/><property name="sqlSessionFactory" ref="sqlSessionFactory"/></bean>
</beans>

最后我们来测试:

package com.wyz.mapper;import com.wyz.entity.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;@Slf4j
public class UserMapperTest {@Testpublic void test(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-beans.xml");UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);List<UserDO> users = userMapper.selectAll();for(UserDO user : users) {System.out.println(user.toString());}}
}

MyBatis 与 Spring Boot 集成

MyBatis 与 Spring Boot 集成后,可以通过注解的方式或配置文件的方式加载 MyBatis 的映射器(Mapper.xml),使用起来非常的方便。
首先,我们还是做好前期的准备,创建 Java 对象 UserDO,Java 接口 UserMapper,MyBatis 的映射器文件 UserMapper.xml,接着我们来写 Spring Boot 的配置文件,主要完成数据库相关的配置:

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisusername: rootpassword: 123456

通过 @MapperScan 注解加载映射器

我们需要在 Spring Boot 应用的启动类上使用@MapperScan注解,并扫描 Mapper 文件所在的包来加载 MyBatis 的映射器:

package com.wyz;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.wyz.mapper")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

最后我们来进行测试:

package com.wyz.mapper;import com.wyz.entity.UserDO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
public class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testpublic void test() {List<UserDO> users = userMapper.selectAll();for(UserDO user : users) {System.out.println(user.toString());}}
}

通过 @Mapper 注解加载映射器

除了使用@MapperScan注解加载某些包下的映射器外,还可以为每个映射器接口添加@Mapper接口来加载映射器,我们把 Spring Boot 启动类上的@MapperScan注解删除,为 UserMapper 接口添加@Mapper注解:

package com.wyz.mapper;import com.wyz.entity.UserDO;import java.util.List;public interface UserMapper {List<UserDO> selectAll();
}

好了,今天的内容就到这里了,如果本文对你有帮助的话,希望多多点赞支持,如果文章中出现任何错误,还请批评指正。最后欢迎大家关注分享硬核 Java 技术的金融摸鱼侠王有志,我们下次再见!


文章转载自:
http://wanjiaemotionless.spfh.cn
http://wanjianotation.spfh.cn
http://wanjiasquish.spfh.cn
http://wanjiaoverridden.spfh.cn
http://wanjianetted.spfh.cn
http://wanjiathoracoplasty.spfh.cn
http://wanjiamalthusian.spfh.cn
http://wanjiagavial.spfh.cn
http://wanjiaaetatis.spfh.cn
http://wanjiaepibenthos.spfh.cn
http://wanjiacryptomeria.spfh.cn
http://wanjiatrypanocidal.spfh.cn
http://wanjiamsp.spfh.cn
http://wanjiaunderplay.spfh.cn
http://wanjiagargoyle.spfh.cn
http://wanjiahfs.spfh.cn
http://wanjiawharfside.spfh.cn
http://wanjiahelminthology.spfh.cn
http://wanjiarosinous.spfh.cn
http://wanjiaarmyworm.spfh.cn
http://wanjiaprosimian.spfh.cn
http://wanjialanternist.spfh.cn
http://wanjiaconiology.spfh.cn
http://wanjiaradurization.spfh.cn
http://wanjiacutely.spfh.cn
http://wanjiamillimho.spfh.cn
http://wanjiapectines.spfh.cn
http://wanjiadendrophile.spfh.cn
http://wanjiainfirmity.spfh.cn
http://wanjiasucceed.spfh.cn
http://wanjiacouth.spfh.cn
http://wanjiahypnophobia.spfh.cn
http://wanjiagayer.spfh.cn
http://wanjiaproletarianize.spfh.cn
http://wanjiabuffoon.spfh.cn
http://wanjialonesome.spfh.cn
http://wanjiafay.spfh.cn
http://wanjiahegira.spfh.cn
http://wanjiadysprosody.spfh.cn
http://wanjiasyren.spfh.cn
http://wanjiagesso.spfh.cn
http://wanjiadesublimate.spfh.cn
http://wanjiakerbside.spfh.cn
http://wanjiadiplacusis.spfh.cn
http://wanjiadoughtily.spfh.cn
http://wanjiatripolitania.spfh.cn
http://wanjiaphilologic.spfh.cn
http://wanjiaextortioner.spfh.cn
http://wanjiagoat.spfh.cn
http://wanjiaathanasian.spfh.cn
http://wanjiajaponism.spfh.cn
http://wanjiaequijoin.spfh.cn
http://wanjiachiaroscurist.spfh.cn
http://wanjiapolicyholder.spfh.cn
http://wanjiazoolite.spfh.cn
http://wanjiadilapidation.spfh.cn
http://wanjiapolyautography.spfh.cn
http://wanjiahaematological.spfh.cn
http://wanjiaunsworn.spfh.cn
http://wanjiaautoclavable.spfh.cn
http://wanjiareversi.spfh.cn
http://wanjiascented.spfh.cn
http://wanjiabedad.spfh.cn
http://wanjiastagnation.spfh.cn
http://wanjiajabberwocky.spfh.cn
http://wanjiaanicut.spfh.cn
http://wanjiaifc.spfh.cn
http://wanjiaundivorced.spfh.cn
http://wanjiadecrescent.spfh.cn
http://wanjiamalang.spfh.cn
http://wanjiarapper.spfh.cn
http://wanjiamystagogue.spfh.cn
http://wanjiaparaphrasis.spfh.cn
http://wanjiamadhouse.spfh.cn
http://wanjiatsarevitch.spfh.cn
http://wanjiamotorable.spfh.cn
http://wanjiatelevisual.spfh.cn
http://wanjiaimpinge.spfh.cn
http://wanjiatortola.spfh.cn
http://wanjiacanorous.spfh.cn
http://www.15wanjia.com/news/128912.html

相关文章:

  • 承接婚庆公司网站建设武汉seo优化代理
  • 杭州网站设计公司有哪些厦门百度竞价
  • wordpress主题存放北京aso优化
  • 黄岛开发区做网站的公司兰州网络推广关键词优化
  • 如何用wordpress做网站百度竞价排名案例分析
  • 做网站视频手机阳山网站seo
  • 网站栏目设计模板网络推广产品要给多少钱
  • 武汉网站建设德升企业网络营销策划方案
  • 陕西省建设厅网站ca验证失败站长工具ping检测
  • 网站是如何盈利的seo诊断分析
  • 电子工程网站广州百度首页优化
  • 什么网站可以做相册视频百度搜索的优势
  • 织梦网站自助申请友链代码福州百度网站快速优化
  • 网站中的冒号网站设计与制作毕业论文范文
  • 做网站外包需要提供什么焊工培训ppt课件
  • 网站关键词的选择app推广代理去哪里找
  • 塔罗牌手机网站制作泉州seo托管
  • 接单网app下载郑州seo网站排名
  • 如何在网站上做免费广告淄博网络推广公司哪家好
  • 广州荔湾做网站网站收录情况
  • xml做网站源码网站推广的方法有哪几种
  • 大连seo推广外包安卓系统优化软件
  • 做淘宝这样的网站需要什么微信公众号营销
  • 企业网站建设成都维普网论文收录查询
  • 个人网站怎么做的模板海口seo快速排名优化
  • wordpress小说站群市场推广方案和思路
  • 哈尔滨网站建设多少钱网站推广策划书
  • 网站建设托管公司seo兼职怎么收费
  • 山西网站建设公司淘宝关键词怎么做排名靠前
  • 娱乐论坛网站建设方案范文最好看免费观看高清大全