Configuration problems in SSM project

Keywords: Spring xml Mybatis encoding

When creating a spring + spring MVC + mybatis project, you first need to configure some corresponding configuration files. Here is a brief introduction of the simplest requirements for these configuration files.

1  Mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">


<!-- 1. data source : DriverManagerDataSource -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://localhost:3306/*"/>

<property name="username" value="*"/>

<property name="password" value="*"/>

</bean>


<!--

2. mybatis Of SqlSession Factory: SqlSessionFactoryBean

-->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

</bean>

<!--

3. mybatis Auto scan load Sql Mapping file : MapperScannerConfigurer

-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

<property name="basePackage" value="com.*.*.mapper" />

</bean>


<!-- 4. transaction management : DataSourceTransactionManager -->

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>


<!-- 5. Use declarative transactions -->

<tx:annotation-driven transaction-manager="txManager"/>


</beans>

2 springmvc-servlet.xml

This configuration file configures what the spring MVC framework requires. The parts with * are filled in by yourself

  1. <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    
    xmlns:context="http://www.springframework.org/schema/context"
    
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
    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">
    
    
    <!--Open notes-->
    
    <mvc:annotation-driven />
    
    <!-- Static resources are directly accessible -->
    
    <mvc:default-servlet-handler />
    
    
    <!-- Scan contains Controller Package with annotation -->
    
    <context:component-scan base-package="com.*.*.controller" />
    
    
    <!-- jsp View path configuration -->
    
    <bean id="jspViewResolver"
    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
    <property name="prefix" value="/WEB-INF/jsp/" />
    
    <property name="suffix" value=".jsp" />
    
    </bean>
    
    </beans>

     

3 applicationContext.xml

This is the configuration file required by Spring. Note here: the part with * is the project name, not specific to the package.

  1. <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    
    xmlns:context="http://www.springframework.org/schema/context"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
    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">
    
    
    <!-- Auto load, filter out@Controller Annotated classes -->
    
    <context:component-scan base-package="com.*.*">
    
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    
    </context:component-scan>
    
    <!--Import required file resources-->
    
    <import resource="MyBatis.xml" />
    
    
    </beans>

     

4 web.xml

  1. <?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"
    
    version="3.0">
    
    
    
    <listener>
    
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    
    </listener>
    
    
    <!-- Spring and Spring MVC -->
    
    <context-param>
    
    <param-name>contextConfigLocation</param-name>
    
    <param-value>classpath:applicationContext.xml</param-value>
    
    </context-param>
    
    
    <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:springmvc-servlet.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>

     

The listener must have one. If it is missing, an error will be reported. After that, you can also add filter and other content.

Posted by nut legend on Sat, 04 Jan 2020 06:53:37 -0800