您的位置:首页 > 其它

MyBatis 笔记(三)——优化配置

2017-08-21 23:50 225 查看
在之前的讲解中,不难发现 MyBatis 的配置有些笨重,这一节就讲 MyBatis 的配置优化。毫无疑问,主要是两方面:

1. 优化 MyBatis 基础配置文件。

2. 优化 MyBatis 映射文件。

优化 MyBatis 基础配置文件

在 MyBatis 基础配置文件中,之前的数据库配置都是在这个文件中完成的。实际上,数据库配置是可以抽出去的,如 db.properties:

DRIVER=com.mysql.cj.jdbc.Driver
URL=jdbc:mysql://127.0.0.1:3306/test?useSSL=true
USERNAME=root
PASSWORD=123456


然后,在 MyBatis 的基础配置文件中的 configuration 标签里配置如下:

<!-- 引用db.properties配置文件 -->
<properties resource="db.properties"/>
<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>


优化 MyBatis 映射文件

在 MyBatis 映射文件中,之前对于数据库实体类的引用都是通过全类名(包名+类名)映射,这无疑产生了很多的冗余。为了解决这个问题,可以在 MyBatis 基础配置文件中,为每个实体类起一个别名。例如给实体类 edu.wzm.mybatis.domain.Person 别名为 Person,然后在映射文件中就可以直接用 Person 映射了:

<!-- 为实体类edu.wzm.mybatis.domain.Person配置一个别名_User -->
<typeAlias type="edu.wzm.mybatis.domain.Person" alias="Person"/>


当然,如果为每一个实体类都起别名显然也是很费事的,所以,可以以包为单位为每个类起一个别名,其中类的别名就是类名。这样就需要在 MyBatis 基础配置文件中,配置如下:

<!--
为edu.wzm.mybatis.domain包下的所有实体类配置别名,MyBatis默认的设置别名的方式就是去除类所在的包后的简单的类名。
例如:edu.wzm.mybatis.domain.Person这个实体类的别名就会被设置成Person
-->
<typeAliases>
<package name="edu.wzm.mybatis.domain"/>
</typeAliases>


完整的 MyBatis 基础配置文件:

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

<!-- 为实体类edu.wzm.mybatis.domain.Person配置一个别名_User -->
<!-- <typeAlias type="edu.wzm.mybatis.domain.Person" alias="Person"/> -->

<!-- 为edu.wzm.mybatis.domain包下的所有实体类配置别名,MyBatis默认的设置别名的方式就是去除类所在的包后的简单的类名。 例如:edu.wzm.mybatis.domain.Person这个实体类的别名就会被设置成Person --> <typeAliases> <package name="edu.wzm.mybatis.domain"/> </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>
<!-- 基于XML配置文件 -->
<mapper resource="edu/wzm/mybatis/mapping/PersonMapper.xml"/>
<!-- 基于注解 -->
<!--<mapper class="edu.wzm.mybatis.mapper.PersonMapper"/>-->
</mappers>
</configuration>


完整的 MyBatis 映射文件:

<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如:namespace="edu.wzm.mybatis.mapping.PersonMapper"(userMapper.xml文件去除后缀)
-->
<mapper namespace="edu.wzm.mybatis.mapping.PersonMapper">
<!--
在select、insert、update、delete标签中编写查询的SQL语句, 设置标签的id属性值必须是唯一的,不能够重复。使用parameterType属性
指明查询时,使用的参数类型。resultType属性指明查询返回的结果集类型,resultType="edu.wzm.mybatis.domain.Person"就表示将查询
结果封装成一个Person类的对象返回,Person类就是person表所对应的实体类。
-->

<insert id="insert" parameterType="Person">
insert into person(name, age) values(#{name}, #{age})
</insert>

<select id="getById" resultType="Person">
select *
from person
where id = #{id}
</select>

<select id="getAll" resultType="Person">
select * from person;
</select>

<update id="update" parameterType="edu.wzm.mybatis.domain.Person">
update person set age = #{age} where id = #{id}
</update>

<delete id="delete" parameterType="int">
delete from person where id = #{id}
</delete>
</mapper>


查看完整代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis