编程知识 cdmana.com

Detailed configuration of Spring + mybatis + spring MVC

spring Integrate mybatis Configuration method

1. Prepare the database configuration file jdbc.properties
 Insert picture description here
2. To configure applicationContext.xml


	<!--1. Load profile -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 2. Give the connection pool object to Spring To manage  -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

Here is spring Integrate mybatis Configuration of

<!-- 3. take SqlSessionFactory hand Spring To manage   -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
	<!-- Map all suffixes to Mapper.xml file -->
		<property name="mapperLocations" value="classpath:cn/mycode/mapper/*Mapper.xml"></property>
	</bean>
	
<!-- 4. take Mapper Give the interface to Spring To manage  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!--  Appoint mapper The package path of the interface  -->
		<property name="basePackage" value="cn.mycode.mapper"></property>
	</bean>

Here is service Layer scan annotation settings

	<!-- 5. Scan package path  -->
	<context:component-scan base-package="cn.mycode.service"></context:component-scan

Then put on spring-mvc Configuration of

. I won't let go of the binding file , The main configuration is as follows

<!-- 1. Static resource release  -->
	<mvc:default-servlet-handler />

	<!-- 2. Scan package path : Context component scan  -->
	<context:component-scan base-package="cn.mycode.controller"></context:component-scan>

	<!-- 3. Turn on Spring Yes Mvc Support for : Able to use @RequestMapping -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
<!-- 4. view resolver : Unified treatment 【webmvc】 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property><!--  Prefix  -->
		<property name="suffix" value=".jsp"></property><!--  suffix  -->
	</bean>

After the above configuration, it should be set in web Project use , You also need to set web.xml

1. take spring Profile application for , This is the file in the root path, so configuration files one and bin Catalog at the same level , This section is mainly controlled by the background .

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

2. This is about the configuration of the front end

<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

After the above operations ,spring+mybatis+spring-mvc The integration framework is complete . And filters are added as needed . Here's coding filtering

<filter>
    <filter-name>EncodingFilter</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>
  </filter>
  
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

版权声明
本文为[osc_ 31d5oo2i]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225095044693O.html

Scroll to Top