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

做兼职写小说网站杭州室内设计公司

做兼职写小说网站,杭州室内设计公司,网站建设原则应考虑哪些内容,龙岗网站制作培训班大家好#xff0c;我是王有志。 今天给大家带来的是一道来自京东的 MyBatis 面试题#xff1a;MyBatis 中有几种加载映射器#xff08;Mapper.xml#xff09;的方式#xff1f; 常见加载 MyBatis 映射器的方式有 5 种#xff0c;可以根据不同的使用方式来进行具体区分我是王有志。 今天给大家带来的是一道来自京东的 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 version1.0 encodingUTF-8 ? !DOCTYPE configuration PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtdconfiguration!-- 省略环境配置 --mappersmapper resourcemapper/UserMapper.xml/mapper resourcemapper/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 对象 UserDOJava 接口 UserMapper 的创建接着是编写 MyBatis 的映射器 UserMapper.xml最后我们还需要引入相关依赖 dependencies!-- 省略 MySQLjunit等依赖 --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion3.0.3/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion6.1.4/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion6.1.4/version/dependency /dependencies接下来我们通过注入 Spring Bean 的方式来加载 Mapper.xml这里我们创建 spring-beans.xml 配置文件 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdbean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/mybatis/property nameusername valueroot/property namepassword value123456//beanbean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource/property namemapperLocations valuemapper/UserMapper.xml//bean!-- 通过 Spring Bean 的方式加载 MyBatis 映射器--bean iduserMapper classorg.mybatis.spring.mapper.MapperFactoryBeanproperty namemapperInterface valuecom.wyz.mapper.UserMapper/property namesqlSessionFactory refsqlSessionFactory//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);ListUserDO users userMapper.selectAll();for(UserDO user : users) {System.out.println(user.toString());}} }MyBatis 与 Spring Boot 集成 MyBatis 与 Spring Boot 集成后可以通过注解的方式或配置文件的方式加载 MyBatis 的映射器Mapper.xml使用起来非常的方便。首先我们还是做好前期的准备创建 Java 对象 UserDOJava 接口 UserMapperMyBatis 的映射器文件 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() {ListUserDO 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 {ListUserDO selectAll(); }好了今天的内容就到这里了如果本文对你有帮助的话希望多多点赞支持如果文章中出现任何错误还请批评指正。最后欢迎大家关注分享硬核 Java 技术的金融摸鱼侠王有志我们下次再见
http://www.hkea.cn/news/14504659/

相关文章:

  • 网站默认模板广西住房城乡建设领域
  • 网站模板但没有后台如何做网站曲靖市网站建设
  • 网站做哪些比较有意思文网文网站建设
  • 中核工建设集团网站sem是什么品牌
  • 网站开发和沈阳seo专业培训
  • 免费ppt模板大全下载的网站请人做网站需要多少钱
  • 做网站需要哪些资质企业网站的特征
  • 网页网站的区别怎么查网站的备案号
  • 深圳市城乡住房和建设局网站首页中国建筑集团有限公司官网首页
  • 建站怎么赚钱wordpress必须先登录
  • 佛山网站seo东莞在那里建个网站
  • 商城型网站的概念建设银行钓鱼网站
  • 做家纺的主要国际网站阳江招聘网最新招聘
  • 免费的软件下载网站香河做网站公司
  • 河南网站建设哪家公司好有限公司破产债务怎么办
  • 自助建站系统搭建网站建一个团购网站需要多少钱
  • 哪里有手机网站建设联系方式核工业南京建设集团网站
  • 成都网站建设3六六百度站长平台开绿色收录通道加快网站收录
  • php怎么做直播网站吗wordpress标签管理
  • 做金融服务网站赚钱做的好的手机网站有哪些
  • 做棋牌推广网站违反不校园网站cms
  • 公司名被注册网站有经验的盐城网站开发
  • 做网站框架可用jpg图吗毕节做网站优化
  • 电子网站开发技术包括诸城网站制作
  • 企业网站建设排名河北网站建设seo优化营销制作设计
  • 牡丹江地区做网站的公司网站建设在线培训
  • 至少保存十个以上域名网站欧美品牌网站设计
  • 江西企业 网站建设网站视频插件怎么做
  • 深圳网站建设公司公司网络营销竞价推广
  • 广州网站开发定制设计服装网站建设与规划