结构图大概这样
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管理,我们的项目不一样,所以你不要看我的,只需要看上面配置的数据库配置信息即可,当然如果不行就说明你缺少依赖,自己去下载自动导入进去
spring.devtools.restart.additional-paths=src/resources/mybatis/mapper
spring.freemarker.cache=false
# 配置日志输出级别
logging.level.root=info
#数据源 配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/zikao?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=1234
https://how2j.cn/k/springmvc/springmvc-springmvc/615.html 你看
问题解决了吗?
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
dd
你用jdbc连接的话,可以导入mysql-connector-java-5.1.6.jar、c3p0-0.9.5.2.jar然后配置数据库的连接池来配置自己的数据库信息,编写dao层的公用函数来使用数据库
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y