MyBatis-09 全局配置typeAliases


在全局配置文件中配置类型别名

<?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>
  <properties resource="com/cd/db.properties"/>

  <typeAliases>
    <typeAlias type="com.cd.Student" alias="Student"/>
    <typeAlias type="com.cd.Customer" alias="Customer"/>
    <typeAlias type="com.cd.Order" alias="Order"/>
    <package name="com.cd"/>
  </typeAliases>

  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
      </dataSource>
    </environment>
  </environments>


  <mappers>
    <mapper resource="com/cd/StudentMapper.xml"/>
  </mappers>
</configuration>

在映射文件中就可以使用别名

<?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.cd.StudentMapper">

  <select id="selectStudent" resultType="Student">
    select * from stu_tbl where id = #{id}
  </select>

  <insert id="insertStudent" parameterType="Student"  useGeneratedKeys="true">
    insert into stu_tbl(name,age) values(#{name},#{age})
  </insert>

  <delete id="deleteStudent">
   delete from stu_tbl where id=#{id}
  </delete>

  <update id="updateStudent" parameterType="Student">
   update stu_tbl set name=#{name}, age=#{age} where id=#{id}
  </update>

</mapper>


原文链接:http://codingdict.com/