In spring+mybatis, adding, deleting, modifying and checking is implemented in the form of configuration files.

   At present, various enterprises have widely used Spring+Mybatis technology, and annotations in such frameworks bring convenience to people, but also increase the difficulty of encapsulation under some special services. This article provides a method for spring injection in the form of configuration files.
   1. First, introduce
private ApplicationContext applicationContext = null in the controller; and add the set method to inject into spring.
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
    2. Create a configuration file for applicationContext-search
<?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:jee="http://www.springframework.org/ schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
<description>Spring-JDBC</description>
  
</beans>
    3. Create a service interface and create an implementation class to configure it into spring through a configuration file
<bean id="testSearch" class="com.aisino.bigdata.support.serviceImpl.QueryServiceImpl">
  
</bean>
can be called with i through applicationContext in the controller
QueryService queryService = (QueryService) applicationContext
.getBean("testSearch" );
through this invocation, we can inject the service into spring without annotations, and can dynamically call different services in the same controller, encapsulate the controller as a data transmission tool, and only need to pass in the beanid from the front end to call different services the controller.
    4. Create the dao interface and the implementation class of the dao interface, configure the bean of the dao interface in the configuration file, and need to implement the sqlSessionFactory factory in the dao, and add the path of the sql mapping that needs to be queried.
The value of sqlMapper is the path of this sql mapping.
<bean id="QueryDaoImpl" class="com.lius.bigdata.support.daoImpl.QueryDaoImpl">
    <property name="sqlSessionFactory" ref="sqlSessionFactory">
</property>
<property name="sqlMapper" value="com.lius.bigdata.daoMapper.testMapper.testsqlid"></property>
    </bean>
配置文件如图1中namespace配置为映射文件路径
<mapper namespace="com.lius.bigdata.daoMapper.testMapper">
将dao配置到service中
<bean id="testSearch" class="com.lius.bigdata.support.serviceImpl.QueryServiceImpl">
        <property name="queryDao" ref="QueryDaoImpl">
        </property>
    </bean>
    <bean id="QueryDaoImpl" class="com.aisino.bigdata.support.daoImpl.QueryDaoImpl">
    <property name="sqlSessionFactory" ref="sqlSessionFactory">
</property>
<property name="sqlMapper" value="com.aisino.bigdata.example.fpyj.search.daoMapperNew.searchKpsjycjkMapper.queryKpsjycjk"></property>
    </bean>
Write the query in the dao implementation
//Create a sqlSessionFactory factory, add get, set methods
private SqlSessionFactory sqlSessionFactory;
//Get sql mapping, add get, set methods
private String sqlMapper;
@Override
public List<Map<String, Object >> query(Map<String, Object> map, int page,
int pageSize) {
map.put("pageSize", pageSize);
map.put("startNum", (page - 1) * pageSize);
//Open sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
execute query
List<Map<String, Object>> list = sqlSession.selectList(decodeSqlMapper(),map);
return list;
}
/**
* Get the mapping in the configuration file
* @return
*/
private String decodeSqlMapper(){
String sqlMapper = getSqlMapper();
System.out.println(sqlMapper);
return sqlMapper;
}

/**
* @return the sqlSessionFactory
*/
public SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}

/**
* @param sqlSessionFactory the sqlSessionFactory to set
*/
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}

/**
* @return the sqlMapper
*/
public String getSqlMapper() {
return sqlMapper;
}

/**
* @param sqlMapper the sqlMapper to set
*/
public void setSqlMapper(String sqlMapper) {
this.sqlMapper = sqlMapper;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326447609&siteId=291194637