SpringMVC 【配置文件】

~~~~~~~~~~~~~~配置文件结构~~~~~~~~~~~~~~~~~~~~~

SpringMVC 【配置文件】

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1.web.xml

SpringMVC 【配置文件】
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    
    <!-- DispatcherServlet是一个Servlet,所以可以配置多个DispatcherServlet -->
    <servlet>
        <servlet-name>main</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/main-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup><!-- 启动顺序,让这个Servlet随Servlet容器一起启动 -->
    </servlet>
    <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- 加了 SpringMVC Filter 之后,也只能处理post的数据乱码 -->
    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

    <!-- 默认首页 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
SpringMVC 【配置文件】

2.main-servlet.xml

SpringMVC 【配置文件】
<?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/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <!-- 自动扫描的报名 -->
    <context:component-scan base-package="com.vipshop" />
    
    <!-- 【视图解析器】,  根据【Controller】返回的字符串【映射】到相应的【jsp】 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 【加载】其他SpringMVC【子容器】 -->
    <import resource="classpath:spring/SpringMVC_jdbc.xml"/>
    
    <!-- 【@ResponseBody】ajax 请求返回乱码 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
           <property name="messageConverters">
               <list>
                   <ref bean="stringHttpMessageConverter" />
               </list>
           </property>
    </bean>
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
           <property name ="supportedMediaTypes">   
                 <list>   
                     <value>text/plain;charset=UTF-8</value>   
                 </list>   
           </property> 
    </bean>

</beans>
SpringMVC 【配置文件】

3.SpringMVC_jdbc.xml

SpringMVC 【配置文件】
<?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:p="http://www.springframework.org/schema/p"
    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-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
               ">

    <!-- 附加属性文件 -->
    <context:property-placeholder location="classpath:spring/jdbc.properties,
                                            classpath:spring/mapper.properties" />

    <!-- 数据连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" 
        p:driverClass="${app.jdbc.driverClassName}" 
        p:jdbcUrl="${app.jdbc.url}" 
        p:user="${app.jdbc.username}" 
        p:password="${app.jdbc.password}" 
        p:acquireIncrement="10" 
        p:idleConnectionTestPeriod="60" 
        p:maxPoolSize="100" 
        p:maxStatements="50" 
        p:minPoolSize="10" />

    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />

    <!-- sqlSessionFaction 配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:spring/MyBatis_config.xml" />
    </bean>

    <!-- 自动扫描mapper文件 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.persistence" />
    </bean>
    
</beans>
SpringMVC 【配置文件】

4.jdbc.properties

# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost/weibo
app.jdbc.username=root
app.jdbc.password=123456

5.Mybatis_config.xml

SpringMVC 【配置文件】
<?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.entity.User" alias="User"/>
        <typeAlias type="com.entity.Message" alias="Message"/>
    </typeAliases>    
    
</configuration>
SpringMVC 【配置文件】

 6.xxxMapper.xml

SpringMVC 【配置文件】
<?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="com.persistence.MessageMapper">

    <resultMap id="resultMessage" type="Message">
        <result property="id" column="id" />
        <result property="userid" column="userid" />
        <result property="content" column="content" />
        <result property="time" column="time" />
    </resultMap>

    <insert id="addMessage" parameterType="Message">
        INSERT INTO message
        (id,userid,content,time)
        VALUE (null,#{userid},#{content},#{time})
    </insert>

    <select id="findAll" resultMap="resultMessage">
        SELECT id,userid,content,time
        FROM message;
    </select>

    <select id="getById" parameterType="int" resultMap="resultMessage">
        SELECT id,userid,content,time
        FROM message where id = #{id};
    </select>

    <delete id="deleteById" parameterType="int">
        DELETE FROM
        message
        WHERE ID = #{id}
    </delete>

    <update id="update" parameterType="Message">
        UPDATE message
        <set>
            <if test="userid != null ">userid = #{userid},</if>
            <if test="content != null ">content = #{content},</if>
            <if test="time != null ">time = #{time}</if>
        </set>
        WHERE ID = #{id}
    </update>
    
    <select id="getOneByCondition" parameterType="Message" resultMap="resultMessage">
        select id,userid,content,time
        from message
        where 1=1 
        <if test="id != null and ‘‘ != id">
            and id = #{id}
        </if>
        
        <if test="userid != null and ‘‘ != userid">
            and userid = #{userid}
        </if>
        
        <if test="content != null and ‘‘ != content">
            and content = #{content}
        </if>
        
        <if test="time != null and ‘‘ != time">
            and time = #{time}
        </if>
    </select>

</mapper>
SpringMVC 【配置文件】

 

SpringMVC 【配置文件】,布布扣,bubuko.com

SpringMVC 【配置文件】

上一篇:QTP安装JAVA插件后,打开Eclipse报错“An error has occurred,see th log file”


下一篇:一步一步将Vim打造成C++超级IDE