- 问题
单独使用注解报错
package com.dilidiliniang.pojo;
import org.apache.ibatis.type.Alias;
@Alias(value = "hello")
public class User {
private int id;
private String name;
private String 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;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
}
注意不使用扫描包的情况下
<?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="db.properties">
</properties>
<!-- 别名
第一种:直接起别名
第二种:扫描实体类的包,起别名,使用的小写
第三种:使用注解
-->
<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>
<!--每一个Mapper.xml 都需要在mybatis注册! -->
<mappers>
<mapper resource="com/dilidiliniang/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>
报错
- 解决办法
添加开启注解扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描注解的包,会自动扫描包及其所有子包下的注解-->
<context:component-scan base-package="com.dilidiliniang.pojo"></context:component-scan>
</beans>
- 第二种解决办法
添加扫描包
<!-- 别名
第一种:直接起别名
第二种:扫描实体类的包,起别名,使用的小写
第三种:使用注解
-->
<typeAliases>
<package name="com.dilidiliniang.pojo"/>
</typeAliases>