dao實現類UserDaoImpl2,,代碼以下:
Java代碼
public
class UserDaoImpl2
implements UserDao {
private UserMapper userMapper;
public
void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public User getUserById(User user) {
return userMapper.getUser(user.getId());
}
}
二、採用接口org.apache.ibatis.session.SqlSession的實現類org.mybatis.spring.SqlSessionTemplate。
mybatis中, sessionFactory可由SqlSessionFactoryBuilder.來建立。
MyBatis-Spring 中,使用了SqlSessionFactoryBean來替代。
SqlSessionFactoryBean有一個必須屬性dataSource,
另外其還有一個通用屬性configLocation(用來指定mybatis的xml配置文件路徑)。
(1)Spring配置文件:
<!-- 建立SqlSessionFactory,同時指定數據源-->
<bean
id=
"sqlSessionFactory"
class=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<property
name=
"dataSource"
ref=
"dataSource"
/>
<!-- 指定sqlMapConfig總配置文件,訂製的environment在spring容器中不在生效-->
<property
name=
"configLocation"
value=
"classpath:sqlMapConfig.xml"
/>
<!--指定實體類映射文件,能夠指定同時指定某一包以及子包下面的全部配置文件,mapperLocations和configLocation有一個便可,當須要爲實體類指定別名時,可指定configLocation屬性,再在mybatis總配置文件中採用mapper引入實體類映射文件 -->
<!- -
<property
name=
"mapperLocations"
value=
"classpath*:com/xxt/ibatis/dbcp/**/*.xml"
/> --
>
<bean
>
(2)mybatis總配置文件sqlMapConfig.xml:
<configuration>
<typeAliases>
<typeAlias
type=
"com.xxt.ibatis.dbcp.domain.User"
alias=
"User"
/>
</typeAliases>
<mappers>
<mapper
resource=
"com/xxt/ibatis/dbcp/domain/user.map.xml"
/>
</mappers>
</configuration>
(3)實體類映射文件user.map.xml:
<mapper namespace=
"com.xxt.ibatis.dbcp.domain.User">
<resultMap type=
"User" id=
"userMap">
<id property=
"id" column=
"id" />
<result property=
"name" column=
"name" />
<result property=
"password" column=
"password" />
<result property=
"createTime" column=
"createtime" />
</resultMap>
<select id=
"getUser" parameterType=
"User" resultMap=
"userMap">
select * from user where id = #{id}
</select>
<mapper/>
(4)dao層接口實現類UserDaoImpl:
Java代碼
public
class UserDaoImpl
implements UserDao {
public SqlSessionTemplate sqlSession;
public User getUserById(User user) {
return (User)sqlSession.selectOne(
"com.xxt.ibatis.dbcp.domain.User.getUser", user);
}
public
void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession; }
}
三、採用抽象類org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。
(1)spring配置文件:
Java代碼
<bean id=
"sqlSessionFactory"
class=
"org.mybatis.spring.SqlSessionFactoryBean">
<property name=
"dataSource" ref=
"dataSource" />
<property name=
"configLocation" value=
"classpath:sqlMapConfig.xml"/>
<!-- <property name=
"mapperLocations" value=
"classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"/ > -->
</bean>
<bean id=
"sqlSession"
class=
"org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index=
"0" ref=
"sqlSessionFactory" />
</bean>
<bean id=
"userDaoImpl3"
class=
"com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl3">
<!--注入SqlSessionTemplate實例 -->
<property name=
"sqlSessionTemplate" ref=
"sqlSession" />
<!--也可直接注入SqlSessionFactory實例,兩者都指定時,SqlSessionFactory失效 -->
<!-- <property name=
"sqlSessionFactory" ref=
"sqlSessionFactory" /> -->
</bean>
(2) dao層接口實現類UserDaoImpl3:
Java代碼
public
class UserDaoImpl3
extends SqlSessionDaoSupport
implements UserDao {
public User getUserById(User user) {
return (User) getSqlSession().selectOne(
"com.xxt.ibatis.dbcp.domain.User.getUser", user);
}
}