四、优化

1.db.properties

可以将配置信息单独放入db.properties文件中,然后再动态引入
db.properties:k=v(在conf.xml中通过k的值来取v的值)

<configuration>
	<properties resource="db.properties"/>
</configuration>

引入之后,使用${key}

(1)db.properties

driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username=scott
password=tiger

(2)conf.xml

<?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>

	<properties resource="db.properties"/>
	
	<!-- 
		<settings>
		<setting name="cacheEnabled" value="false"  />
		<setting name="lazyLoadingEnabled" value="false"  />
		</settings>
	-->
	
	<!--  定义别名-->
	<typeAliases>
		<typeAlias type="nuc.hzb.entity.Student" alias="student"/>
	</typeAliases>
	
	<environments default="development">
	
		<environment id="development">
		
			<transactionManager type="JDBC" />	
			
			<dataSource type="POOLED">		
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>	
			
		</environment>
		
	</environments>
	
	<mappers>
		<mapper resource="nuc/hzb/mapper/studentMapper.xml" />
	</mappers>
	
</configuration>

2.MyBatis全局参数

在conf.xml中设置

<properties resource="db.properties"/>
<!-- 
	<settings>
	<setting name="cacheEnabled" value="false"  />
	<setting name="lazyLoadingEnabled" value="false"  />
	</settings>
-->

<!--  定义别名-->
<typeAliases>
	<typeAlias type="nuc.hzb.entity.Student" alias="student"/>
</typeAliases>

3.别名

在conf.xml中定义

(1)设置单个别名

<typeAlias type="nuc.hzb.entity.Student" alias="student"/>

(2)批量设置别名

<!--  定义别名-->
<typeAliases>
	<!-- <typeAlias type="nuc.hzb.entity.Student" alias="student"/> -->
	<package name="nuc.hzb.entity"/>
    <!-- 单个别名(别名忽略大小写)-->
	<!-- <typeAlias type="nuc.hzb.entity.Student" alias="student"/> -->
	<!-- 批量定义别名 (别名忽略大小写),以下会自动将该包中的所有类批量定义别名:别名就是类名(不带包名,忽略大小写)-->
</typeAliases>

除了自定义别名外,MyBatis还内置了一些常见类的别名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值