ssm环境搭建(spring4.2.0、mybatis3.2.2)

本文详细介绍了Spring、Spring MVC及MyBatis三个框架在实际项目中的整合应用过程,包括配置文件解析、数据源设置、事务管理、注解驱动等关键环节,并展示了具体的XML配置示例。

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

app_base.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.2.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                            http://www.springframework.org/schema/mvc   
                            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">  

    <!-- 创建数据源 -->
    <bean id="data" class="org.apache.commons.dbcp2.BasicDataSource">
        <!-- 驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <!-- url -->
        <property name="url" value="jdbc:mysql://localhost:3306/j122?characterEncoding=utf-8&amp;allowMultiQueries=true&amp;useSSL=false"></property>
        <!-- 用户名 -->
        <property name="username" value="root"></property>
        <!-- 密码 -->
        <property name="password" value="99233"></property>
        <!-- 最大连接数 -->
        <property name="maxTotal" value="100"></property>
        <!-- 最小连接数 -->
        <property name="maxIdle" value="20"></property>
        <!-- 超时时间 -->
        <property name="maxWaitMillis" value="2000"></property>
    </bean>

    <!-- 创建会话工厂 -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引用数据源 -->
        <property name="dataSource" ref="data"></property>
        <!-- 配置myBatis的基本配置文件 -->
        <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
        <!-- 自动扫描指定包中的ORM映射文件 -->
        <property name="mapperLocations">
            <list>
                <value>classpath:orm/*.xml</value>
            </list>
        </property> 
    </bean>

    <!-- 配置会话 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg ref="sessionFactory"></constructor-arg>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="trans" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="data"></property>
    </bean>

    <!-- 允许使用注解配置事务 -->
    <tx:annotation-driven transaction-manager="trans"/> 

    <!-- 扫描指定包中的spring注解类 -->
    <context:component-scan base-package="com.lovo.service"></context:component-scan>
</beans>

app_mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.2.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                            http://www.springframework.org/schema/mvc   
                            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">  

    <mvc:annotation-driven conversion-service="typeConvert"></mvc:annotation-driven>
    <bean id="typeConvert" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <list>              
                    <bean class="com.lovo.util.DateConverter"></bean>
                </list>         
            </property>         
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
    <context:component-scan base-package="com.lovo.action"></context:component-scan>        
</beans>

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>
    <typeAlias type="com.lovo.bean.UserBean" alias="user"/>
    </typeAliases>
</configuration>  

orm

<?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="user">
    <!-- 添加语句块 -->
    <insert id="add" parameterType="user" >
        insert into t_user(userName,birthday) value(#{userName},#{birthday});
    </insert>

    <!-- 删除语句块 -->
    <insert id="del" parameterType="int">
        delete from t_user where id =#{id};
    </insert>

    <!-- 修改语句块-->
    <insert id="update">
        update t_user set birthday =#{birthday} where id =#{id};
    </insert>

    <!-- 按id查询语句块 -->
    <select id="findById" parameterType="int" resultType="user">
        select * from t_user where id =#{id};
    </select>

    <!-- 定义可以重复使用的Sql语句块 -->
    <sql id="itemsSql">
            <if test="userName != null and userName != '' ">
                and userName like '%${userName}%'
            </if>           
            <if test="startDate != null">
                <![CDATA[and birthday >= #{startDate}]]>
            </if>
            <if test="endDate != null">
                <![CDATA[and birthday <= #{endDate}]]>
            </if>
    </sql>

    <!-- 分页动态查询语句块 -->
    <select id="findByItem" resultType="user">  
            select * from t_user where 1=1
            <include refid="itemsSql"/>
            limit #{start},#{pageSize};
    </select>

    <!-- 分页动态查询总记录数语句块 -->
    <select id="findByItemCount" resultType="int">
        select count(*) from t_user where 1=1
        <include refid="itemsSql"/>
    </select>
</mapper>

web.xml

<?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">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:app_base.xml</param-value>
  </context-param>

  <listener>
  <!-- 上下文监听器,该监听器在容器启动时,读取contextConfigLocation全局参数信息,从而加载spring配置文件 -->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener>

  <servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:app_mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>


  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值