mybatis(4)---优化配置文件中的配置
2017-11-02 23:19
281 查看
mybatis的配置文件优化
优化1:将mybatis.cfg.xml配置文件中的数据库连接配置单独的放在一个properties文件中
优化前的配置:
<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://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
优化后的配置:
首先在src下新建properties配置文件,如db.properties,代码如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
user=root
password=root然后修改配置文件,如下:
<!-- 1、引入db.properties配置文件 -->
<properties resource="db.properties" />
<!--
development为开发模式
work为工作模式
-->
<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="${user}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
优化2:为实体类设置别名
在sql映射文件中引用实体类时,需要写上实体类的包名+类名
如:<select id="selectUser" parameterType="int"
resultType="com.cn.vo.UserVO">
select * from t_user where id=#{id}
</select>
parameterType="com.cn.vo.UserVO",每次这样写很麻烦
可以修改为:
<select id="selectUser" parameterType="int"
resultType="_User">
select * from t_user where id=#{id}
</select>为了达到这种效果,需要在mybatis.cfg.xml中增加如下配置:
<!-- 2、设置别名 -->
<typeAliases>
<typeAlias type="com.cn.vo.UserVO" alias="_User" />
</typeAliases>该配置加载<configuration></configuration>标签对中即可
上面的配置是为单个实体类设置别名,实际开发中应为多个实体类设置别名
修改后配置如下:
<!-- 2、设置别名 -->
<typeAliases>
<package name="com.cn.vo" />
</typeAliases>这样修改后,别名就为com.cn.vo.UserVO中实体类的名称UserVO
优化1:将mybatis.cfg.xml配置文件中的数据库连接配置单独的放在一个properties文件中
优化前的配置:
<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://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
优化后的配置:
首先在src下新建properties配置文件,如db.properties,代码如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
user=root
password=root然后修改配置文件,如下:
<!-- 1、引入db.properties配置文件 -->
<properties resource="db.properties" />
<!--
development为开发模式
work为工作模式
-->
<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="${user}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
优化2:为实体类设置别名
在sql映射文件中引用实体类时,需要写上实体类的包名+类名
如:<select id="selectUser" parameterType="int"
resultType="com.cn.vo.UserVO">
select * from t_user where id=#{id}
</select>
parameterType="com.cn.vo.UserVO",每次这样写很麻烦
可以修改为:
<select id="selectUser" parameterType="int"
resultType="_User">
select * from t_user where id=#{id}
</select>为了达到这种效果,需要在mybatis.cfg.xml中增加如下配置:
<!-- 2、设置别名 -->
<typeAliases>
<typeAlias type="com.cn.vo.UserVO" alias="_User" />
</typeAliases>该配置加载<configuration></configuration>标签对中即可
上面的配置是为单个实体类设置别名,实际开发中应为多个实体类设置别名
修改后配置如下:
<!-- 2、设置别名 -->
<typeAliases>
<package name="com.cn.vo" />
</typeAliases>这样修改后,别名就为com.cn.vo.UserVO中实体类的名称UserVO
相关文章推荐
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis笔记--优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis (3)——优化MyBatis配置文件中的配置
- MyBatis学习总结_03_优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- Mybatis总结(3)--优化Mybatis配置文件的配置
- MyBatis 配置文件优化
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis学习笔记(四)优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- 优化MyBatis配置文件中的配置_3
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置