- MyBatis中别名存在的主要意义就是简化开发
- MyBatis中引用类有三种方式
- 直接使用实体类的完整包名
- 使用实体类的typeAlias别名
- 使用包名作为别名
使用完整包名
<select id="getUserById" parameterType="int"
resultType="com.mybatis.pojo.Users">
select
* from smbms_user where id=#{id}
</select>
使用实体类的 typeAlias 别名
- resource/mybatis-config.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>
…
<!-- 设置实体类别名 -->
<typeAliases>
<typeAlias type="com.mybatis.pojo.Users" alias="Users"/>
</typeAliases>
</configuration>
- UserMapper.xml
<?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.mybatis.dao.UsersMapper">
<select id="getUserById" parameterType="int"
resultType="Users">
select
* from smbms_user where id=#{id}
</select>
…
</mapper>
使用包名作为别名
- resource/mybatis-config.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>
<!-- 设置实体类别名 -->
<typeAliases>
<package name="com.mybatis.pojo.Users" />
</typeAliases>
</configuration>
- UserMapper.xml
<?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.mybatis.dao.UsersMapper">
<select id="getUserById" parameterType="int"
resultType="Users">
select
* from smbms_user where id=#{id}
</select>
…
</mapper>