spring-mybatis常见问题

目录

此贴为自我复习使用

1.关于mapper标签问题

① 在mybatis.xml

问题分析

方法如下

2.其他代码详情

StudentDao.xml

applicationContext.xml


此贴为自我复习使用

1.关于mapper标签问题

① 在mybatis.xml

Caused by: org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com.leyouju.dao.StudentDao.xml

问题分析

是mapper路径报错导致程序不能正常执行

检查StudentDao.xml中的

<mapper namespace="com.leyouju.dao.StudentDao.xml">

无论是.还是/都不行,后查阅相关资料,解决问题

方法如下

在resources目录下的mybatis.xml文件中将mappers resource注释掉

<mappers>
        <!--<mapper resource="com.leyouju.dao.StudentDao.xml"/>-->
</mappers>

/**
    具体原因为mapper文件路径出错,注释之后成功运行

*/

其他代码详情

StudentDao.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.leyouju.dao.StudentDao.xml">
    <insert id="">
        insert into student values (#{name},#{email},#{age})
    </insert>

    <select id="selectStudents" resultType="com.leyouju.domain.Student">
        select id,name,email,age from student order by id desc
    </select>
</mapper>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--声明数据源DataSource,作用是连接数据库的-->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close" >
        <!--set输入给DruidDataSource提供连接数据库的信息-->
        <property name="url" value="jdbc:mysql://localhost:3306/ssm" /><!--setUrl()-->
        <property name="username" value="root" />
        <property name="password" value="******" />
        <property name="maxActive" value="20" />
    </bean>

    <!--声明的是mybatis中提供的SqlSessionFactorBean类,这个类创建SqlSessionFactory的-->
    <bean init-method="buildSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--set注入,把数据库连接池赋给了dataSource属性-->
        <property name="dataSource" ref="myDataSource" />
        <!--mybatis主配置文件
            configLocation属性是Resource类型,读取配置文件的
            他的赋值,使用value,指定文件的路径,使用classpath:表示文件的位置
        -->
        <property name="configLocation" value="classpath:mybatis.xml" />
    </bean>

</beans>

1.在<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />标签

问题描述:当有多个接口时,默认会自动加载,而我刚才碰到的问题

Wrong namespace. Expected 'com.leyouju.dao.StudentDao' but found 'com.leyouju.dao.OrderDao'.

问题分析

预期的com.leyouju.dao。 StudentDao',但发现'com.leyouju.dao.OrderDao'。 

方法如下

经仔细检查过后,发现OrderDao.xml中的:

<mapper namespace="com.leyouju.dao.StudentDao">

因复制粘贴过来忘记修改路径,导致报错

数据库插入汉字乱码问题

Java插入中文到数据库中文变成问号解决 - Sharpest - 博客园

 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring-MyBatis整合是一种常见的开发方式,其中spring-mybatis.xml文件是用来配置SpringMyBatis框架集成的配置文件。在这个文件中,我们会定义数据源、事务管理器、扫描Mapper接口等配置信息。同时,我们还需要将MyBatis的SqlSessionFactory注入到Spring容器中,以便其他组件可以使用它来执行数据库操作。以下是一个简单的spring-mybatis.xml文件示例: ```xml <?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.example.entity" /> <property name="mapperLocations" value="classpath*:com/example/mapper/*.xml" /> </bean> <!-- 配置Mapper扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启Spring的注解功能 --> <context:annotation-config /> <!-- 开启Spring的AOP功能 --> <aop:aspectj-autoproxy /> <!-- 开启Spring的事务管理功能 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans> ``` 在这个配置文件中,我们使用了Spring的注解功能、AOP功能和事务管理功能,以及MyBatis的Mapper扫描和SqlSessionFactory配置。其中,数据源、事务管理器和Mapper扫描的配置信息需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值