IDEA中使用maven整合mybatis與spring

整合思路

須要spring經過方式管理SqlSessionFactory.
spring和mybatis整合生成代理對象,使用SqlSessionFactory建立SqlSession.(Sping和mybati整合自動完成).
持久層的mapper都須要有spring進行管理.java

整合環境

建立一個新的java工程.spring

mybatis的jar包,spring的jar包
spring和mybatis的整合包
參考:SSM的maven工程的建立sql

代碼操做

1、建立配置文件:applicationContext.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 加載配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 數據源,使用dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="maxActive" value="${maxActive}"/>
        <property name="maxIdle" value="${maxIdle}"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加載mybatis的配置文件-->
        <property name="configLocation" value="mybatis/SqlMapperConfig.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

其餘配置文件的建立apache

2、原始dao開發:applicationContext.xml

applicationContext.xml中的原始dao配置spring-mvc

<!-- 原始dao接口 -->
    <bean id="userDao" class="club.lemos.ssm.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

測試文件:tomcat

public class UserDaoImplTest {

    private ApplicationContext applicationContext;
    @Before
    public void setUp() throws Exception {

        //獲取spring的配置文件applicationContext.xml,這個配置文件中包含着dbcp數據源的相關配置.
        //還包含着sqlSessionFactory的生成類SqlSessionFactoryBean.它可讓數據源和 sqlMapConfig.xml(配置了mapper的映射文件及mybatis的其餘配置信息)相聯繫.
        //還有原始的dao接口,這個接口依賴於上面的SqlSessionFactory 那麼能夠在dao的實現類中能夠經過繼承SqlSessionDaoSupport來
        //獲取sqlSessionFactory對象.
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
    }

    @Test
    public void findUserById() throws Exception {

        //經過spring配置文件,獲取userDao對象
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");

        //調用UserDao的方法
        User userById = userDao.findUserById(1);
        System.out.println(userById);
    }
}

注意事項:mybatis

IntelliJ Idea編譯報錯:javacTask: 源發行版 1.7 須要目標發行版 1.7
解決辦法:選中項目,右擊選擇Maven–>Reimport, 再次編譯,問題解決。mvc

原文:http://blog.csdn.net/wave_1102/article/details/47671019app

 

或者,可使用:關鍵是java編譯插件。maven

<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 資源文件拷貝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

3、mapper代理開發

applicationContext.xml中的mapper代理配置

<!--mapper代理配置,由mapper接口生成代理對象.原始dao的方法是根據實現類生成代理對象 -->
    <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
        <!--<property name="mapperInterface" value="club.lemos.ssm.mapper.UserMapper"/>-->
        <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"/>-->
    <!--</bean>-->

    <!--自動掃描mapper,其bean對象的id默認爲接口名,首字母小寫-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 這裏不使用sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 指定掃描的路徑 -->
        <property name="basePackage" value="club.lemos.ssm"/>
    </bean>

注意:使用IDEA的maven工具進行開發時,會出現spring代理器沒法讀取mapper配置文件 XXXMaper.xml的問題—— org.apache.ibatis.binding.BindingException

.
此時,能夠嘗試手動將其移動到對應接口的類路徑下,看看能不能運行。

可是,實際開發中不可能這樣作,能夠在pom.xml中配置下面這句話:

...
    <build>
        <finalName>springmvc-study</finalName>
        <resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
    </build>
</project>

若是文件已經移動到指定的位置。還報錯。那麼多是文件自身有問題。好比命名空間不正確。以前就是逆向工程生成的mapping文件的命名空間不正確致使這個問題。

 

END

相關文章
相關標籤/搜索