前面说到spring mvc+maven,今天把mybatis集成上去了
1、首先是配置文件的修改-applicationContext.xml,增加mybatis的配置,前提:先引用mybatis的jar包
<span style="white-space:pre"> </span><!-- 加入自动扫描 --> <context:component-scan base-package="alijk.dataView.**" /> <context:annotation-config /> <!-- 加载系统中的properties文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="100" /> <property name="maxWait" value="1000" /> <property name="poolPreparedStatements" value="false" /> <property name="defaultAutoCommit" value="true" /> </bean> <!-- 配置mybatis sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:alijk/dataView/dao/*.xml" /> <property name="typeAliasesPackage" value="alijk.dataView.model"/> </bean> <!-- scan for mappers and let them be autowired 自动扫描mapper包 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="alijk.dataView.dao" /> </bean>2、新建mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="alijk.dataView.dao.SysUserDao"> <select id="getAlluser" parameterType="java.util.HashMap" resultType="java.util.Map"> SELECT USER_ID,LOGIN_NAME,LOGIN_PASSWORD,USER_NAME FROM SYS_USER WHERE DATA_FLAG = #{data_flag} </select> </mapper>
@Repository public interface SysUserDao { List<Map<String, Object>> getAlluser(Map<String, Object> map); }
public interface SysUserInf { List<Map<String, Object>> getAlluser(Map<String, Object> map); }
@Service public class SysUserInfImpl implements SysUserInf{ @Autowired private SysUserDao sysUserDao; public List<Map<String, Object>> getAlluser(Map<String, Object> map) { return sysUserDao.getAlluser(map); } }
6、测试,用SpringJUnit4ClassRunner测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class MybatisTest { @Autowired private SysUserInf sysUserInf; @Test public void testMybatis(){ Map<String, Object> map_query = new HashMap<String, Object>(); map_query.put("data_flag", "0"); List<Map<String, Object>> map = sysUserInf.getAlluser(map_query); for(Map<String, Object> user : map){ Iterator<String> iter = user.keySet().iterator(); String row = ""; while(iter.hasNext()){ row += user.get(iter.next())+"\t"; } System.out.println(row); } } }
14117106365323 gulou b445e54cb19b0e9fd41f1c13540a9a9d gulou 14117196624151 江苏南京六合区用户 1952a781d27ca1f47 liuhe 14132501631192 江苏南京六合区用户 1a3bb2e9dfece8ec82