Get the App
SLTechnology News&Howtos  ›  Database  › 

Springmvc Classic tutorials (2)

Shulou Source: shulou.com Published: 2022-06-01 08:29:07 10月01日 Update

Springmvc tutorial series

Best tutorial in the History of springmvc (2)

Best tutorial in the History of springmvc (1)

Best tutorial in the History of springmvc (3)

Best tutorial in the History of springmvc (4)

First, integrate mybatis

In order to better learn the integrated development method of springmvc and mybatis, it is necessary to integrate springmvc and mybatis.

Integration goal: springmvc is used in the control layer and mybatis is used in the persistence layer.

1.1 demand

Realize commodity query list and query commodity information from MySQL database.

1.2 jar package

Including: spring (including springmvc), mybatis, mybatis-spring integration package, database driver, third-party connection pool.

1.3 Dao

Goal:

1. Spring manages SqlSessionFactory and mapper

1.3.1 sqlMapConfig.xml

Create mybatis/sqlMapConfig.xml under classpath

1.3.2 applicationContext-dao.xml

Configure data source, transaction management, configure SqlSessionFactory, mapper scanner.

1.3.3 ItemsMapper.xml

And items.name like'% ${items.name}%'

Select * from items

1.3.4 ItemsMapper.java

Public interface ItemsMapper {

/ / Product list

Public List findItemsList (QueryVo queryVo) throws Exception

}

1.4 Service

Goal:

1. Service is managed by spring

2. Spring controls the transaction of Service.

1.4.1 applicationContext-service.xml

Configure the service interface.

1.4.2 applicationContext-transaction.xml

Configure the transaction manager.

1.4.3 OrderService

Public interface OrderService {

/ / Product query list

Public List findItemsList (QueryVo queryVo) throws Exception

}

@ Autowired

Private ItemsMapper itemsMapper

@ Override

Public List findItemsList (QueryVo queryVo) throws Exception {

/ / query commodity information

Return itemsMapper.findItemsList (queryVo)

}

}

1.5 Action

1.5.1 springmvc.xml

1.5.2 web.xml

Load the spring container and configure the springmvc front controller.

Springmvc

ContextConfigLocation

/ WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml

Org.springframework.web.context.ContextLoaderListener

CharacterEncodingFilter

Org.springframework.web.filter.CharacterEncodingFilter

Encoding

Utf-8

CharacterEncodingFilter

/ *

Springmvc

Org.springframework.web.servlet.DispatcherServlet

ContextConfigLocation

Classpath:spring/springmvc.xml

one

Springmvc

* .action

Index.html

Index.htm

Index.jsp

Default.html

Default.htm

Default.jsp

1.5.3 OrderController

@ Controller

Public class OrderController {

@ Autowired

Private OrderService orderService

@ RequestMapping ("/ queryItem.action")

Public ModelAndView queryItem () throws Exception {

/ / Product list

List itemsList = orderService.findItemsList (null)

/ / create modelAndView to populate data and set view

ModelAndView modelAndView = new ModelAndView ()

/ / populate data

ModelAndView.addObject ("itemsList", itemsList)

/ / View

ModelAndView.setViewName ("order/itemsList")

Return modelAndView

}

}

1.6 Test

Http://localhost:8080/springmvc_mybatis/queryItem.action

Tags: Goods tutorials data integration configuration best query management transaction goal control transaction management information database view container scanner interface controller data source Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Shulou Technology macOS Linux