你的位置:首页 > Java教程

[Java教程]springmvc+mybatis整合


maven 依赖

 

<!-- springmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.3.2.RELEASE</version>
</dependency>

<!-- mybatis -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.1</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.0.BETA-1</version>
</dependency>


<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.3.0.RELEASE</version>
</dependency>

<!-- 数据库连接 -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.26</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.25</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.2</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>com.springsource.org.apache.commons.fileupload</artifactId>
  <version>1.2.1</version>
</dependency>

 

web.

 

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring-mybatis. </context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<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:spring-mvc.   </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

 

 

spring-mvc.

<beans 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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 注解扫描的范围,多个用逗号隔开 -->
<context:component-scan base-package="com.xxx">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

<mvc:annotation-driven />
<!-- 当在web. <mvc:default-servlet-handler />

<!-- 静态资源映射 -->
<mvc:resources mapping="/static/**" location="/static/" />


<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver"

p:order="1">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="contentType" value="text/html" />
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 控制器异常处理 -->
<bean id="exceptionHandlerExceptionResolver"
>
</bean>

<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver"
>
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>

 

spring-mybatis.

 

<?<beans xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="dataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/springtest" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>

<!-- <bean id="log-filter" > -->
<!-- <property name="statementExecutableSqlLogEnable" value="true"/> -->
<!-- </bean> -->


<!-- (事务管理) -->
<bean id="transactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" >
<property name="dataSource" ref="dataSource"/>
<!-- <property name="configLocation" value="classpath:/mybatis. <property name="mapperLocations" >
<value>classpath:com/xxx/**/mapper/*. <!-- mapper.<!-- <array> -->
<!-- <value></value> -->
<!-- <value></value> -->
<!-- </array> -->
</property>
</bean>

<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper 如果包有多个,次类也可配置多个 -->
<bean >
<property name="basePackage" value="com.xxx"/>
<property name="markerInterface" value="com.xxx.MybatisMapper"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/><!--当只需要调用一次dao-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/><!--当需要通过多个dao获取数据时-->
<tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>

<aop:config expose-proxy="true" proxy-target->
<!-- 只对业务逻辑层实施事务 有多个包可用 || 连接 -->
<aop:pointcut id="txPointcut" expression="execution(* com.xxx.*..service..*+.*(..))"/>
<aop:advisor id="txAdvisor" advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

</beans>