请问在两者整合期间我是那里出了问题呀
这是我的数据库内容
请问为什么出错呀!我不熟悉Mybatis和Spring的整合,如果您能讲解一下就更好了
最后这是整个项目结构
因为你没有加@Mapper注解或者在启动类哪儿加入@MapperScan注解,导致在dao层没有正常注入bean,你用@Autoware注解就获取不到没有注入的dao,所以会报错
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 关联数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 数据库连接池 c3p0-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}}"></property>
<property name="user" value="${username}}"></property>
<property name="password" value="${password}}"></property>
</bean>
<!-- sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:cn/kexing/dao/*.xml"></property>
</bean>
<!-- SqlSessionTemplate-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean class="cn.kexing.dao.BookMapperImpl" id="bookMapper">
<property name="sqlSession" ref="sqlSession"></property>
</bean>
</beans>
db.properties:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost://3306/ssmbuild?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true
username=root
password=123
你使用的不对吧