Spring + SpringMVC + Mybaties integration-configuration file configuration and content explanation-can be directly copied for use-occasionally changed, springmvcmybaties
The following configurations can be used directly. You only need to change the package name.
The description and usage of internal labels are described in the code in the form of annotations. Personal originality. The source must be indicated for reprinting.
1. web. xml. After adding a jar package, you first need to configure the web. xml file under the WEB-INF
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3 xmlns = "http://java.sun.com/xml/ns/javaee" 4 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id = "WebApp_ID" version = "3.0"> 6 <display-name> CRM </display-name> 7 <welcome-file-list> 8 <welcome-file> login. jsp </welcome-file> 9 </welcome-file-list> 10 11 <! -- The following configuration can be obtained in the spring document by searching for web-app (excluding Chinese encoder) --> 12 13 <! -- Spring configuration file, which is placed under the WEB-INF by default. Put it in src, so you need to configure the following --> 14 <context-param> 15 <param-name> contextConfigLocation </param-name> 16 <param-value> classPath: appliationContext. xml </param-value> 17 </context-param> 18 19 <! -- Load the spring configuration file when starting the web --> 20 <listener> 21 <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> 22 </listener> 23 24 <! -- Added springMVC support. When springMVC is used separately, it must be stored on the web. in the xml file, configure --> 25 <servlet> 26 <servlet-name> dispatcher </servlet-name> 27 <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> 28 <init-param> 29 <param-name> contextConfigLocation </param-name> 30 <param-value> classPath: spring-mvc.xml </param-value> 31 </init-param> 32 <! -- Load the springmvc servlet when starting the web, load the springmvc configuration file --> 33 <load-on-startup> 1 </load-on-startup> 34 <! -- It can be executed asynchronously. It is best to match them all. The project will be smoother --> 35 <! -- If an error is reported during asynchronous configuration, because it is a new feature of 3.0, you can change 2.5 to 3.0 --> 36 <async-supported> true </async-supported> 37 </servlet> 38 <servlet-mapping> 39 <servlet-name> dispatcher </servlet-name> 40 <! -- Intercept all. for requests ending with do, the suffix of subsequent business code requests will be. do end (personal habits) --> 41 <url-pattern> *. do </url-pattern> 42 </servlet-mapping> 43 44 <! -- Encoder to prevent Chinese garbled characters --> 45 <filter> 46 <filter-name> characterEncodingFilter </filter-name> 47 <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> 48 <init-param> 49 <param-name> encoding </param-name> 50 <param-value> UTF-8 </param-value> 51 </init-param> 52 </filter> 53 <filter-mapping> 54 <filter-name> characterEncodingFilter </filter-name> 55 <! -- All requests must pass this verification --> 56 <url-pattern>/* </url-pattern> 57 </filter-mapping> 58 59 </web-app>
Web. xml
2. applicationContext. xml.
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" xmlns: context = "http://www.springframework.org/schema/context" 4 xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" 5 xmlns: jee = "http://www.springframework.org/schema/jee" 6 xsi: schemaLocation = "http://www.springframework.org/sche Ma/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd10 http://www.springframework.org/schema/aop11 http://www.springframework.org/schema/aop/spring-aop.xsd12 http://www.springframework.org/schema/tx13 http://www.springframework.org/schema/tx/spring-tx.xsd14 http: // w Ww.springframework.org/schema/jee15 http://www.springframework.org/schema/jee/spring-jee.xsd "> 16 17 <! -- Spring automatically scans all Java files under the base-package and its sub-packages. When scanning a Java file containing the @ service or @ controller annotation, it automatically registers the class as bean --> 18 <! -- You do not need to configure <context: annotation-config/> because --> 19 <context: component-scan base-package = "com. xxx. service "> </context: component-scan> 20 <context: component-scan base-package =" com. xxx. controller "> </context: component-scan> 21 22 <! -- Configure the data source --> 23 <bean id = "dataSource" 24 class = "org. springframework. jdbc. datasource. driverManagerDataSource "> 25 <property name =" driver "value =" com. mysql. jdbc. driver "> </property> 26 <property name =" url "value =" jdbc: mysql: // localhost: 3306/db_xxx "> </property> 27 <property name =" user "value =" root "> </property> 28 <property name =" password "value =" 123456"> </property> 29 </bean> 30 31 <! -- Configure sqlSessionFactory of mybatis --> 32 <bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> 33 <property name =" dataSource "ref =" dataSource "> </property> 34 <property name =" configLocation "value =" classPath: mybatis-config.xml "> </property> 35 <property name =" mapperLocations "value =" classPath: com/xxx/mappers /*. xml "> </property> 36 <property name =" typeAliasesPackage "value =" classPath: c Om. xxx. entity "> </property> 37 </bean> 38 <! -- SqlSessionFactory Attributes --> 39 <! -- 1, dataSource: Required attribute --> 40 <! -- 2, configLocation: configure this property when the mybatis-config.xml is placed under src (personal habits. The alias is already configured in SqlSessionFactoryBean, and I am not sure if the mybatis-config.xml file can be omitted. If you know each other, please tell us! Thank you! --> 41 <! -- 3, mapperLocations: automatically scans mapper. xml files. --> 42 <! -- 4, typeAliasesPackage: Automatic alias configuration --> 43 44 <! -- Converter MapperScannerConfig it can convert the interface to Bean in the Spring container without annotation (@ service) --> 45 <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> 46 <property name =" basePackage "value =" com. xxx. dao "> </property> 47 <property name =" sqlSessionFactoryBeanName "value =" sqlSessionFactory "/> 48 </bean> 49 50 <! -- Configure transaction notification --> 51 <! -- <Tx: annotation-driven/> is not used, that is, annotation transaction management. It is very troublesome to add @ Transactional before many public methods to the annotation transaction. You can use the following methods to fix and fix the issue once and for all --> 52 <tx: advice id = "txAdvice" transaction-manager = "transactionManager"> 53 <tx: attributes> 54 <tx: method name = "insert *" propagation = "REQUIRED"/> 55 <tx: method name = "add *" propagation = "REQUIRED"/> 56 <tx: method name = "save *" propagation = "REQUIRED"/> 57 <tx: method name = "delete *" propagation = "REQUIRED"/> 58 <tx: method name = "remove *" propagation = "REQU IRED "/> 59 <tx: method name =" update * "propagation =" REQUIRED "/> 60 <tx: method name = "edit *" propagation = "REQUIRED"/> 61 <tx: method name = "get *" propagation = "REQUIRED" read-only = "true"/> 62 <tx: method name = "find *" propagation = "REQUIRED" read-only = "true"/> 63 <tx: method name = "*" propagation = "REQUIRED" read-only = "true"/> 64 </tx: attributes> 65 </tx: advice> 66 <! -- Intercept method names starting with 'insert', 'add'... 'Find ', and so on. The last sentence indicates blocking all methods --> 67 68 <! -- Configure the transaction aspect and nest the transaction into the code logic through the aspect, so as not to intrude into the code business --> 69 <! -- If you need to use a single aspect, you must add a partition class when defining <aop: config>, that is, <aop: aspect> --> 70 <aop: config> 71 <aop: pointcut id = "txPointCut" expression = "execution (* com. xxx. service. *. *(..)) "/> 72 <aop: advisor advice-ref =" txAdvice "pointcut-ref =" txPointCut "/> 73 </aop: config> 74 <! -- Format of the tangent method expression --> 75 <! -- 76 execution (<scope> <return-type> <fully-qualified-class-name>. <method-name>. (parameters) 77 examples: 78 execution (* com. spring. service. *. *(..)): match the service package and its sub-package (service. impl) all methods of all classes; 79 execution (public List com. spring. service. impl. userServiceImpl. getUserList (int, String): All common methods that return a List in the 80-matched UserServiceImpl class and have the method name getUserList and the parameters of the int and String types are 81 --> 82 83 </beans>
ApplicationContext. xml
3, spring-mvc.xml
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 xsi: schemaLocation = "5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd"> 7 8 <! -- View parser --> 9 <bean id = "viewResolver" 10 class = "org. springframework. web. servlet. view. InternalResourceViewResolver"> 11 <! -- Prefix, jsp folder under WEB-INF --> 12 <property name = "prefix" value = "/WEB-INF/jsp/"/> 13 <! -- Suffix, ending with. jsp --> 14 <property name = "suffix" value = ". jsp"/> 15 </bean> 16 17 </beans>
Spring-mvc.xml
Mybatis-config.xml 4
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE configuration3 PUBLIC "-// mybatis.org//DTD Config 3.0 // EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <! -- Do not write anything. Data Sources, aliases, and er scans are defined in applicationContext. xml --> 6 <! -- Whether or not the mybatis master configuration file can be omitted. If you do not know, please let us know. Thank you! -->
Mybatis-config.xml