首页 技术 正文
技术 2022年11月8日
0 收藏 873 点赞 1,888 浏览 4331 个字

MyBatis 实用篇(二)配置文件

一、全局配置

全局配置:http://www.mybatis.org/mybatis-3/zh/configuration.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>
<!-- 1. 引入配置文件 .properties -->
<properties resource="conf.properties"/> <!-- 2. settings 配置项 -->
<settings>
<!--开启数据库下划线转驼峰规则-->
<setting name="mapUnderscoreToCamelCase" value="true"/> <!--开启分步查询的懒加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/> <!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings> <!-- 3. typeAliases 别名处理器,为 java 类型取别名 -->
<typeAliases>
<!-- 3.1 typeAlias: 为 java 类取别名
type 为类的全路径,alias 默认为类名的小写 -->
<!-- <typeAlias alias="User" type="com.github.binarylei.spring03.day0902.User"/> --> <!-- 3.2 package 为某个包下的所有类批量取别名,默认类名小写,注意别名不区分大小写 -->
<package name="com.github.binarylei.mybatis.helloworld"/> <!-- 3.3 注解取别名 @Alias("user") -->
</typeAliases> <!-- 4. typeHandlers 类型处理器 -->
<typeHandlers/> <!-- 5. plugins 插件,如分布插件等 -->
<!--<plugins>
<plugin interceptor=""/>
</plugins>--> <!-- 6. environments 数据库配置,mybatis 可以配置多种环境
environment 配置一个具体的环境,必须有 transactionManager 和 dataSource 两个元素
transactionManager 事务管理器
type: JDBC(JdbcTransactionFactory)|MANAGED(ManagedTransactionFactory)
自定义事务管理器:实现 TransactionFactory 接口
dataSource 数据源
type: POOLEDJNDI(PooledDataSourceFactory)|UNPOOLEDJNDI(PooledDataSourceFactory)|JNDI(JndiDataSourceFactory)
自定义数据源:实现 DataSourceFactory 接口
JDBC|MANAGED 等别名的注册在 org.apache.ibatis.session.Configuration 中定义
-->
<environments default="dev">
<environment id="test">
<!-- 配置事务管理 ,采用JDBC管理事务-->
<transactionManager type="JDBC"/>
<!-- POOLED是mybatis的 数据源 -->
<!-- JNDI是基于tomcat的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment> <environment id="dev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments> <!-- 7. databaseIdProvider -->
<databaseIdProvider type="DB_VENDOR">
<property name="SQL Server" value="sqlserver"/>
<property name="DB2" value="db2"/>
<property name="Oracle" value="oracle"/>
<property name="MySQL" value="mysql"/>
<property name="PostgreSQL" value="postgresql"/>
<property name="Derby" value="derby"/>
<property name="HSQL" value="hsql"/>
<property name="H2" value="h2"/>
</databaseIdProvider> <!-- 8. 将 sql 注册到全局配置中 -->
<mappers>
<!--
mapper: 注册一个 sql 映射
7.1 resource: 引用 classpath 下的 sql 映射文件 eg: com/github/binarylei/mybatis/helloworld/UserMapper.xml
7.2 url: 引用网络或磁盘路径下的 sql 映射文件 eg: file:///var/mybatis/UserMapper.xml
7.3 class: 引用(注册)接口。
1. 有 sql 映射文件,映射文件名必须和接口同名,并且放在与接口同一目录下
2. 没有 sql 映射文件,所有的接口都写到注解上 eg: @select("select * from user")
-->
<!--<mapper resource="com/github/binarylei/mybatis/helloworld/UserMapper.xml"/>-->
<!--<mapper class="com.github.binarylei.mybatis.helloworld.UserMapper"/>--> <!-- 7.4 批量包扫描 -->
<package name="com.github.binarylei.mybatis"/>
</mappers>
</configuration>

二、mapper 配置

mapper 配置:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.github.binarylei.mybatis.helloworld.UserMapper"> <!--eviction: 緩存的回收策路。
LRU 默认,最近最少使用的:移除最长时间不被使用的对象。
FTFO 先进先出:按对家进入缓存的顺序来移除它们。
SOFT 软引用:移除基于垃圾回收器状态和软引用规则的对象。
WEAK 弱引用:更积极地移除基于拉圾收集器状态和弱引用规则。
flushInterval: 存刷新间隔缓存多长时间清空一次,默认不清空,设置一个毫秒值。
readon1y: 是否只读 true。
true: MyBatis 直接就会将数据在存中的引用交给用户。不安全,速度快
fa1se: MyBatis 会利用反序列的技术克隆一份新的数据给你。安全,速度慢
size: 缓存存放多少元素
type: 指定自定义存的全类名实现 Cache 接口即可
-->
<cache eviction="LRU" flushInterval="1000" readOnly="false" size="20"/> <!-- 获取自增主键,方式一 -->
<insert id="save" parameterType="user" useGeneratedKeys="true" keyProperty="id">
insert into user(name,age,sex) values(#{name},#{age},#{sex})
</insert> <!-- 获取自增主键,方式二 -->
<insert id="save2" parameterType="user">
<selectKey resultType="int" keyProperty="id" order="AFTER">
select last_insert_id();
</selectKey>
insert into user(name,age,sex) values(#{name},#{age},#{sex})
</insert> <!-- useCache: true 表示开启二级缓存。
flushCache: true 表示每次使用时都会清除缓存,包括一级缓存和二级缓存
-->
<select id="getUser" resultType="User" useCache="false" flushCache="true">
select * from user where id=#{id};
</select>
</mapper>

每天用心记录一点点。内容也许不重要,但习惯很重要!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291