zoukankan      html  css  js  c++  java
  • 整合springmvc+spring+mybatis

    1 导入相关jar包

    aopalliance-1.0.jar
    asm-5.0.4.jar
    aspectjweaver.jar
    cglib-3.2.2.jar
    commons-fileupload-1.3.jar
    commons-io-2.2.jar
    commons-logging-1.2.jar
    jackson-annotations-2.7.0.jar
    jackson-core-2.7.7.jar
    jackson-databind-2.7.8.jar
    javassist-3.20.0-GA.jar
    log4j-1.2.17.jar
    log4j-api-2.3.jar
    log4j-core-2.3.jar
    mybatis-3.4.1.jar
    mybatis-spring-1.3.0.jar
    mysql-connector-java-5.1.7-bin.jar
    slf4j-api-1.7.21.jar
    slf4j-log4j12-1.7.21.jar
    spring-aop-4.3.3.RELEASE.jar
    spring-aspects-4.3.3.RELEASE.jar
    spring-beans-4.3.3.RELEASE.jar
    spring-context-4.3.3.RELEASE.jar
    spring-core-4.3.3.RELEASE.jar
    spring-expression-4.3.3.RELEASE.jar
    spring-jdbc-4.3.3.RELEASE.jar
    spring-tx-4.3.3.RELEASE.jar
    spring-web-4.3.3.RELEASE.jar
    spring-webmvc-4.3.3.RELEASE.jar

    2 编写配置文件

      web.xml 配置spring 配置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"
    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>SpringMVC_Spring_MyBatis</display-name>
    <!-- 加载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>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
    </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>

      mvc.xml --springmvc  使用注解 如果需要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:p="http://www.springframework.org/schema/p"
    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/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- configure the InternalResourceViewResolver -->
    <!-- 用于将对象转换为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>
    <context:component-scan base-package="com.sgcc"></context:component-scan>
    </beans>

      applicationContext.xml--spring

      

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    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.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">
    <!-- 读取属性文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:db.properties"></property>
    </bean>
    <!-- 配置datasource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driver}"></property>
    <property name="url" value="${url}"></property>
    <property name="username" value="${username}"></property>
    <property name="password" value="${password}"></property>
    </bean>
    <!-- 配置工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
    </bean>
    <!-- 配置声明式事务 -->
    <!-- 事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </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(* com.sgcc.service.impl.*.*(..))" id="pointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>

    <context:component-scan base-package="com.sgcc"></context:component-scan>

    </beans>

      mybatis.cfg.xml--mybatis

    <?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="com.sgcc"/>
        </typeAliases>
        <mappers>
            <!-- 所有mapper文件填写位置 -->
        </mappers>
    </configuration>

     编码

    package com.sgcc.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.sgcc.service.UserService;
    
    @Controller
    public class UserController {
    
        @Autowired
        private UserService userService;
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
        
        @RequestMapping("/list")
        public String list(ModelMap map){
            map.addAttribute("list", userService.list());
            return "/list.jsp";
        }
    }
  • 相关阅读:
    解决Python错误-----SSL: CERTIFICATE_VERIFY_FAILED
    SpringBoot性能优化
    解决WARN:No URLs will be polled as dynamic configuration sources.
    浅析如何解决终端输入长命令不换行覆盖(Docker容器内输入长命令折行覆盖)问题:如何设置docker容器tty终端窗口大小-Linux stty命令设置串口终端行列数
    shell中的传递参数$0 / $n、shell运算符(算术/关系/布尔/字符串/文件测试)、echo 命令输出字符串、printf 命令输出格式化的字符串、test 命令检查某条件是否成立
    【转】Grafna学习随记
    【转】使用InfluxDB的连续查询解决聚合性能问题
    【转】TDengine踩坑随记(最后一次更新:2021-4-7 20:30)
    【转】tdengine的更新功能,呼声最高的数据更新功能来了,用户需要什么,我们就开源什么
    【转】Go mod常用与高级操作
  • 原文地址:https://www.cnblogs.com/alloevil/p/6077578.html
Copyright © 2011-2022 走看看