文章目录
MyBatis的typeAlias配置
类型别名, 为Java类型设置一个简短的名字
它只和XML
配置有关, 存在的意义是减少类完全限定名的冗余
mybatis-config.xml代码:
<!-- 类型别名 -->
<typeAliases>
<typeAlias type="com.gx.mybatis.hello.User" alias="User"/>
</typeAliases>
type
属性值为类的全限定名称
alias
属性值为类的别名
之间的UserMapper.xml代码:
<select id="getAll" resultType="com.gx.mybatis.hello.UserMapperr">
SELECT * FROM t_user
</select>
现在的UserMapper.xml代码:
<select id="getAll" resultType="User">
SELECT * FROM t_user
</select>
MyBatis的properties配置
之前代码存在的问题: 耦合性太高, 不利于维护
解决方法: 数据库连接信息与mybatis-config.xml
代码相分离
添加properties属性
之前的mybatis-config.xml代码:
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC" />
<!-- ****************************************** -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis-demo"/>
<property name="username" value="root"/>
<property name="password" value="1092568516"/>
</dataSource>
<!-- ****************************************** -->
</environment>
</environments>
解耦后的代码:
<!-- ****************************************** -->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis-demo"/>
<property name="username" value="root"/>
<property name="password" value="1092568516"/>
</properties>
<!-- ****************************************** -->
<environments default="dev">
<environment id="dev">
<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>
添加properties属性
上述问题依然没有解决. 可以将数据库连接信息配置在db.properties
文件中来降低耦合性
新增db.properties文件
db.properties代码:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis-demo
username=root
password=1092568516
mybatis-config.xml代码:
<!-- ****************************************** -->
<properties resource="db.properties" />
<!-- ****************************************** -->
<environments default="dev">
<environment id="dev">
<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的ResultMap配置
之前存在的问题
数据库表的列名与domain
属性名必须相同, 否则封装结果集对象时会出错
类比BeanHandler
//把结果集的每一行数据封装成一个对象
public class BeanHandler<T> implements IResultSetHandler<T>{
private Class<T> beanClass;
public BeanHandler (Class<T> beanClass) {
this.beanClass = beanClass;
}
public T handle (Resultset rs) throws Exception {
if(rs.next()){
T obj = beanClass. newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(beanclass, object.class);
PropertyDescriptor pds = beanInfo.getPropertyDescriptos;
for (PropertyDescriptor pd : pds) {
String name = pd.getName();//属性名称(列名)
Object value = rs.getobject(name);
//调用改对象的sette方法, 把列的值设置到对象中指定的同名的属性上去
pd.getWriteMethod().invoke(obj, value);
}
return obj;
}
return null;
}
}
解决列名与属性名不匹配的问题
Mapper
文件 使用ResultMap
元素
ResultMap元素: 结果集合对象的映射
id属性: 当前Mapper文件中ResultMap的唯一名称
type属性: 把结果集中的每一行数据封装为什么对象
子元素:
result元素: 匹配对象的哪一个属性对应表中的哪一个列
id元素: 功能和result元素一模一样. 如果是主键, 建议使用id元素, 能提升性能
UserMapper.xml代码:
<mapper namespace="com.gx.mybatis.hello.UserMapper">
<!-- ************************************************* -->
<resultMap id="BaseResultMap" type="User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="salary" property="salary"/>
</resultMap>
<!-- ************************************************* -->
<select id="get" parameterType="java.lang.Long" resultMap="BaseResultMap">
SELECT * FROM t_user WHERE id = #{id}
</select>
<select id="getAll" resultMap="BaseResultMap">
SELECT * FROM t_user
</select>
</mapper>
添加了resultMap
元素, resultSet
属性也变为resultMap