类型别名typeAliases
解决的问题
在之前的 mapper.xml配置文件时, parameterType与resultType如果是对象,需要是对象的 全限定名
冗余且易出错
<select id="getUserList" resultType="com.liu.pojo.User">
<!--resultType需要使用全限定名 集合使用内部泛型类-->
select * from mybatis.user
</select>
解决方案
类型别名(typeAliases)
==1.==类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。例如:
<typeAliases>
<typeAlias alias="Author" type="domain.blog.Author"/>
<typeAlias alias="Blog" type="domain.blog.Blog"/>
<typeAlias alias="Comment" type="domain.blog.Comment"/>
<typeAlias alias="Post" type="domain.blog.Post"/>
<typeAlias alias="Section" type="domain.blog.Section"/>
<typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases>
当这样配置时,Blog
可以用在任何使用 domain.blog.Blog
的地方。
==2.==也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean (例如pojo包),比如:
<typeAliases>
<package name="domain.blog"/>
</typeAliases>
==3.==利用注解设置别名(+包名)
每一个在包 domain.blog
中的 Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。 比如 domain.blog.Author
的别名为 author
;若有注解,则别名为其注解值。见下面的例子:
- mybatis-config.xml 设置 package
- 设置注解
@Alias("author")
public class Author {
...
}
下面是一些为常见的 Java 类型内建的类型别名。它们都是不区分大小写的,注意,为了应对原始类型的命名重复,采取了特殊的命名风格。
别名 | 映射的类型 |
---|---|
_byte | byte |
_long | long |
_short | short |
_int | int |
_integer | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
decimal | BigDecimal |
bigdecimal | BigDecimal |
object | Object |
map | Map |
hashmap | HashMap |
list | List |
arraylist | ArrayList |
collection | Collection |
iterator | Iterator |
直接别名设置
<configuration>
<!--引入外部配置文件-->
<properties resource="db.properties"></properties>
<typeAliases>
<typeAlias alias="User" type="com.liu.pojo.User"></typeAlias> <!--直接别名设置-->
</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="com/liu/dao/UserMapper.xml"/>
</mappers>
</configuration>
<!--根据id查询用户-->
<select id="getUserById" resultType="User" parameterType="int">//可以直接使用User
select * from mybatis.user where id = #{id}
</select>
包名别名设置
<configuration>
<!--引入外部配置文件-->
<properties resource="db.properties"></properties>
<typeAliases>
<!-- <typeAlias alias="User" type="com.liu.pojo.User"></typeAlias>-->
<package name="com.liu.pojo"/> <!--使用pojo包配置别名,MyBatis自动扫描pojo包下的pojo类-->
</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="com/liu/dao/UserMapper.xml"/>
</mappers>
</configuration>
<!--根据id查询用户-->
<select id="getUserById" resultType="User" parameterType="user">
//使用包名 , 对应类的首字母必须小写 user
select * from mybatis.user where id = #{id}
</select>
注解包名
-
mybatis-config.xml 中 设置 package
<typeAliases> <package name="com.liu.pojo"/> </typeAliases>
-
pojo类设置注解
@Alias("User") public class User { private int id ; private String name; private String password; public User(){}; public User(int id, String name, String password) { this.id = id; this.name = name; this.password = password; } }
-
mapper.xml修改配置
<!--根据id查询用户--> <select id="getUserById" resultType="User" parameterType="User">/*添加了参数类型*/ select * from mybatis.user where id = #{id} </select>
对象工厂(objectFactory)
每次 MyBatis 创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成实例化工作。 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认无参构造方法,要么通过存在的参数映射来调用带有参数的构造方法。 如果想覆盖对象工厂的默认行为,可以通过创建自己的对象工厂来实现。比如:
// ExampleObjectFactory.java
public class ExampleObjectFactory extends DefaultObjectFactory {
public Object create(Class type) {
return super.create(type);
}
public Object create(Class type, List<Class> constructorArgTypes, List<Object> constructorArgs) {
return super.create(type, constructorArgTypes, constructorArgs);
}
public void setProperties(Properties properties) {
super.setProperties(properties);
}
public <T> boolean isCollection(Class<T> type) {
return Collection.class.isAssignableFrom(type);
}}
<!-- mybatis-config.xml -->
<objectFactory type="org.mybatis.example.ExampleObjectFactory">
<property name="someProperty" value="100"/>
</objectFactory>
映射器(mappers)
既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要来定义 SQL 映射语句了。 但首先,我们需要告诉 MyBatis 到哪里去找到这些语句。 在自动查找资源方面,Java 并没有提供一个很好的解决方案,所以最好的办法是直接告诉 MyBatis 到哪里去找映射文件。 你可以使用相对于类路径的资源引用,或完全限定资源定位符(包括 file:///
形式的 URL),或类名和包名等。例如:
使用class绑定注册 、扫描包注册需要 :
- 接口与Mapper.xml配置文件必须同名
- 二者必须属于一个包
推荐使用 resource 查找
<!-- 使用相对于类路径的资源引用 -->
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
<mapper resource="org/mybatis/builder/BlogMapper.xml"/>
<mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
<!-- 使用完全限定资源定位符(URL) -->
<mappers>
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
<mapper url="file:///var/mappers/BlogMapper.xml"/>
<mapper url="file:///var/mappers/PostMapper.xml"/>
</mappers>
<!-- 使用映射器接口实现类的完全限定类名 -->
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
<mapper class="org.mybatis.builder.BlogMapper"/>
<mapper class="org.mybatis.builder.PostMapper"/>
</mappers>
<!-- 将包内的映射器接口实现全部注册为映射器 -->
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
这些配置会告诉 MyBatis 去哪里找映射文件,剩下的细节就应该是每个 SQL 映射文件了,也就是接下来我们要讨论的。
更加自动化的MyBatis解决方案 Mybatis-plus
https://mybatis.plus/