使用SSM完成增删改查

随便写的

先上图

1.添加页面

                      

2.展示页面

3.修改页面

4.查询指定部门下的员工

先看目录结构创建对应的包

1.先导入该有的Jar包(建议用maven)

2.建表

employee表

department表

3配置文件

编写Spring.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:aop="http://www.springframework.org/schema/aop"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        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-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    <!-- 组件扫描 -->
    <context:component-scan base-package="com.yzw.ssm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 数据源 -->
    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    
    <!-- 事务 -->
    <bean id="dataSourceTransactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>    
    </bean>
    <!-- 使用声明式事务 -->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
    
    <!-- Spring整合Mybatis  -->
    <!-- a.SqlSession对象的创建及管理
           Mybatis: 基于全局配置文件==>SqlSessionFactory===>SqlSession
           SSM: SqlSessionFactoryBean
     -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- Mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 别名处理 -->
        <property name="typeAliasesPackage" value="com.yzw.ssm.bean"></property>
        <!-- SQL映射文件 -->
        <property name="mapperLocations" value="classpath:com/yzw/ssm/mapper/*.xml"></property>
        
    </bean>
    
    <!-- b.Mapper接口代理实现类对象的创建及管理
             MyBatis: session.getMapper(xxxMapper.class);
            SSM: MapperScannerConfigurer
                 EmployeeMapper ==> 代理实现类对象==>IOC==> employeeMapper
    -->
    <!-- 方式一 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yzw.ssm.mapper"></property>    
    </bean>
    <!-- 方式二 -->
    <!-- <mybatis-spring:scan base-package="com.yzw.ssm.mapper"/> -->

</beans>

编写springmvc.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        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-4.0.xsd">

    <!-- 组件扫描  -->
    <context:component-scan base-package="com.yzw.ssm" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- mvc配置 -->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    
    <!-- 配置上传组件
         注意: id必须指定成multipartResolver,因为springmvc会通过multipartResolver在容器中查找对应的bean对象.
      -->
     <bean id="multipartResolver"
         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <!-- 编码 要设置成与页面的编码一致 -->
         <property name="defaultEncoding" value="utf-8"></property>
         <!-- 设置文件放入临时文件夹的最小大小限制
         此为阈值,低于此值,则保存在内存中,如高于此值,则生成硬盘上的临时文件-->
         <property name="maxInMemorySize" value="1
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值