把MyBatis的常见错误总结一下。。
UserMapper:
<mapper namespace="com.ydweb.data.dao.UserMapper"> <resultMap type="com.ydweb.data.model.UserBean" id="UserBean"> <result property="id" column="id" /> <result property="username" column="username" /> <result property="password" column="password" /> </resultMap> <!-- 根据id查询得到一个user对象 <select id="getUser" resultMap="UserBean" parameterType="int" > --> <select id="getUser" resultType="com.ydweb.data.model.UserBean" parameterType="int" > select * from Users where id=#{id} </select> </mapper>
mybatisConfig:
<configuration> <!-- 引入外部配置文件 --> <properties resource="jdbc.properties"></properties> <typeAliases> <typeAlias alias="UserBean" type="com.ydweb.data.model.UserBean" /> </typeAliases> <!-- 配置mybatis运行环境 --> <environments default="development"> <environment id="development"> <!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 --> <transactionManager type="JDBC" /> <!-- mybatis提供了3种数据源类型,分别是:POOLED,UNPOOLED,JNDI --> <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> <!-- 告知映射文件方式1,一个一个的配置--> <mapper resource="com/ydweb/sqlmapper/UserMapper.xml"/> <!-- 自动扫描包内的Mapper接口与配置文件 <package name="com.ydweb.sqlmapper"/>--> </mappers> </configuration>
jdbc.properties:
# =================================================
# the configurations and credentials for H2 DEV DB
# =================================================
#jdbc.driverClassName=org.h2.Driver
#jdbc.url=jdbc:h2:mem:test;MODE=Oracle;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
#jdbc.username=sa
#jdbc.password=
#jdbc.schemaScript=h2sql/plannerportal-h2-schema.sql
#jdbc.dataloadingScript=h2sql/plannerportal-h2-dataloading.sql
#jdbc.hibernate.dialect=org.hibernate.dialect.H2Dialect
# ====================================================
# the configurations and credentials for MySql STG DB
# ====================================================
jdbc.driver=com.mysql.jdbc.Driver
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ydportal?useUnicode=true
jdbc.username=root
jdbc.password=
一、mybatis的映射文件的命令空间与接口的全限定名不一致;
二、mybatis的Mapper文件名字与接口文件名字不一致;
三、Bean文件名称路径不正确;
四、Bean属性名称不匹配;
五、ResultMap 没有定义 或者返回类型定义不正确;
六、语法错误,标签顺序写错。