首页 > 代码库 > spring与mybati整合方法
spring与mybati整合方法
(1)spring配置文件:
- <!-- 引入jdbc配置文件 -->
- <context:property-placeholder location="jdbc.properties"/>
- <!--创建jdbc数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value=http://www.mamicode.com/"${driver}"/>
- <property name="url" value=http://www.mamicode.com/"${url}"/>
- <property name="username" value=http://www.mamicode.com/"${username}"/>
- <property name="password" value=http://www.mamicode.com/"${password}"/>
- <property name="initialSize" value=http://www.mamicode.com/"${initialSize}"/>
- <property name="maxActive" value=http://www.mamicode.com/"${maxActive}"/>
- <property name="maxIdle" value=http://www.mamicode.com/"${maxIdle}"/>
- <property name="minIdle" value=http://www.mamicode.com/"${minIdle}"/>
- </bean>
- <!-- 创建SqlSessionFactory,同时指定数据源-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!--创建数据映射器,数据映射器必须为接口-->
- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value=http://www.mamicode.com/"com.xxt.ibatis.dbcp.dao.UserMapper" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- </bean>
- <bean id="userDaoImpl2" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2">
- <property name="userMapper" ref="userMapper"/>
- </bean>
(2)数据映射器UserMapper,代码如下:
- public interface UserMapper {
- @Select("SELECT * FROM user WHERE id = #{userId}")
- User getUser(@Param("userId") long id);
- }
(3) dao接口类UserDao,代码如下:
- public interface UserDao {
- public User getUserById(User user);
- }
(4)dao实现类UserDaoImpl2,,代码如下:
- 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());
- }
- }
spring与mybati整合方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。