原文:http://m.oschina.net/code/snippet_94799_3366
[代码] [Java]代码
映射文件内容(User.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mbs.com.mapper.IUserMapper">
<resultMap type="User" id="uM">
<id property="id" column="id" />
<result property="address" column="address" />
<result property="birthday" column="birthday" />
<result property="cardId" column="cardId" />
<result property="email" column="email" />
<result property="loginName" column="loginName" />
<result property="passWord" column="passWord" />
<result property="pictureUrl" column="pictureUrl" />
<result property="sex" column="sex" />
<result property="telphone" column="telphone" />
<result property="userName" column="userName" />
</resultMap>
<!--查询:通过用户ID返回一个用户对象 -->
<select id="getUser" resultMap="uM" parameterType="Integer">
select * from users
<where>#{id}</where>
</select>
</mapper>
mybatis_config.xml内容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="user" type="mbs.com.pojo.User" />
</typeAliases>
<mappers>
<mapper resource="mapper_sql/User.xml" />
</mappers>
</configuration>
IUserService.java 接口内容
public interface IUserService
{
User getUser(Integer id);
}
IUserMapper.java 映射器内容
@Mapper("userMapper")
public interface IUserMapper
{
User getUser(Integer id);
}
FooService.java 实现类内容
@Service("fooService")
public class FooService implements IUserService
{
@Autowired
private IUserMapper userMapper;
public User getUser(Integer id)
{
return userMapper.getUser(id);
}
}
spring.xm内容
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd"
default-init-method="init">
<!-- Annotation Config -->
<context:annotation-config/>
<!--注册数据库的连接信息 -->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!-- 扫描物理路径及注册 -->
<context:component-scan base-package="mbs.com"/>
<!-- Data Source -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${dirver}"/>
<property name="driverUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<property name="minimumConnectionCount" value="${min_conn_count}"/>
<property name="maximumConnectionCount" value="${max_conn_count}"/>
</bean>
<!-- 配置mybatis的sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis_config.xml"/>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置 ibatis的映射器 -->
<bean id="userMapper" class="org.mybatis.spring.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="mbs.com.mapper.IUserMapper"/>
</bean>
</beans>
main 函数测试
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
IUserService service =(IUserService)context.getBean("fooService");
User user = service.getUser(1);
System.out.println("用户名:"+user.getUserName());
System.out.println("密 码:"+user.getPassWord());
}