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

    一、springmvc---controller  spring----service  mybatiss---dao

    pring(包括springmvc)、mybatis、mybatis-spring整合包、数据库驱动、第三方连接池。

    因为springmvc和spring属于同一个公司不需要整合包,而mybaits和spring需要整合包。

    二、jar包列表

    pom文件依赖:

      1  <dependencies>
      2     <dependency>
      3       <groupId>org.hamcrest</groupId>
      4       <artifactId>hamcrest-library</artifactId>
      5       <version>1.3</version>
      6     </dependency>
      7     <dependency>
      8       <groupId>org.springframework</groupId>
      9       <artifactId>spring-webmvc</artifactId>
     10       <version>4.3.12.RELEASE</version>
     11     </dependency>
     12     <dependency>
     13       <groupId>org.slf4j</groupId>
     14       <artifactId>slf4j-log4j12</artifactId>
     15       <version>1.7.5</version>
     16     </dependency>
     17     <dependency>
     18       <groupId>org.slf4j</groupId>
     19       <artifactId>slf4j-api</artifactId>
     20       <version>1.7.5</version>
     21     </dependency>
     22     <dependency>
     23       <groupId>org.apache.logging.log4j</groupId>
     24       <artifactId>log4j-core</artifactId>
     25       <version>2.0.2</version>
     26     </dependency>
     27     <dependency>
     28       <groupId>org.apache.logging.log4j</groupId>
     29       <artifactId>log4j-api</artifactId>
     30       <version>2.0.2</version>
     31     </dependency>
     32     <dependency>
     33       <groupId>spring-jms</groupId>
     34       <artifactId>spring-jms</artifactId>
     35       <version>4.2.4</version>
     36     </dependency>
     37     <dependency>
     38       <groupId>org.springframework</groupId>
     39       <artifactId>spring-web</artifactId>
     40       <version>4.3.12.RELEASE</version>
     41     </dependency>
     42     <dependency>
     43       <groupId>mysql</groupId>
     44       <artifactId>mysql-connector-java</artifactId>
     45       <version>5.1.34</version>
     46     </dependency>
     47     <dependency>
     48       <groupId>taglibs-standard</groupId>
     49       <artifactId>taglibs-standard</artifactId>
     50       <version>1.2.5</version>
     51     </dependency>
     52     <dependency>
     53       <groupId>taglibs-standard-spec</groupId>
     54       <artifactId>taglibs-standard-spec</artifactId>
     55       <version>1.2.5</version>
     56     </dependency>
     57     <dependency>
     58       <groupId>org.springframework</groupId>
     59       <artifactId>spring-oxm</artifactId>
     60       <version>4.1.5.RELEASE</version>
     61     </dependency>
     62     <dependency>
     63       <groupId>org.springframework</groupId>
     64       <artifactId>spring-test</artifactId>
     65       <version>4.3.12.RELEASE</version>
     66     </dependency>
     67 
     68     <dependency>
     69       <groupId>org.springframework</groupId>
     70       <artifactId>spring-expression</artifactId>
     71       <version>4.3.12.RELEASE</version>
     72     </dependency>
     73     <dependency>
     74       <groupId>org.springframework</groupId>
     75       <artifactId>spring-core</artifactId>
     76       <version>4.3.12.RELEASE</version>
     77     </dependency>
     78     <dependency>
     79       <groupId>org.springframework</groupId>
     80       <artifactId>spring-beans</artifactId>
     81       <version>4.3.12.RELEASE</version>
     82     </dependency>
     83     <dependency>
     84       <groupId>org.springframework</groupId>
     85       <artifactId>spring-context</artifactId>
     86       <version>4.3.12.RELEASE</version>
     87     </dependency>
     88     <dependency>
     89       <groupId>org.springframework</groupId>
     90       <artifactId>spring-context-support</artifactId>
     91       <version>4.3.12.RELEASE</version>
     92     </dependency>
     93     <dependency>
     94       <groupId>org.springframework</groupId>
     95       <artifactId>spring-aop</artifactId>
     96       <version>4.3.12.RELEASE</version>
     97     </dependency>
     98     <dependency>
     99       <groupId>aopalliance</groupId>
    100       <artifactId>aopalliance</artifactId>
    101       <version>1.0</version>
    102     </dependency>
    103     <dependency>
    104       <groupId>spring-aspects</groupId>
    105       <artifactId>spring-aspects</artifactId>
    106       <version>4.2.4</version>
    107     </dependency>
    108     <dependency>
    109       <groupId>com.springsource.org</groupId>
    110       <artifactId>aspectj.weaver</artifactId>
    111       <version>1.6.8</version>
    112     </dependency>
    113 
    114     <dependency>
    115       <groupId>org.apache.commons</groupId>
    116       <artifactId>commons-pool2</artifactId>
    117       <version>2.4.1</version>
    118     </dependency>
    119 
    120     <dependency>
    121       <groupId>org.apache.commons</groupId>
    122       <artifactId>commons-dbcp2</artifactId>
    123       <version>2.0.1</version>
    124     </dependency>
    125     <dependency>
    126       <groupId>org.springframework</groupId>
    127       <artifactId>spring-jdbc</artifactId>
    128       <version>4.1.5.RELEASE</version>
    129     </dependency>
    130     <dependency>
    131       <groupId>log4j</groupId>
    132       <artifactId>log4j</artifactId>
    133       <version>1.2.17</version>
    134     </dependency>
    135     <dependency>
    136       <groupId>org.javassist</groupId>
    137       <artifactId>javassist</artifactId>
    138       <version>3.16.1-GA</version>
    139     </dependency>
    140     <dependency>
    141       <groupId>org.mybatis</groupId>
    142       <artifactId>mybatis-spring</artifactId>
    143       <version>1.2.2</version>
    144     </dependency>
    145     <dependency>
    146       <groupId>org.springframework</groupId>
    147       <artifactId>spring-tx</artifactId>
    148       <version>4.1.5.RELEASE</version>
    149     </dependency>
    150     <dependency>
    151       <groupId>commons-logging</groupId>
    152       <artifactId>commons-logging</artifactId>
    153       <version>1.1.1</version>
    154     </dependency>
    155     <dependency>
    156       <groupId>cglib</groupId>
    157       <artifactId>cglib</artifactId>
    158       <version>2.2.2</version>
    159     </dependency>
    160     <dependency>
    161       <groupId>asm</groupId>
    162       <artifactId>asm</artifactId>
    163       <version>3.3.1</version>
    164     </dependency>
    165     <dependency>
    166       <groupId>org.mybatis</groupId>
    167       <artifactId>mybatis</artifactId>
    168       <version>3.2.7</version>
    169     </dependency>
    170     <dependency>
    171       <groupId>junit</groupId>
    172       <artifactId>junit</artifactId>
    173       <version>4.11</version>
    174       <scope>test</scope>
    175     </dependency>
    176     <dependency>
    177       <groupId>org.junit.jupiter</groupId>
    178       <artifactId>junit-jupiter-api</artifactId>
    179       <version>RELEASE</version>
    180     </dependency>
    181     <dependency>
    182       <groupId>org.springframework</groupId>
    183       <artifactId>spring-test</artifactId>
    184       <version>4.3.12.RELEASE</version>
    185     </dependency>

    三、配置文件

    1、SqlMapConfig.xml: mybaits配置文件,只保存约束和configuration标签即可,其中的mapper映射在applicationContex-dao.xml里进行添加。

    1 <?xml version="1.0" encoding="UTF-8" ?>
    2 <!DOCTYPE configuration
    3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5 
    6 
    7 <configuration>
    8 </configuration>

     2、applicationContext-dao.xml:dao层配置文件 (注解扫描在其他文件配置即可)

    1)数据库连接池

    2)会话工厂

    3)配置mapper扫描

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:context="http://www.springframework.org/schema/context"
     4         xmlns:p="http://www.springframework.org/schema/p"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"
     7         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     8         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    11         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    12 
    13     <!-- 加载配置文件 -->
    14     <context:property-placeholder location="classpath:db.properties"/>
    15     <!-- 数据库连接池 -->
    16     <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    17     <property name="driverClassName" value="${jdbc.driver}"/>
    18     <property name="url" value="${jdbc.url}"/>
    19     <property name="username" value="${jdbc.username}"/>
    20     <property name="password" value="${jdbc.password}"/>
    21     <property name="maxTotal" value="10"/>
    22     <property name="maxIdle" value="5"/>
    23     </bean>
    24         <!-- mapper配置 -->
    25         <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    26     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    27         <!-- 数据库连接池 -->
    28         <property name="dataSource" ref="dataSource"/>
    29         <!-- 加载mybatis的全局配置文件 -->
    30         <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
    31         </bean>
    32         <!-- 配置Mapper扫描器 -->
    33         <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    34             <property name="basePackage" value="jd.com.dao"/>
    35         </bean>
    36         </beans>

    3、service层

    1)applicationContext-service.xml 包扫描器和Service层的注解开启。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:context="http://www.springframework.org/schema/context"
     4         xmlns:p="http://www.springframework.org/schema/p"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"
     7         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     8         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    11         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    12         <!--开启注解扫描-->
    13         <context:component-scan base-package="jd.com" />
    14 
    15 </beans>

    2)applicationContext-trans.xml事务配置文件。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:context="http://www.springframework.org/schema/context"
     4         xmlns:p="http://www.springframework.org/schema/p"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"
     7         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     8         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    11         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    12 
    13     <!-- 事务管理器 -->
    14         <bean id="transactionManager"
    15             class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    16             <!-- 数据源 -->
    17         <property name="dataSource" ref="dataSource"/>
    18         </bean>
    19                 <!-- 通知 -->
    20         <tx:advice  id="txAdvice" transaction-manager="transactionManager">
    21         <tx:attributes>
    22         <!-- 传播行为 -->
    23         <tx:method name="save*" propagation="REQUIRED"/>
    24         <tx:method name="insert*" propagation="REQUIRED"/>
    25         <tx:method name="delete*" propagation="REQUIRED"/>
    26         <tx:method name="update*" propagation="REQUIRED"/>
    27         <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
    28         <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    29         </tx:attributes>
    30                 </tx:advice>
    31                 <!-- 切面 -->
    32         <aop:config>
    33         <aop:advisor advice-ref="txAdvice"
    34         pointcut="execution(* jd.com.service.*.*(..))"/>
    35         </aop:config>
    36 
    37         </beans>

    4、Springmvc.xml

    1)注解扫描器开启

    2)注解驱动(处理器映射器和处理器适配器)

    3)视图解析器配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:p="http://www.springframework.org/schema/p"
     5         xmlns:context="http://www.springframework.org/schema/context"
     6         xmlns:mvc="http://www.springframework.org/schema/mvc"
     7         xsi:schemaLocation="http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     9         http://www.springframework.org/schema/mvc
    10         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    11         http://www.springframework.org/schema/context
    12         http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    13         <!--开启注解扫描-->
    14         <context:component-scan base-package="jd.com.contronller"/>
    15         <!--开启注解驱动-->
    16         <mvc:annotation-driven/>
    17         <!--视图解析器配置-->
    18         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  >
    19             <property name="prefix" value="WEB-INF/jsp/"/>
    20             <property name="suffix" value=".jsp" />
    21             <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    22         </bean>
    23 
    24 
    25 </beans>

    注意上面配置的视图解析器有问题。前缀必须要/否则在配置窄化请求映射的时候会报如下错误:

    正确配置:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:p="http://www.springframework.org/schema/p"
     5         xmlns:context="http://www.springframework.org/schema/context"
     6         xmlns:mvc="http://www.springframework.org/schema/mvc"
     7         xsi:schemaLocation="http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     9         http://www.springframework.org/schema/mvc
    10         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    11         http://www.springframework.org/schema/context
    12         http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    13         <!--开启注解扫描-->
    14         <context:component-scan base-package="jd.com.contronller"/>
    15         <!--开启注解驱动-->
    16         <mvc:annotation-driven conversion-service="conversionService"  />
    17         <!--视图解析器配置-->
    18         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  >
    19             <property name="prefix" value="/WEB-INF/jsp/"/>
    20             <property name="suffix" value=".jsp" />
    21             <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    22         </bean>
    23         <mvc:resources mapping="/resource/**" location="/resources/"/>
    24 
    25         <!--自定义转换器 注意在注解驱动里指明引用-->
    26     <bean id="conversionService"
    27     class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    28     <property name="converters">
    29     <set>
    30         <bean class="jd.com.contronller.jd.com.convert.CustomStringToDate"/>
    31     </set>
    32         </property>
    33         </bean>
    34 
    35         </beans>

    5、web.xml 前端控制器配置。

     1 <!DOCTYPE web-app PUBLIC
     2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 
     5 <web-app>
     6 
     7 
     8   <!-- 加载spring容器 -->
     9   <context-param>
    10     <param-name>contextConfigLocation</param-name>
    11     <param-value>classpath:applicationContext-*.xml</param-value>
    12   </context-param>
    13   <listener>
    14     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    15   </listener>
    16   <servlet>
    17     <servlet-name>springmvc</servlet-name>
    18     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    19     <init-param>
    20       <param-name>contextConfigLocation</param-name>
    21       <!--使用通配符加载所有的配置文件-->
    22       <param-value>classpath:springmvc.xml</param-value>
    23     </init-param>
    24     <load-on-startup>1</load-on-startup>
    25   </servlet>
    26   <servlet-mapping>
    27     <servlet-name>springmvc</servlet-name>
    28     <url-pattern>*.action</url-pattern>
    29   </servlet-mapping>
    30 </web-app>

    建表和插入数据

    1         CREATE TABLE goods(
    2             id INT  AUTO_INCREMENT,
    3             goodsname VARCHAR(20),
    4             money DOUBLE,
    5             detail VARCHAR(100),
    6             PRIMARY KEY(id)
    7         );

    mapper文件:

    接口:

    1 package jd.com.dao;
    2 
    3 import java.util.List;
    4 import jd.com.projo.goods;
    5 
    6 public interface goodsMapperImpl {
    7     List<goods>  findGoodsById();
    8 }
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper
     3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 <!--和主配置文件约束是不一样的-->
     6 
     7 <mapper namespace="jd.com.dao.goodsMapperImpl">
     8     <select id="findGoodsById" resultType="jd.com.projo.goods"  >
     9         select * from goods
    10     </select>
    11 </mapper>
  • 相关阅读:
    数据访问类
    批量删除与查询
    CRUD
    数据访问与全局变量
    设计模式
    加载类
    PDO数据访问抽象层(上)
    PDO数据访问抽象层(下)
    会话控制
    php租房题目
  • 原文地址:https://www.cnblogs.com/evilliu/p/8974125.html
Copyright © 2011-2022 走看看