ssm框架搭建之sm框架整合

之前已经搭建好了ss框架,现在接上sm框架搭整合;

1 需要引入几个依赖jar包:

<!--mybatis的依赖-->
       <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
         <!--spring和mybats整合的jar包-->
         <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>

 2 配置XML文件

   1⃣️为了扩展和便于管理,将单独配置操作持久层的文件,一个文件一个数据源  

上图中:spring-dal-mysql就是我的持久层的配置文件了,如果需要加载到这个文件可以在applicationContext.xml文件中引入:<import resource="spring-dal-mysql.xml"></import>即可。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
       default-lazy-init="true">

    <!--destroy-method="close":当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.该连接池是tomcat的-->
    <bean id="mysqlDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!--还有一些其他属性需要设置。比如最大连接数,连接时间等等 。。。-->
    </bean>

    <!--配置mybatis和spring整合,并自动扫描-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="mysqlDataSource"/>
        <!--typeAliases 是一个类的数组类型,该配置表示扫描这该包下面的-->
        <property name="typeAliasesPackage" value="FirstDemo.dao.domain"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="FirstDemo.dao.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--配置事务-->
    <bean id="mysqlDataSourceTransManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="mysqlDataSource"/>
    </bean>
</beans>

ps:上面红色标示的参数值(${jdbc.username};${jdbc.password})可是有小技巧的哦。因为这个项目是用maven来管理的,所以下面贴上这个地方的一个小配置,在pom.xml文件中加入:
<!--设置加载不同的配置环境-->
    <profiles>
        <profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
    </profiles>

    <!--配置可以自动匹配配置文件的信息值-->
    <build>
        <finalName>FistSpringMvcPrj</finalName>
        <!--扫描到到文件到路径,即各个resources中不同后缀文件中的变量的值的配置位置-->
        <filters>
            <filter>src/filters/${env}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xlsx</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xlsx</include>
                </includes>
                <excludes>
                    <exclude>**/.svn</exclude>
                </excludes>
            </resource>
        </resources>
    </build>

 3 *mapper.xml 文件的配置,本文中叫:UserMapper.xml .但是还需要一个地方需要注意,就是这个 *mapper.xml 中还需要关联好对应的mapper.java文件,“<mapper namespace="FirstDemo.dao.mapper.UserMapper">” 这句话。不然的话会报错的,见报错。

 代码1:

<?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="FirstDemo.dao.mapper.UserMapper">
    <!--1 可以配置映射关系-->
    <!--2 具体的mybatis的sql了,注意这个地方的sql标签的id就是对应的mapper.java文件接口中对应的方法名称-->
</mapper>

报错:

15:05:36.343 [RMI TCP Connection(2)-127.0.0.1] ERROR o.m.spring.mapper.MapperFactoryBean - Error while adding the mapper 'interface FirstDemo.dao.mapper.UserMapper' to configuration.
org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 1; 文件提前结束。
	at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:253) ~[mybatis-3.1.1.jar:3.1.1]

  

4 几个坑:

  jar包版本不一致一般会出现的错误,比如说这里的,因为spring-core的版本应该和其他spring的包保持一致,但是我这里没有,而是低了几个版本,一般报这个错基本是两个原因:1⃣️jar版本不对,2⃣️缺少对应jar包了:

  

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) [spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]

  没有在*mapper.xml 中配置对应的namespace:

14:42:14.939 [RMI TCP Connection(2)-127.0.0.1] ERROR o.m.spring.mapper.MapperFactoryBean - Error while adding the mapper 'interface FirstDemo.dao.mapper.UserMapper' to configuration.
org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 1; 文件提前结束。

 需要加上上面3中红色部门标记的配置,这个制定的是对应的接口的那个mapper.java文件,一般这两个是放在一个包下面的

猜你喜欢

转载自java-holding.iteye.com/blog/2354041