SSM framework integration (Spring + SpringMVC + MyBatis) and springmvcmybatis

Source: Internet
Author: User

SSM framework integration (Spring + SpringMVC + MyBatis) and springmvcmybatis

[SSM system architecture]

[Integration overview]

Step 1:

Integrate MyBatis with spring and use Spring to manage er interfaces.

Use the er scanner to automatically scan mapper interfaces for registration in Spring.

Step 2:

Use Spring to manage the Service interface.

Configure the Service interface in the Spring configuration file.

Implement transaction control.

Step 3:

SpringMVC is a Spring module and does not need to be integrated.

 

[Project]

[Database items]

[Table Structure]

 

[Table Data]

 

[1. Integrate dao]
Integrate Mybatis and Spring

[1.1 sqlMapConfig. xml] configuration file of MyBatis

 
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configurationPUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Add the global setting configuration as needed --> <! -- Configure the alias --> <typeAliases> <! -- Batch scan aliases --> <package name = "cn. Higgin. ssm. po"/> </typeAliases> <! -- Configure mapper because the integration package of spring and mybatis is used for integration. This requires no configuration, but must follow: mapper. xml and mapper. xml files with the same name and in the same directory --> <! -- <Mappers> </mappers> --> </configuration>
 

[1.2 db. properties] Database Configuration File

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=

[1.3 applicationContext-dao.xml]

Configuration required: data source, SqlSessionFactory, and er Scanner

 
<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans ht Tp: // www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.sprin Gframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! -- Load db. content in the properties file, db. the key name in the properties file must have certain special rules --> <context: property-placeholder location = "classpath: db. properties "/> <! -- Configure the data source, dbcp --> <bean id = "dataSource" class = "org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {jdbc. driver} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> <property name =" maxActive "value =" 30 "/> <property name =" maxIdle "value =" 5 "/> </Bean> <! -- SqlSessionFactory --> <bean id = "sqlSessionFactory" class = "org. mybatis. spring. SqlSessionFactoryBean"> <! -- Database connection pool --> <property name = "dataSource" ref = "dataSource"/> <! -- Load the global configuration file of mybatis --> <property name = "configLocation" value = "classpath: mybatis/sqlMapConfig. xml"/> </bean> <! -- Mapper scanner --> <bean class = "org. mybatis. spring. mapper. MapperScannerConfigurer"> <! -- Scan the package path. If you want to scan multiple packages, use commas to separate them. --> <property name = "basePackage" value = "cn. higgin. ssm. mapper "> </property> <property name =" sqlSessionFactoryBeanName "value =" sqlSessionFactory "> </property> </bean> </beans>
 

[1.4 reverse engineering generates PO class and mapper] (add, delete, modify, and query corresponding table)

[1.5 manually define product query mapper]

Generally, associated queries are used to query mapper. We recommend that you customize mapper.

[1.5.1 ItemsMapperCustom. Java]

 
Package cn. Higgin. ssm. po;/*** extension of product information */public class ItemsCustom extends Items {// Add attributes of product information extension}
 

[1.5.2 ItemsMapperCustom. xml]

 
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" cn. Higgin. ssm. mapper. ItemsMapperCustom "> <! -- Define the SQL segment of the item query, that is, the item query condition --> <SQL id = "query_items_where"> <! -- Use dynamic SQL and use if to judge and meet the conditions for SQL concatenation --> <! -- The item query condition is passed through the itemsCustom attribute in the ItemsQueryVo packaging object --> <if test = "itemsCustom! = Null "> <if test =" itemsCustom. name! = Null and itemsCustom. name! = ''"> Items. name LIKE '% $ {itemsCustom. name} %' </if> </SQL> <! -- Item List query --> <! -- ParameterType: Incoming packaging object (wrapped with query conditions) resultType: it is recommended to use extended object --> <select id = "findItemsList" parameterType = "cn. higgin. ssm. po. itemsQueryVo "resultType =" cn. higgin. ssm. po. itemsCustom "> SELECT items. * FROM items <where> <include refid = "query_items_where"> </include> </where> </select> </mapper>
 

 

[2. Integrated service]

Let spring manage service interfaces.

[2.1 ItemsService. java] defines the service Interface

 
Package cn. higgin. ssm. service; import java. util. list; import cn. higgin. ssm. po. itemsCustom; import cn. higgin. ssm. po. itemsQueryVo; public interface ItemsService {/*** query the database through ItemsMapperCustomer */public List <ItemsCustom> findItemsList (ItemsQueryVo itemsQueryVo) throws Exception ;}
 

[2.2 ItemsServiceImpl. java] service implementation Interface

 
Package cn. higgin. ssm. service. impl; import java. util. list; import org. springframework. beans. factory. annotation. autowired; import cn. higgin. ssm. mapper. itemsMapperCustom; import cn. higgin. ssm. po. itemsCustom; import cn. higgin. ssm. po. itemsQueryVo; import cn. higgin. ssm. service. itemsService; public class ItemsServiceImpl implements ItemsService {@ Autowired private ItemsMapperCustom detail; // query the database public List through detail <ItemsCustom> findItemsList (ItemsQueryVo itemsQueryVo) throws Exception {return response. findItemsList (itemsQueryVo );}}
 

[2.3 applicationContext-service.xml]

Configure service in spring container

 
<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans ht Tp: // www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.sprin Gframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! -- Define the service for Product Management --> <bean id = "itemService" class = "cn. Higgin. ssm. service. impl. ItemsServiceImpl"/> </beans>
 

2.4 applicationContext-transaction.xml transaction control

Use spring to declare the transaction control method in applicationContext-transaction.xml.

 
<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans ht Tp: // www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.sprin Gframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! -- The transaction manager controls the transactions that mybatis operates on the database, and spring uses the jdbc transaction control class --> <bean id = "transactionManager" class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "> <! -- The data source dataSource has been configured in the applicationContext-dao.xml --> <property name = "dataSource" ref = "dataSource"/> </bean> <! -- Notification --> <tx: advice id = "txAdvice" transaction-manager = "transactionManager"> <tx: attributes> <! -- Propagation Behavior --> <tx: method name = "save *" propagation = "REQUIRED"/> <tx: method name = "delete *" propagation = "REQUIRED"/> <tx: method name = "insert *" propagation = "REQUIRED"/> <tx: method name = "update *" propagation = "REQUIRED"/> <tx: method name = "find *" propagation = "SUPPORTS" read-only = "true"/> <tx: method name = "get *" propagation = "SUPPORTS" read-only = "true"/> <tx: method name = "select *" propagation = "SUPPORTS" read-on Ly = "true"/> </tx: attributes> </tx: advice> <! -- Aop --> <aop: config> <aop: advisor advice-ref = "txAdvice" pointcut = "execution (* cn. higgin. ssm. service. impl. *. *(..)) "/> </aop: config> </beans>
 

 

[3. Integrate springmvc]

[3.1 springmvc. xml]

Configure and process the mappers, adapters, and view parsers in springmvc.

 
<? 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: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans ht Tp: // www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.sprin Gframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! -- Scan controller, service ,... scan the controller and specify the controller package --> <context: component-scan base-package = "cn. higgin. ssm. controller "> </context: component-scan> <! -- Use mvc: annotation-driven instead of annotation er and annotation adapter to configure mvc: annotation-driven to load many parameter binding methods by default. For example, the json conversion parser is loaded by default, if you use mvc: annotation-driven, you do not need to configure the above RequestMappingHandlerMapping and RequestMappingHandlerAdapter to use mvc: annotation-driven --> <mvc: annotation-driven> </mvc: annotation-driven> <! -- View parser parses jsp parsing. By default, the jstl tag is used, and the jstl package under classpath --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <! -- Configure the prefix of the jsp path --> <property name = "prefix" value = "/WEB-INF/jsp/"/> <! -- Configure the suffix of the jsp path --> <property name = "suffix" value = ". jsp"/> </bean> </beans>
 

[3.2 in web. xml] configure the front-end controller code in web. xml

 
<! -- SpringMVC front-end Controller --> <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/springmvc. xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern> *. action </url-pattern> </servlet-mapping>
 

[3.3 ItemsController. java]

Compile Controller (Handler)

 
Package cn. higgin. ssm. controller; import java. util. list; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. servlet. modelAndView; import cn. higgin. ssm. po. itemsCustom; import cn. higgin. ssm. service. itemsService; @ Controllerpublic class ItemsController {@ Autowired private ItemsService itemsService; // item query @ RequestMapping ("/queryItems") public ModelAndView queryItems () throws Exception {// call the Service to find the database, query the product List. Here, we use static data to simulate the List <ItemsCustom> itemsList = itemsService. findItemsList (null); // return ModelAndView modelAndView = new ModelAndView (); // equivalent to the setAttribute of the request, which is obtained through itemList on the jsp page. addObject ("itemsList", itemsList); // specify the modelAndView. setViewName ("items/itemsList"); System. out. println ("annotation method: ItemsComtroller ...... "); return modelAndView ;}}
 

[4. itemsList. jsp]

 

Location: WEB-INF/jsp/items/itemsList. jsp

 
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "prefix =" c "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/fmt "prefix =" fmt "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">  

 

[5. Load the spring container]

Load mapper, service, and controller to the spring container.

We recommend that you use wildcards to load the preceding configuration file.

In web. xml, add the Spring container listener to load the spring container.

[Complete web. xml]

 
<? 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_2_5.xsd" id = "WebApp_ID" version = "2.5"> <display-name> Spring_SpringMVC_MyBatis </display-name> <! -- Load spring container --> <context-param> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/classes/spring/applicationContext -*. xml </param-value> </context-param> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <! -- SpringMVC front-end Controller --> <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/springmvc. xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern> *. action </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>

 

Source code source: http://minglisoft.cn/technology

If you are interested, you can go to the ball ~ Sharing learning technologies: 2042849237

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.