结构图大概这样
mysql数据库
项目建的姿势不对哦,你要建立maven项目,配置文件放在resources文件夹里面,前端文件夹不是web而是webapp,好了,划归正题
1.在resources新建一个配置文件,命名jdbc.properties,里面代码,配置应该都明白吧?不明白建议你不要做项目了,去看b站的视频教程吧
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/dangdang?useUnicode=true&characterEncoding=utf8
username=root
password=root
2.配置spring的配置文件,命名applicationContext.xml ,就是将mybatias交给了spring管理
代码如下
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- <!–引入小配置文件 读取jdbc.properties–>-->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 定义SqlSessionFactory,管理mybatis的数据库连接sqksession对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="entity"/>
<property name="mapperLocations">
<list>
<value>classpath:dao/*Dao.xml</value>
</list>
</property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"/>
</array>
</property>
</bean>
<property name="basePackage" value="dao"/>
</bean>
<bean id="cs" class="service.impl.BillsServiceImpl">
<property name="bd" ref="billsDao"/>
</bean>
<bean id="es" class="service.impl.TybillServiceImpl">
<property name="td" ref="tybillDao"/>
</bean>
<!--事务管理器-->
<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--把事务管理器注入到事务通知类-->
<tx:advice id="txAdvice" transaction-manager="tm">
<!--事务的策略,类中不同方法使用不同的事务策略-->
<tx:attributes>
<!--以select的方法,都是只读的方法,不用使用事务-->
<tx:method name="select*" read-only="true"/>
<!--除了select开头的方法,其余的方法都必须使用事务-->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置切入点和织入-->
<aop:config>
<aop:pointcut id="pc" expression="execution(* service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
你不需要看下面的动态代理bean标签,那是将业务类service创建的对象交给spring管理,我们的项目不一样,所以你不要看我的,只需要看上面配置的数据库配置信息即可,当然如果不行就说明你缺少依赖,自己去下载自动导入进去