【Mybatis学习笔记3】配置文件别名的3种方法
正常情况下,我们UserMapper.xml每个select接口的resultType都会写具体的实体类名,但如下图样式会很冗余。
<?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.coke.dao.UserMapper"> <select id="getUserList" resultType="com.coke.pojo.User"> select * from mybatis.user </select> </mapper>
我们可以通过以下几种方法,简化操作!
方法一、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.coke.pojo.User" alias="User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&serverTimezone=Asia/Shanghai&allowMultiQueries=TRUE" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/coke/dao/UserMapper.xml" /> </mappers> </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.coke.dao.UserMapper"> <select id="getUserList" resultType="User"> select * from mybatis.user </select> </mapper>
方法二、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.coke.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&serverTimezone=Asia/Shanghai&allowMultiQueries=TRUE" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/coke/dao/UserMapper.xml" /> </mappers> </configuration>
mybatis会自动扫描该包下的实体类,UserMapper.xml里的resultType以方法名的小写命名即可(大写也行)
<?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.coke.dao.UserMapper"> <select id="getUserList" resultType="User"> select * from mybatis.user </select> </mapper>
方法三、实体类上加注解
package com.coke.pojo; // 实体类 import org.apache.ibatis.type.Alias; @Alias(value = "mybatis") public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
<?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.coke.dao.UserMapper"> <select id="getUserList" resultType="mybatis"> select * from mybatis.user </select> </mapper>
---------------------------------------------------
立足软件测试领域,并重新定义测试!
---------------------------------------------------