2017/12/31
承接上文:Spring(13):新增功能:超市订单的供应商表查询 and 根据供应商名称查询供应商信息--功能实现(上)(http://blog.youkuaiyun.com/qq_29166327/article/details/78943953)
Spring框架下的相关函数:
1、ClassPathXmlApplicationContext构造方法:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
ClassPathXmlApplicationContext
public ClassPathXmlApplicationContext(String configLocation) throws BeansException
-
Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML file and automatically refreshing the context.
-
Parameters:
-
configLocation- resource location
Throws:
-
BeansException- if context creation failed
-
2、getBean()方法:
ProviderService providerService = (ProviderService) ctx.getBean("providerServiceImpl");
getBean
public Object getBean(String name) throws BeansException
-
Description copied from interface:
BeanFactory -
Return an instance, which may be shared or independent, of the specified bean.
This method allows a Spring BeanFactory to be used as a replacement for the Singleton or Prototype design pattern. Callers may retain references to returned objects in the case of Singleton beans.
Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
-
-
Specified by:
-
getBeanin interfaceBeanFactory
-
-
-
Parameters:
-
name- the name of the bean to retrieve
Returns:
- an instance of the bean Throws:
-
NoSuchBeanDefinitionException- if there is no bean definition with the specified name -
BeansException- if the bean could not be obtained
-
sqlSession 类的相关函数:
【根据查询是否需要传参来确定】
1、selectList
<E> List<E> selectList(String statement)
- Retrieve a list of mapped objects from the statement key and parameter.
-
-
Type Parameters:
-
E- the returned list element type
Parameters:
-
statement- Unique identifier matching the statement to use.
Returns:
- List of mapped object
-
2、selectList
<E> List<E> selectList(String statement, Object parameter)
- Retrieve a list of mapped objects from the statement key and parameter.
-
-
Type Parameters:
-
E- the returned list element type
Parameters:
-
statement- Unique identifier matching the statement to use. -
parameter- A parameter object to pass to the statement.
Returns:
-
List of mapped object
-

本文介绍如何使用Spring框架实现超市订单的供应商表查询及根据供应商名称查询供应商信息的功能。主要涉及ClassPathXmlApplicationContext的配置加载、getBean方法的使用以及sqlSession的selectList方法进行数据查询。

被折叠的 条评论
为什么被折叠?



