SpringMVC整合Mybatis

SpringMVC + springfox + MyBatis的系列文章,在此篇也基本可以定调了。故此这篇会给出完整代码。

1.完整jar包

----------------SpringMVC jar START--------------------
spring-aop-4.3.4.RELEASE.jar
spring-beans-4.3.4.RELEASE.jar
spring-context-4.3.4.RELEASE.jar
spring-core-4.3.4.RELEASE.jar
spring-expression-4.3.4.RELEASE.jar
spring-web-4.3.4.RELEASE.jar
spring-webmvc-4.3.4.RELEASE.jar
common-logging-1.0.4.jar
----------------SpringMVC整合springfox的jar START--------------------
classmate-1.3.3.jar
guava-19.0.jar
jackson-annotations-2.8.4.jar
jackson-core-2.8.7.jar
jackson-databind-2.8.7.jar
slf4j-api-1.7.24.jar
spring-plugin-core-1.2.0.RELEASE.jar
spring-plugin-metadata-1.2.0.RELEASE.jar
springfox-core-2.6.1.jar
springfox-schema-2.6.1.jar
springfox-spi-2.6.1.jar
springfox-spring-web-2.6.1.jar
springfox-swagger-common-2.6.1.jar
springfox-swagger-ui-2.6.1.jar
springfox-swagger2-2.6.1.jar
swagger-annotations-1.5.10.jar
swagger-models-1.5.10.jar
----------------SpringMVC整合MyBatis的jar START--------------------
mysql-connector-java-5.1.40-bin.jar
junit-4.12.jar
hamcreate-core-1.3.jar
hamcreate-library-1.3.jar
spring-jdbc-4.3.4.RELEASE.jar
spring-tx-4.3.4.RELEASE.jar
druid-1.0.27.jar
mybatis-3.4.2.jar
mybatis-spring-1.3.1.jar
 

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
		http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
		id="WebApp_ID" version="3.1">
  <display-name>bphss-sample</display-name>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 加载spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-config.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 解决post乱码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- springmvc的前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation不是必须的;
  	     如果不配置contextConfigLocation, 
  	  springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml"
     -->
    <init-param>
	  <param-name>contextConfigLocation</param-name>
	  <param-value>classpath:springmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3.springmvc-config.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:p="http://www.springframework.org/schema/p" 
  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-4.3.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  
  <!-- 开启springmvc注解 -->
  <mvc:annotation-driven />
  
  <!-- 自动扫描包 -->
  <context:component-scan base-package="com.chensan" />
  
  <!-- 注入swagger -->
  <bean class="com.chensan.config.Swagger2Config" />
  <!-- Enables swgger ui -->
  <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html" />
  <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**" />
  
  <!-- 
  <mvc:resources location="/WEB-INF/static/" mapping="/static/**"/>
   -->
   
  <!-- 配置视图解析器:如何把Handler方法返回值解析为实际的物理视图 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
  </bean>
</beans>

4.spring-config.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:p="http://www.springframework.org/schema/p" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  
  <!-- 引入配置文件 -->
  <bean id="propertyConfigurer"  
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location" value="classpath:db-config.properties" />  
  </bean>
  
  <!-- 数据源配置 -->
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
	init-method="init" destroy-method="close">
	<property name="driverClassName" value="${driver}" />
	<property name="url" value="${url}" />
	<property name="username" value="${username}" />
	<property name="password" value="${password}" />
	
	<!-- 配置初始化大小、最小、最大 -->
	<property name="initialSize" value="${initialSize}" />
	<property name="maxActive" value="${maxActive}" />
	<property name="minIdle" value="${minIdle}" />

	<!-- 配置获取连接等待超时的时间 -->
	<property name="maxWait" value="${maxWait}" />

	<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
	<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />

	<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
	<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />

	<property name="validationQuery" value="${validationQuery}" />
	<property name="testWhileIdle" value="${testWhileIdle}" />
	<property name="testOnBorrow" value="${testOnBorrow}" />
	<property name="testOnReturn" value="${testOnReturn}" />

	<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
	<property name="poolPreparedStatements" value="${poolPreparedStatements}" />
	<property name="maxPoolPreparedStatementPerConnectionSize"
	  value="${maxPoolPreparedStatementPerConnectionSize}" />
  </bean>
 
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<!-- 配置mybatis配置文件的位置 -->
	<property name="configLocation" value="classpath:mybatis-config.xml" />
	<!-- 配置扫描Mapper XML的位置 -->
	<property name="mapperLocations" value="classpath*:com/chensan/mybatis/**/*.xml" />
  </bean>

  <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg ref="sqlSessionFactory" />
  </bean>
</beans>

5.db-config.properties

## MySQL
driver=com.mysql.jdbc
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值