标题:mybatis spring集成
只看楼主
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
结帖率:40%
 问题点数:0 回复次数:7 
mybatis spring集成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.
    xmlns:xsi="http://www. xmlns:context="http://www.
    xmlns:mvc="http://www.
    xsi:schemaLocation="http://www.           
    http://www.           
    http://www.           
    http://www.           
    http://www.           
    http://www.
    <bean id="dataSource" class="org.
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3309/mybatis?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="123" />
        <property name="maxActive" value="100"></property>
        <property name="maxIdle" value="30"></property>
        <property name="maxWait" value="500"></property>
        <property name="defaultAutoCommit" value="true"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 注册Mapper方式一 -->
        <!-- 该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的
            元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。因为这些都是使用Spring中的配置 。当然如果你不打算添加typeAlias
            之类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了 -->
        <!--<property name="configLocation" value="classpath:com/conf/Configuration.xml"
            /> -->
        <!-- 注册Mapper方式二 -->
        <!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名,且在同一路径下,那么可以不配置该选项 -->
        <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件, mapperLocations和configLocation有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
        <property name="mapperLocations" value="com/pojo/usedaomap.xml" />
    </bean>
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    <bean id="UserServiceImpl2" class="com.userserviceimp2">
        <property name="sqlSession" ref="sqlSessionTemplate"></property>
    </bean>
    <bean id="UserServiceImpl1" class="com.userserviceimp1">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>



    <!-- 注册Mapper方式三:也可不指定特定mapper, 而使用自动扫描包的方式来注册各种Mapper ,即直接从接口中获得,配置如下: -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com" />
    </bean>
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
        <property name="mapperInterface" value="com.IUserDao" />
    </bean>
    <bean id="UserServiceImpl0" class="com.userserviceimp0">
        <property name="userDao" ref="IUserDao"></property>
    </bean>
</beans>
搜索更多相关主题的帖子: version spring 
2013-11-09 16:22
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
得分:0 
<?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration PUBLIC "-// Config 3.0//EN"
 "http://
 <configuration>
     <typeAliases>
         <typeAlias alias="User" type="com.pojo.User"/>
         <typeAlias alias="Article" type="com.pojo.Article"/>  
     </typeAliases>
     <mappers>
       <mapper resource="com/pojo/usedaomap.xml" />
      </mappers>
  </configuration>
 </configuration>
2013-11-09 16:26
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
得分:0 
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN" "http://
<mapper namespace="com.IUserDao.map">
    <select id="countAll" resultType="int">
        select count(*) c from user;
    </select>
</mapper>
2013-11-09 16:26
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
得分:0 
package com;

import org.apache.ibatis.annotations.Select;


public interface IUserDao {
     @Select("select count(*) c from user")
     public int countAll();
}
2013-11-09 16:27
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
得分:0 
package com;

public interface IUserService {
    public int countAll();
}
2013-11-09 16:27
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
得分:0 
package com;

public class userserviceimp0 implements IUserService {
    private IUserDao userDao;

    public IUserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    public int countAll() {
        return this.userDao.countAll();
    }
}
2013-11-09 16:27
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
得分:0 
package com;

import org.mybatis.spring.support.SqlSessionDaoSupport;

public class userserviceimp1 extends SqlSessionDaoSupport implements IUserService {
    @Override
    public int countAll() {
        return getSqlSession().selectOne("com.IUserDao.map.countAll");
    }

}
2013-11-09 16:27
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
得分:0 
package com;

import org.mybatis.spring.SqlSessionTemplate;

public class userserviceimp2 implements IUserService {
    public SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
         this.sqlSession = sqlSession;
    }
    @Override
    public int countAll() {
        return sqlSession.selectOne("com.IUserDao.map.countAll");
    }

}
2013-11-09 16:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-423295-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.170761 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved