spring+mybatis

前面说到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>

3、mapper对应该的dao

@Repository
public interface SysUserDao {
	List<Map<String, Object>> getAlluser(Map<String, Object> map);
}

4、数据库访问接口service

public interface SysUserInf {
	List<Map<String, Object>> getAlluser(Map<String, Object> map);
}

5、接口的实现

@Service
public class SysUserInfImpl implements SysUserInf{
	
	@Autowired
	private SysUserDao sysUserDao;
	
	public List<Map<String, Object>> getAlluser(Map<String, Object> map) {
		
		return sysUserDao.getAlluser(map);
	}
	
}

以后工作做完后,spring+mybatis已经配置完成,基本上结构为:配置文件(spring加载,datasource,mybatis数据源配置,mapper配置等)、mapper、dao、service、serviceImpl

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值