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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记