mybatis3.0.2整合spring3.0

原文: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());
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值