网上商城项目实战之spring mybatis整合

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31951571/article/details/49793943
1.配置POM.xml文件,配置项目所需jar(下载jar包,寻找依赖,热部署)
2.配置web.xml:
     配置spring监听器
    <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:application-context.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.创建 anotation.xml配置文件
<!-- spring扫描 @service -->
<context:component-scan base-package="cn.shop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 自动装配 -->
<context:annotation-config/>
4.创建 jdbc.properties和jdbc.xml,并配置:


driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8
user=root
password=




<!-- 配置c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}" />
<property name="password" value="${password}" />
</bean>

5.配置property.xml文件,读取JDBC配置
<!-- 读取jdbc配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- jdbc的配置 -->
<value>classpath:properties/jdbc.properties</value>
</list>
</property>
</bean>
6.配置mybatis.xml文件
<!-- mybatis org.mybatis.spring.SqlSessionFactoryBean 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/shop/croe/dao/*.xml"/>
<property name="typeAliasesPackage" value="cn.shop.croe.bean"></property>
</bean>

<!-- 扫包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.shop.croe.dao"/>
</bean>
7.事务管理transation.xml:
<!-- spring 事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
展开阅读全文

没有更多推荐了,返回首页