MyBatis的常見錯誤總結

把MyBatis的常見錯誤總結一下。。mysql

UserMapper:web

<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>
View Code

mybatisConfig:sql

<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>
View Code

jdbc.properties:mybatis

# =================================================
# 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=
View Code

1、mybatis的映射文件的命令空間與接口的全限定名不一致;app

2、mybatis的Mapper文件名字與接口文件名字不一致;ide

3、Bean文件名稱路徑不正確;url

4、Bean屬性名稱不匹配;spa

5、ResultMap 沒有定義 或者返回類型定義不正確;hibernate

6、語法錯誤,標籤順序寫錯。3d

相關文章
相關標籤/搜索