zoukankan      html  css  js  c++  java
  • spring+spring MVC+mybatis 框架搭建

    1.新建一个javaWeb工程Test,创建时记得勾选web.xml文件。

    2.导入需要的jar包,Mybatis所有的jar,spring所有的jar,mysql驱动包。

    这里mybatis和spring的jar包就不介绍了,看看其他的包:

    mysql驱动包:mysql-connector-java-5.1.35-bin.jar。

    数据库连接的两个包:commons-dbcp-1.2.jar,commons-pool.jar

    mybatis和spring的整合包:mybatis-spring-1.2.2.jar

    接下来是配置文件:

    web.xml

    <?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Test</display-name>

    <context-param> <!--全局范围内环境参数初始化-->
    <param-name>contextConfigLocation</param-name> <!--参数名称-->
    <param-value>classpath:mybatis-spring.xml</param-value> <!--参数取值-->
    </context-param>


    <!--以下配置的加载顺序:先 ServletContext >> context-param >> listener >> filter >> servlet >> spring-->

    <filter> <!-- 用来声明filter的相关设定,过滤器可以截取和修改一个Servlet或JSP页面的请求或从一个Servlet或JSP页面发出的响应-->
    <filter-name>encodingFilter</filter-name> <!--指定filter的名字-->
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--定义filter的类的名称-->
    <async-supported>true</async-supported> <!--设置是否启用异步支持-->
    <init-param><!--用来定义参数,若在Servlet可以使用下列方法来获得:String param_name=getServletContext().getInitParamter("param-name里面的值");-->
    <param-name>encoding</param-name> <!--参数名称-->
    <param-value>UTF-8</param-value> <!--参数值-->
    </init-param>
    </filter>
    <filter-mapping><!--用来定义filter所对应的URL-->
    <filter-name>encodingFilter</filter-name> <!--指定对应filter的名字-->
    <url-pattern>/*</url-pattern> <!--指定filter所对应的URL-->
    </filter-mapping>

    <!--例:spring监听器-->
    <listener> <!--用来设定Listener接口-->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--定义Listener的类名称-->
    </listener>
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <servlet> <!--用来声明一个servlet的数据 -->
    <servlet-name>Test</servlet-name> <!--指定servlet的名称-->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指定servlet的类名称,这里配置了前端控制器-->
    <init-param><!--用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数 -->
    <param-name>contextConfigLocation</param-name> <!--参数名称-->
    <param-value>classpath:spring-mvc.xml</param-value> <!--参数值-->
    </init-param>
    <load-on-startup>1</load-on-startup><!--当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.-->
    <async-supported>true</async-supported><!--设置是否启用异步支持-->
    </servlet>
    <servlet-mapping><!--用来定义servlet所对应的URL-->
    <servlet-name>Test</servlet-name> <!--指定servlet的名称-->
    <url-pattern>/</url-pattern> <!--指定servlet所对应的URL-->
    </servlet-mapping>

    <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>

    jdbc.properties文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/domain?useUnicode=true&amp;characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=li123456

    log4j.properties文件

    # Global logging configuration
    log4j.rootLogger=ERROR, stdout
    # MyBatis logging configuration...
    log4j.logger.org.mybatis.example.BlogMapper=TRACE
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m

    mybatis-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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.1.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.1.xsd">
    <!--1 自动扫描 将标注Spring注解的类自动转化Bean-->
    <context:component-scan base-package="com.test" />

    <!--2 加载数据资源属性文件 -->
    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="jdbcDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    </bean>

    <!-- 4 配置sessionfactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="jdbcDataSource" />
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperLocations" value="classpath:com/lihui/mapper/*.xml"></property>
    </bean>

    <!-- 5 装配dao接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.lihui.mapper" /> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- 6、声明式事务管理 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="jdbcDataSource" />
    </bean>
    </beans>

    spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.1.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.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 1、配置映射器与适配器 -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 3、自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="com.lihui.*"/>
    </beans>

    注意:文件中的很多路径和包名都要改成你自己工程中的。

    创建TestController.java

    package com.lihui.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;

    @Controller
    @RequestMapping(value="/controller")
    public class TestController {

    @RequestMapping(value="/get",method = RequestMethod.GET)
    @ResponseBody
    public void testController() {
    System.out.println("输出");
    }

    }

    每一步都是一个深刻的脚印
  • 相关阅读:
    一起学ORBSLAM2(10)ORBSLAM中的相似变换矩阵的计算
    一起学ORBSLAM2(9)ORBSLAM的PNP解决方案
    一起学ORBSLAM2(8)ORBSLAM的loopclosing
    一起学ORBSLAM2(7)ORBSLAM中的优化问题
    一起学ORBSLAM2(6)ORBSLAM中的特征匹配
    IntelliJ IDEA 2018.2.2远程调试Tomcat的配置方法
    window.location.href 跳转页面时传递参数并且在新页面接收参数
    form.loadRecord(record)后isDirty总是true的解决办法
    ExtJS让被遮盖的窗体显示在最前面以及解决Ext.MessageBox提示框被TabPanel覆盖的方法【转】
    extjs store中数据转换成json
  • 原文地址:https://www.cnblogs.com/chzlh/p/9252897.html
Copyright © 2011-2022 走看看