spring+mybatis中以配置文件方式实现增删改查

   当前各个企业对Spring+Mybatis技术应用都较为广泛,而此类框架中注解在给人们带来方便的同时也对某些特殊业务下的封装增加了困难。本文提供了以配置文件的方式进行spring注入的方法。
   1、首先在controller中引入
private ApplicationContext applicationContext = null;并添加set方法注入到spring中。
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
    2、创建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、创建一个service接口并创建实现类将其通过配置文件的方式配置到spring中
<bean id="testSearch" class="com.aisino.bigdata.support.serviceImpl.QueryServiceImpl">
  
</bean>
在controller中可与i通过applicationContext进行调用
QueryService queryService = (QueryService) applicationContext
.getBean("testSearch");
通过这种调用我们就可以不通过注解将service注入到spring中,并可以在同一个controller中动态调用不同的service,将controller封装为数据传输工具只需要从前端传入beanid就可以调用不同的controller了。
    4、创建dao接口和dao接口的实现类,并在配置文件中配置dao接口的bean,并需要再dao中实现sqlSessionFactory工厂,并添加所需要查询的sql映射的路径。
其中sqlMapper的value中为本次sql映射的路径。
<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>
编写dao实现中的查询
//创建sqlSessionFactory工厂,添加get,set方法
private SqlSessionFactory sqlSessionFactory;
//获取sql映射,添加get,set方法
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);
//打开sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
执行查询
List<Map<String, Object>> list = sqlSession.selectList(decodeSqlMapper(),map);
return list;
}
/**
* 获取配置文件中的映射
* @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;
}

猜你喜欢

转载自liushuoa.iteye.com/blog/2404799