weixin_45992860 2021-04-21 18:03 采纳率: 80%
浏览 139
已采纳

SpringMVC怎么连接数据库,先暂时做一个登陆界面测试一下数据库连接,求大佬给个详细解决方案

结构图大概这样

mysql数据库

  • 写回答

9条回答 默认 最新

  • 酷爱码 2021-04-21 19:22
    关注

    项目建的姿势不对哦,你要建立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">
    <!--    &lt;!&ndash;引入小配置文件 读取jdbc.properties&ndash;&gt;-->
        <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管理,我们的项目不一样,所以你不要看我的,只需要看上面配置的数据库配置信息即可,当然如果不行就说明你缺少依赖,自己去下载自动导入进去

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题