ssm 到spring boot 校园商铺 day3 配置ssm

SSM各项配置

Jdbc.properties:

 

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC

jdbc.username=root

jdbc.password=Qq123456

 

mybatis-config.xml

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

 

<configuration>

 

    <settings>

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->

        <setting name="mapUnderscoreToCamelCase" value="true" />

   

        <!-- 使用列别名替换列名 默认:true -->

        <setting name="useColumnLabel" value="true" />

   

        <!-- 使用jdbcgetGeneratedKeys获取数据库自增主键值 -->

        <setting name="useGeneratedKeys" value="true" />

    </settings> 

 

</configuration>

 

 

 

 

spring-dao.xml

 

<?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"  

    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">  

<!-- 1.配置数据库相关参数properties的属性:${url} -->

<context:property-placeholder location="classpath:jdbc.properties"/>

 

<!-- 2.数据库连接池 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

    <!-- 配置连接池属性 -->

    <property name="driverClass" value="${jdbc.driver}" />

    <property name="jdbcUrl" value="${jdbc.url}" />

    <property name="user" value="${jdbc.username}" />

    <property name="password" value="${jdbc.password}" />

 

    <!-- c3p0连接池的私有属性 -->

    <property name="maxPoolSize" value="30" />

    <property name="minPoolSize" value="10" />

    <!-- 关闭连接后不自动commit -->

    <property name="autoCommitOnClose" value="false" />

    <!-- 获取连接超时时间 -->

    <property name="checkoutTimeout" value="10000" />

    <!-- 当获取连接失败重试次数 -->

    <property name="acquireRetryAttempts" value="2" />

</bean>

 

<!-- 3.配置SqlSessionFactory对象 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <!-- 注入数据库连接池 -->

    <property name="dataSource" ref="dataSource" />

    <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->

    <property name="configLocation" value="classpath:mybatis-config.xml" />

    <!-- 扫描entity 使用别名 -->

    <property name="typeAliasesPackage" value="com.gongzhuyou.entity" />

    <!-- 扫描sql配置文件:mapper需要的xml文件 -->

    <property name="mapperLocations" value="classpath:mapper/*.xml" />

</bean>

 

<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <!-- 注入sqlSessionFactory -->

    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

    <!-- 给出需要扫描Dao接口包 -->

    <property name="basePackage" value="com.gongzhuyou.dao" />

</bean> 

 

</beans> 

 

 

spring-service.xml

<?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"  

    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/tx  

    http://www.springframework.org/schema/tx/spring-tx.xsd">  

    

    

    <!--1 扫描service包下所有使用注解的类型 -->

    <context:component-scan base-package="com.gongzhuyou.service" />

    <!-- 2 配置事务管理器 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <!-- 注入数据库连接池 -->

        <property name="dataSource" ref="dataSource" />

    </bean>

   

    <!-- 3、配置基于注解的声明式事务 -->

    <tx:annotation-driven transaction-manager="transactionManager" /> 

</beans>  

 

 

spring-web.xml

<?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:mvc="http://www.springframework.org/schema/mvc"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

 

        <!-- 两个标准配置 -->

        <!-- 1、启用注解驱动-->  

        <mvc:annotation-driven/>

        <!-- 2 可以正常访问静态文件,springmvc静态资源处理-->

        <mvc:resources location="/resources/**" mapping="/resources/"></mvc:resources>

        <mvc:default-servlet-handler/>

       

        <!-- 3.定义视图解析器 -->

        <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">

            <property name="prefix" value="/WEB-INF/html/"></property>

            <property name="suffix" value=".html"></property>

        </bean>

        <!-- 4.扫描web相关的bean -->

        <context:component-scan base-package="com.gongzhuyou.web" /> 

       

       

</beans>

 

 

 

 

 

web.xml 

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

  version="3.1"

  metadata-complete="true">

  

    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

   

    <servlet>

    <servlet-name>spring-dispatcher</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!-- 配置springMVC需要加载的配置文件 spring-dao.xml,spring-service.xml,spring-web.xml 

        Mybatis - > spring -> springmvc -->

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring/spring-*.xml</param-value>

    </init-param>

</servlet>

<servlet-mapping>

    <servlet-name>spring-dispatcher</servlet-name>

    <!-- 默认匹配所有的请求 -->

    <url-pattern>/</url-pattern>

</servlet-mapping>  

   

</web-app>

 

 

验证:

 

问题:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.mybatis.spring.SqlSessionFactoryBean] for bean with name 'sqlSessionFactory' defined in class path resource [spring/spring-dao.xml]; nested exception is java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean

 

 

mybatis 中少了mybatis-spring

解决方法:pomXml中添加

    <dependency>

        <groupId>org.mybatis</groupId>

        <artifactId>mybatis-spring</artifactId>

        <version>1.3.2</version>

    </dependency>

 

 

 

问题:

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@398ba8ce -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (2). Last acquisition attempt exception: 

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

 

Mybatis版本过高与pom文件中版本不符

解决方法:pom中写入对应版本信息:

我的mysql版本:

 

pom文件:   

<dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

        <version>8.0.13</version> 

    </dependency>

   

BaseTest

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration({ "classpath:spring/spring-dao.xml"})

public class BaseTest {

 

 

测试类

public class AreaTest extends BaseTest{

 

       @Autowired

       AreaDao areaDao;

      

       @Test

       public void test1106() {

              List<Area> listArea = areaDao.listArea();

              System.out.println(listArea.get(0).getAreaName());

       }

      

}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值