电子商务平台(网站)建设方式,免费域名申请网站大全,杭州网站搭建,免费做h5的网站目录
MyBatis配置
1.properties标签
2.typeAliases标签
3.Mappers标签 一个最全面的MyBatis配置文件可能会包含各种不同的设置和选项#xff0c;根据实际情况#xff0c;可以根据需要添加或删除配置。以下是一个包含各种可能设置的示例。
这个配置文件包含了环境设置、数…目录
MyBatis配置
1.properties标签
2.typeAliases标签
3.Mappers标签 一个最全面的MyBatis配置文件可能会包含各种不同的设置和选项根据实际情况可以根据需要添加或删除配置。以下是一个包含各种可能设置的示例。
这个配置文件包含了环境设置、数据源配置、映射器配置、属性配置、类型别名、类型处理器、全局设置以及插件配置。根据具体需求可以根据这个模板添加或删除配置项并根据需要进行调整。不同项目的配置可能会有所不同所以可以根据实际情况灵活调整。
更多设置项请参考官方文档mybatis – MyBatis 3 | Configuration
MyBatis配置
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtdconfiguration!-- 设置环境 --environments defaultdevelopmentenvironment iddevelopment!-- 配置事务管理器 --transactionManager typeJDBC /!-- 配置数据源 --dataSource typePOOLEDproperty namedriver valueyour_database_driver /property nameurl valueyour_database_url /property nameusername valueyour_username /property namepassword valueyour_password //dataSource/environment/environments!-- 映射器Mapper的配置 --mappers!-- 使用资源引用映射器文件 --mapper resourcecom/example/YourMapper.xml /!-- 或者使用类引用映射器 --!-- mapper classcom.example.YourMapper / --!-- 或者使用包名来扫描映射器 --!-- package namecom.example.mappers / --/mappers!-- 其他配置 --properties!-- 设置属性 --property namelogImpl valueSTDOUT_LOGGING //properties!-- 类型别名 --typeAliases!-- 指定类型别名 --!-- typeAlias aliasAuthor typecom.example.Author / --/typeAliases!-- 类型处理器 --typeHandlers!-- 注册类型处理器 --!-- typeHandler handlercom.example.CustomTypeHandler / --/typeHandlers!-- 全局设置 --settings!-- 各种设置项 --!-- setting namecacheEnabled valuetrue / --!-- setting namelazyLoadingEnabled valuetrue / --!-- setting namemultipleResultSetsEnabled valuetrue / --!-- setting nameuseColumnLabel valuetrue / --!-- setting namedefaultExecutorType valueSIMPLE / --!-- 更多设置项请参考官方文档 --/settings!-- 插件配置 --plugins!-- 配置插件 --!-- plugin interceptorcom.example.MyPluginproperty namesomeProperty value100 //plugin --/plugins
/configuration1.properties标签 在MyBatis的配置文件中properties 标签用于定义属性这些属性可以在配置文件中多处引用使得配置更加灵活和易于维护。它允许你定义一些可重复使用的值并在其他地方引用这些值 基本结构
properties resourceyour_properties_file.propertiesproperty namepropertyName valuepropertyValue /!-- 可以包含多个 property 标签 --
/properties两种用法
1.使用外部资源使用 resource 属性引用外部属性文件如 .properties 文件
properties resourceyour_properties_file.properties /这样做的好处在于可以将配置的属性值存储在一个单独的文件中使得配置文件更加清晰和易于管理。
2.直接定义属性直接在 properties 标签内部使用 property 标签定义属性。
propertiesproperty namepropertyName valuepropertyValue /!-- 可以定义多个属性 --
/properties这种方式允许你在配置文件内部定义属性以供后续引用和使用。
然后你可以在配置文件的其他地方通过 ${propertyName} 的形式来引用这些属性值。例如
db.properties !--mysql8--!--property namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1:3306/mybatis?useSSLfalseamp;serverTimezoneAsia/Shanghai/ --!--mysql5--
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://127.0.0.1:3305/mybatis?characterEncodingUTF-8
jdbc.usernameroot
jdbc.password
在mybatis-config.xml引入数据库配置信息 ?xml version1.0 encodingUTF-8?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd
configuration!--引入db.properties--properties resourcedb.properties/propertiesenvironments defaultmysqlenvironment idmysqltransactionManager typeJDBC/transactionManagerdataSource typePOOLED!--使用${}占位符获取配置信息--property namedriver value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//dataSource/environment/environmentsmappersmapper resourcecom/by/mapper/RoleMapper.xml//mappers
/configuration
2.typeAliases标签 typeAliases 标签在MyBatis配置文件中用于定义类型别名它允许你为Java类型指定简短的别名以在MyBatis映射文件中引用这些类型。 基本结构
typeAliasestypeAlias aliasAliasName typefully.qualified.name.of.Type /!-- 可以定义多个 typeAlias 标签 --
/typeAliases其中
alias 属性用于定义你想要使用的别名。type 属性指定了对应的Java类型的完全限定名。
查看mybatis源码可以看到 Mybatis 默认支持的别名 public TypeAliasRegistry() {this.registerAlias(string, String.class);this.registerAlias(byte, Byte.class);this.registerAlias(long, Long.class);this.registerAlias(short, Short.class);this.registerAlias(int, Integer.class);this.registerAlias(integer, Integer.class);this.registerAlias(double, Double.class);this.registerAlias(float, Float.class);this.registerAlias(boolean, Boolean.class);this.registerAlias(byte[], Byte[].class);this.registerAlias(long[], Long[].class);this.registerAlias(short[], Short[].class);this.registerAlias(int[], Integer[].class);this.registerAlias(integer[], Integer[].class);this.registerAlias(double[], Double[].class);this.registerAlias(float[], Float[].class);this.registerAlias(boolean[], Boolean[].class);this.registerAlias(_byte, Byte.TYPE);this.registerAlias(_long, Long.TYPE);this.registerAlias(_short, Short.TYPE);this.registerAlias(_int, Integer.TYPE);this.registerAlias(_integer, Integer.TYPE);this.registerAlias(_double, Double.TYPE);this.registerAlias(_float, Float.TYPE);this.registerAlias(_boolean, Boolean.TYPE);this.registerAlias(_byte[], byte[].class);this.registerAlias(_long[], long[].class);this.registerAlias(_short[], short[].class);this.registerAlias(_int[], int[].class);this.registerAlias(_integer[], int[].class);this.registerAlias(_double[], double[].class);this.registerAlias(_float[], float[].class);this.registerAlias(_boolean[], boolean[].class);this.registerAlias(date, Date.class);this.registerAlias(decimal, BigDecimal.class);this.registerAlias(bigdecimal, BigDecimal.class);this.registerAlias(biginteger, BigInteger.class);this.registerAlias(object, Object.class);this.registerAlias(date[], Date[].class);this.registerAlias(decimal[], BigDecimal[].class);this.registerAlias(bigdecimal[], BigDecimal[].class);this.registerAlias(biginteger[], BigInteger[].class);this.registerAlias(object[], Object[].class);this.registerAlias(map, Map.class);this.registerAlias(hashmap, HashMap.class);this.registerAlias(list, List.class);this.registerAlias(arraylist, ArrayList.class);this.registerAlias(collection, Collection.class);this.registerAlias(iterator, Iterator.class);this.registerAlias(ResultSet, ResultSet.class);}例如假设有一个Java类 com.example.Author你可以在 typeAliases 中定义别名
typeAliasestypeAlias aliasAuthor typecom.example.Author /
/typeAliases这样在MyBatis映射文件中就可以使用 Author 作为 com.example.Author 类的别名。
也可以使用包名来注册别名这样就能够批量注册某个包下所有类的别名如下所示
typeAliasespackage namecom.example.models /
/typeAliases这将注册 com.example.models 包下所有类的别名别名默认使用类名不区分大小写。例如com.example.models.Author 类可以直接在映射文件中使用别名 Author。
3.Mappers标签 mappers 标签是用于配置MyBatis映射器Mapper的标签在MyBatis的配置文件中用于指定映射器文件的位置或者扫描映射器的包名。 这个标签可以包含多个子标签每个子标签可以是 mapper、package 或 resource。 Mappers标签的作用是用来在核心配置文件里面引入映射文件引入方式有如下三种
1.使用mapper映射文件的路径 mappersmapper resourcecom/by/mapper/RoleMapper.xml/mapper/mappers
2.使用mapper接口的路径
mappersmapper classcom.by.mapper.RoleMapper/mapper
/mappers
注意此种方法要求 mapper 接口名称和 mapper 映射文件名称相同
3.使用mapper接口的包名批量引入
mapperspackage namecom.by.mapper/
/mappers
注意此种方法要求 mapper 接口名称和 mapper 映射文件名称相同