Mybatis框架学习总结-优化Mybatis配置文件中的配置
连接数据库的配置单独放在一个properties文件中
之前,是直接将数据库的连接配置信息卸载了Mybatis的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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://10.0.20.252:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="Free-Wi11"/>
</dataSource>
</environment>
</environments> <mappers>
<!-- 注册userMapper.xml文件,
userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->
<mapper resource="com/fpc/Mapping/userMapper.xml"/>
<!-- 注册UserMapper映射接口-->
<!-- -->
<mapper class="com.fpc.Mapping.UserMapperI"/>
</mappers> </configuration>
其实完全可以将数据库的连接配置信息写在一个properties文件中,然后再conf.xml文件中引用properties文件,具体的做法是:
1.在src目录下创建一个db.properties文件,如下图所示:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://10.0.20.252:3306/mybatis
name=root
password=Free-Wi11
2.在Mybatis的conf.xml文件中引用db.properties文件,如下:
<?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>
<!-- 引用db.properties配置文件 -->
<properties resource="db.properties"></properties>
<!--
development:开发模式
work:工作模式
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<!-- value属性值引用db.properties配置文件中配置的值 -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <mappers>
<!-- 注册userMapper.xml文件,
userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->
<mapper resource="com/fpc/Mapping/userMapper.xml"/>
<!-- 注册UserMapper映射接口-->
<!-- -->
<mapper class="com.fpc.Mapping.UserMapperI"/>
</mappers> </configuration>
为实体类定义别名,简化sql映射xml文件中的引用
之前,我们在sql映射xml文件中的引用实体类时,需要写上实体类的全类名(包名+类名),如下:
<!-- 创建用户Create -->
<insert id="addUser" parameterType="com.fpc.Entity.User">
insert into users(name,age) values(#{name},#{age})
</insert> <!-- 更新用户 -->
<update id="updateUser" parameterType="com.fpc.Entity.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
parameterType="com.fpc.Entity.User"这里写的实体类User的全类名com.fpc.Entity.User,每次都写这么一长串内容挺麻烦的,而我们希望能够简写成下面的形式:
<!-- 创建用户Create -->
<insert id="addUser" parameterType="_User">
insert into users(name,age) values(#{name},#{age})
</insert> <!-- 更新用户 -->
<update id="updateUser" parameterType="_User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
parameterType="_User"这样写就简单多了,为了达到这种效果,我们需要再conf.xml文件中为实体类=“com.fpc.Entity.User”定义一个别名"_User",具体做法如下:
在conf.xml文件中<configuration></configuration>标签中添加如下配置:
<typeAliases>
<typeAlias type="com.fpc.Entity.User" alias="_User"/>
</typeAliases>
这样就可以为com.fpc.Entity.User类定义了一个别名_User,以后_User就代表了com.fpc.Entity.User类,这样sql映射xml文件中的凡是需要引用com.fpc.Entity.User类的地方都可以使用_User来代替,这就达到了一个简化实体类引用的目的。
除了可以使用<typeAlias type="com.fpc.Entity.User" alias="_User"/>这种方式单独为某一个实体类设置别名之外,我们还可以使用如下的方式批量为某个包下的所有实体类设置别名,如下:
<typeAliases>
<!-- <typeAlias type="com.fpc.Entity.User" alias="_User"/> -->
<package name="com.fpc.Entity"/>
</typeAliases>
<package name="com.fpc.Entity"/>就表示这个包下面的所有实体类设置别名。Mybatis默认的设置别名的方式就是去除类所在的包后的简单的类名,比如com.fpc.Entity.User这个实体类的别名就会被设置成为User。
Mybatis框架学习总结-优化Mybatis配置文件中的配置的更多相关文章
- MyBatis入门学习教程-优化MyBatis配置文件中的配置
一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: 1 <?xml version=" ...
- Mybatis框架学习总结-使用Mybatis对表执行CRUD操作
使用MyBatis对表执行CRUD操作——基于XML的实现 1.创建(create)用户:在userMapper.xml文件中增加: <!-- 创建用户Create --> <ins ...
- 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置
[转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...
- Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突
原文链接:http://www.cnblogs.com/xdp-gacl/p/4264301.html http://www.cnblogs.com/xdp-gacl/p/4264425.ht ...
- 03 Mybatis框架---学习笔记1--框架的概念及优势
1.框架的概念 框架其实就是某种应用的半成品,就是一组组件,供你选用完成你自己的系统.简单说就是使用别人搭好的舞台,你来做表演.而且,框架一般是成熟的,不断升级的软件.框架是我们软件开发中的一套解决方 ...
- MyBatis(三):核心配置文件的重要配置
本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出1便就懂!b站搜索狂神说即可 https://space.bilibili.com/95256449?spm_id_from=333.788 ...
- Prometheus 配置文件中 metric_relabel_configs 配置--转载
Prometheus 配置文件中 metric_relabel_configs 配置 参考1:https://www.baidu.com/link?url=YfpBgnD1RoEthqXOL3Lgny ...
- MyBatis的学习总结三:优化MyBatis配置文件中的配置
一.优化Mybatis配置文件conf.xml中数据库的信息 1.添加properties的配置文件,存放数据库的信息:mysql.properties具体代码: driver=com.mysql.j ...
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013548.html 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置 ...
随机推荐
- am335x文件系统 /etc/fstab的设置
# ...
- SpringBoot DataSource 配置说明
DataSource 配置说明 属性 说明 spring.dao.exceptiontranslation.enabled 是否开启PersistenceExceptionTranslationPos ...
- C++ 函数的扩展②
//函数扩展--默认参数和占位参数 (了解) #include<iostream> using namespace std; /* 可以将占位参数与默认参数结合起来使用 意义 为以后程序扩 ...
- C语言0长度数组(柔性数组)
0长度数组,又称为柔性数组(flexible array).通经常使用来实现变长数组.常见于TLV(type-length-value)的数据结构中. 在标准 C 和 C++ 中,不同意用 0 长度数 ...
- java中main方法的 (String []args)
java中main方法的 (String []args) String[] args是main函数的形式参数,可以用来获取命令行用户输入进去的参数.java 本身不存在不带String ...
- HMLocationEvent
HMLocationEvent *locEvent = [[HMLocationEvent alloc] initWithRegion:region1]; region1.notifyOnEntry ...
- url 模式录制脚本web_concurrent_start和web_concurrent_end
LoadRunner函数中文翻译系列之三--Concurrent Groupweb_concurrent_start 语法: int web_concurrent_start ( [char * Co ...
- 权限模块_使用权限_实现主页面的效果_显示左侧菜单&只显示有权限的菜单项
权限模块__使用权限__实现主页面的效果 HomeAction.java public class HomeAction extends ActionSupport { public String i ...
- ArcGIS ArcMap 问题(ArcMap闪退、cx_oracle安装不上)
一.问题描述 1.ArcMap闪退 2.安装32位cx_oracle提示python目录不存在 二.解决方案 1.修改pythoncore的文件目录,指向C:\Python27\ArcGIS10.3\ ...
- leetcode -- Best Time to Buy and Sell Stock III TODO
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...