在实际项目开发中,我们经常性的设置返回对象.而设置的对象是使用全限定名,太长了 也比较麻烦
例如:
<?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.gzcgxt.erp.UserMapper"> <select id="select" resultType="com.gzcgxt.erp.hello.User"> # 就是这个 com.gzcgxt.erp.hello. select * from user where id = #{id} </select> </mapper>
解决办法 在mybatis全局配置文件中使用 别名方式
mybatis.xml
<typeAliases>
<package name="com.gzcgxt.erp.hello"/>
</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> <typeAliases> <package name="com.gzcgxt.erp.hello"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <!-- 这个是数据库操作的xml文件 --> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
UserMapper.xml 配置文件
resultType="User"
<?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.gzcgxt.erp.UserMapper"> <select id="select" resultType="User"> select * from user where id = #{id} </select> </mapper>
测试代码
原文地址:https://www.cnblogs.com/hua900822/p/11286825.html
时间: 2024-10-09 06:54:34