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

本文介绍如何通过Spring框架整合MyBatis实现持久层操作,包括配置POM.xml、WEB.xml、application-context.xml等文件,设置数据源、事务管理及DAO层操作,并通过测试类验证功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.配置POM.xml文件 ,配置项目所需jar
2.配置WEB.xml:

    配置spring监听器

1
2
3
4
5
6
7
8
    <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 -->
    

1
2
3
4
5
<context:component-scan base-package="cn.liu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
     
    <context:annotation-config/>

4. 创建jdbc.properties和jdbc.xml,并配置:
 

1
2
3
4
5
6
7
8
9
10
11
12
   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配置
 

1
2
3
4
5
6
7
8
   <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文件

1
2
3
4
5
6
7
8
9
10
11
    <!-- mybatis org.mybatis.spring.SqlSessionFactoryBean 配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:cn/liu/croe/dao/*.xml"/>
        <property name="typeAliasesPackage" value="cn.liu.croe.bean"></property>
    </bean>
     
    <!-- 扫包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.liu.croe.dao"/>
    </bean>

7.事务管理transation.xml:    

1
2
3
4
5
6
7
    <!-- spring 事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
     
    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

8.UserInfoDAO.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.liu.croe.dao.UserInfoDAO">
     
    <insert id="add" parameterType="UserInfo">
        insert into user_info (user_name,user_sex)
        values(#{userName},#{userSex})
    </insert>
     
</mapper>

9.事务管理注释:@Transactional



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.liu.croe.bean.UserInfo;
import cn.liu.croe.dao.UserInfoDAO;
import cn.liu.croe.service.IUserInfoService;

@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoDAO userdao;
    
    public void addUser(UserInfo user) {
        
        int i = userdao.add(user);
        System.out.println(i);
        
        throw new RuntimeException("运行时异常");
        
    }

}


10.测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package cn.shop.userinfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.liu.croe.bean.UserInfo;
import cn.liu.croe.service.IUserInfoService;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-context.xml")
public class UserTest {
    @Autowired
    private IUserInfoService service;
    @Test
    public void testAdd() {
        UserInfo user = new UserInfo();
        user.setUserName("秋香");
        user.setUserSex("女");
        service.addUser(user);
    }
}





目 录 第1章 引言 1 1.1 选题背景 1 1.2 网上购物系统的发展与现状 1 1.3系统实现的目标 2 1.4系统的开发意义 2 第2章 系统的开发技术及主要架构 3 2.1开发技术的选择 3 2.2系统的主要架构及开发模式 3 2.2.1基于B/S的体系结构 4 2.2.2 基于MVC的应用开发模型 4 第3章 系统的功能和流程 7 3.1网站的功能分析 7 3.2 系统流程分析 8 3.2.1前台购物流程 8 3.2.2注册功能流程 9 3.2.3商品搜索流程 10 3.2.4用户登录流程 10 3.2.5商品类别管理流程 11 3.2.6商品管理流程 11 3.2.7购物车流程 12 第4章 系统概要设计 13 4.1 系统数据库设计 13 4.1.1数据库的概念结构模型设计 13 4.1.2数据库的逻辑结构模型设计 15 4.2功能模块设计 17 第5章 网上购物系统详细设计与实现 19 5.1 数据库的连接 19 5.2前台各功能模块的实现 19 5.2.2 注册登陆模块的实现 19 5.2.3 个人信息维护的实现 23 5.2.4 商品搜索模块的实现 25 5.2.5 商品浏览模块的实现 26 5.2.6 购物车模块的实现 28 5.3 后台各功能模块的实现 30 5.3.1 管理员登录模块的实现 30 5.3.2 会员管理模块的实现 32 5.3.3 类别管理模块的实现 32 5.3.4 商品管理模块的实现 34 5.3.5 订单管理模块的实现 36 5.3.6 销量统计模块的实现 38 第6章 系统测试及难点分析 40 6.1系统运行环境的搭建 40 6.1.1 JAVA虚拟机的安装 40 6.1.2 Tomcat环境搭建 40 6.2 系统程序的安装和加载 41 6.2.1 文件的拷贝 41 6.2.2 数据库的连接 41 6.2.3 Myeclipse开发工具 41 6.3系统测试 42 6.3.1 系统关键部分测试分析 42 6.3.2 系统功能测试 42 6.3.3 测试总结 46 6.3.4 系统的性能分析 47 6.4 系统中所存在的问题及解决方案 47 6.4.1 系统的整体设计和规划中的问题 47 6.4.2 开发功能模块所遇到的难点 48 6.4.3系统中存在的问题及拟定的解决方法 48 结  论 50 致 谢 51 参考文献 52
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值