SSM整合实现增删改查

       详细讲解一些怎样整合spring,springMVC和mybaties.

第一步:数据库文件

-------------------------创建数据库
--创建用户(以管理员身份登录数据库,创建新用户)
create user handson identified by 274039;

--为用户分配权限
grant create session,resource,dba to handson;

--创建表空间
create tablespace handson_data
datafile   'D:\oracleWorkSpace\handson_data.dbf' size 10m
autoextend on next 1m maxsize 50m
extent management local;

--给用户指定表空间
alter user handson  default tablespace handson_data;

--用新创建的用户登陆数据库(conn handson/274039)

------------------------创建表EMPLOYEE
create table EMPLOYEE(
  EMP_ID number(4) primary key,
  EMP_NAME varchar2(100) not null,
  EMP_SEX number(4) not null,
  EMP_AGE number(4) not null,
  EMP_DEPART varchar(50) not null
)
--添加自增
create sequence seq_EMPLOYEE
start with 1
increment by 1;
--创建触发器
create or replace trigger tr_EMPLOYEE
before insert on EMPLOYEE
for each row 
begin 
  select seq_EMPLOYEE.nextval into :new.EMP_ID from dual;
end;
--插入数据
insert into EMPLOYEE values(1,'李晓明',1,25,'行政部');
insert into EMPLOYEE values(1,'杨伟林',1,29,'行政部');
insert into  EMPLOYEE(EMP_NAME,EMP_SEX,EMP_AGE,EMP_DEPART) values('1',1,29,'行政部');
update EMPLOYEE set EMP_NAME=#{name},EMP_SEX=#{pwd},EMP_AGE=#{name},EMP_DEPART=#{pwd} where EMP_ID = #{id}
--查询
select * from EMPLOYEE
--drop
drop table EMPLOYEE
drop sequence seq_EMPLOYEE

第二步骤:导入相关jar包

aopalliance.jar
asm-3.3.1.jar
aspectjweaver.jar
cglib-2.2.2.jar
commons-dbcp.jar
commons-fileupload-1.3.2.jar
commons-io-2.2.jar
commons-logging-1.1.3.jar
commons-pool.jar
fastjson-1.2.3.jar
jackson-annotations-2.5.3.jar
jackson-core-2.5.3.jar
jackson-databind-2.5.3.jar
javassist-3.17.1-GA.jar
jsp-api.jar
jstl.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
ojdbc14.jar
servlet-api.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.3.2.RELEASE.jar
spring-aspects-4.3.2.RELEASE.jar
spring-beans-4.3.2.RELEASE.jar
spring-context-4.3.2.RELEASE.jar
spring-context-support-4.3.2.RELEASE.jar
spring-core-4.3.2.RELEASE.jar
spring-expression-4.3.2.RELEASE.jar
spring-jdbc-4.3.2.RELEASE.jar
spring-orm-4.3.2.RELEASE.jar
spring-tx-4.3.2.RELEASE.jar
spring-web-4.3.2.RELEASE.jar
spring-webmvc-4.3.2.RELEASE.jar
standard.jar
tomcat-api.jar

编写配置文件

web工程: web.xml
springmvc: mvc.xml
spring: application.xml
mybatis: mybatis.cfg.xml(可有可无,看怎样配置)

a:web.xml配置

配置spring和配置springmvc

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>handson</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 配置spring:读取spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- spring mvc配置文件 -->
    <!-- 分发器,核心控制器的配置 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--DispathcherServlet初始化参数 src下 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc.xml</param-value>
        </init-param>
        <!-- 容器启动servlet就启动 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- <url-pattern>*.do</url-pattern> -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

b:mvc.xml 使用注解

如果需要json或其他处理,需要填写相关配置

<?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:jee="http://www.springframework.org/schema/jee"
    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/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 用于将对象转换为JSON -->
    <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">       
    </bean>
        <!-- 适配器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringConverter"/>
                <ref bean="jsonConverter"/>
            </list>
        </property>
    </bean>

    <!-- 配置注解的扫描:扫描cn.sxt.controller下文件 -->
    <context:component-scan base-package="cn.sxt.controller"/>
</beans>

c:spring:applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置datasource数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>
        <property name="username" value="handson"/>
        <property name="password" value="274039"/>
    </bean> 

    <!-- 配置sqlSessionFactory 工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
    </bean>
    <!-- 配置声明式事务配置 开始 -->
    <!-- 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 -->
            <tx:method name="save" propagation="REQUIRED"/>
            <tx:method name="get" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
    <!-- 声明式事务配置 结束 -->
    <context:component-scan base-package="cn.sxt"/>
</beans>

d:mybatis.cfg.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>
    <typeAliases>
        <package name="cn.sxt.vo"/>
    </typeAliases>
    <mappers>
        <!-- 所有mapper.xml文件填写位置 -->
        <mapper resource="cn/sxt/vo/employee.mapper.xml"/>
    </mappers> 
</configuration>

编码

这一过程就不贴出具体代码了,感兴趣的可以去下载ssm整合源代码
编码结构图
这里写图片描述

效果图

这里写图片描述主页这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值