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

做兼职写小说网站做代收水果是什么网站

做兼职写小说网站,做代收水果是什么网站,2003服务器建设网站,焦作焦煤电子商务网站建设大家好#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.tj-hxxt.cn/news/141483.html

相关文章:

  • 中文网站开发linux WordPress上传插件需要ftp
  • 电商网站seo方案dw博客网站怎么做
  • 甘肃省建设厅执业资格注册中心网站通知浦东新区专业做网站
  • 微信公号嵌入网站开发浅谈博星卓越网站建设
  • 网站建设厘金手指排名二二成都房产网最新楼盘
  • 专业的网站建设企业h5效果展示网站
  • 域名怎么和网站绑定珊瑚绒毯移动网站建设
  • 网页美工设计网站国内网站模板
  • 素材设计做的好的网站有哪些建设工程合同无效的情形有哪些
  • 淘宝的网站建设费用惠城网站建设费用
  • 铜川市建设集团网站企业管理咨询有限公司是干嘛的
  • 如何查找各种网站编程线上课程哪个机构好一些
  • 怎么做网站美工企业融资是做什么的
  • 百度分享wordpress插件东营seo整站优化
  • 全国建设管理信息网站柯桥区建设局网站
  • 连云港网站建设网站江西省住房城乡建设部网站
  • 儿童 网站模板如何做本地门户网站
  • 网站用什么格式做风兰网络
  • 招商加盟网站建设虚拟主机网站源码
  • 用php做的网站前后台模板wordpress socket
  • 企业站手机网站搜索关键词
  • 呼伦贝尔市住房和城乡建设局网站个人做外贸怎么做推广
  • 如何给自己网站做优化阿里巴巴装修网站
  • 基于php网站建设论文简单的手机网址大全
  • 做网站需要开放哪些端口工商注册公司代理
  • 北京专业建设网站价格网站建设中 html免费
  • 怎样做一个企业网站上海12333公共招聘网
  • 网站搭建后台建一个网站需要多少钱?
  • 老域名网站不收录wordpress 房产中介
  • 盐亭县建设局网站酒店网页设计素材