The integration of sm framework built by ssm framework

The ss framework has been built before, and now it is connected to the sm framework for integration;

1 Need to introduce several dependent jar packages:

<!--Mybatis dependencies -->
       <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
         <!--Spring and mybats integrated jar package -->
         <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>

 

 2 Configuration XML file

   1⃣️In order to expand and facilitate management, the files for operating the persistence layer will be configured separately, one data source per file  

In the picture above: spring-dal-mysql is the configuration file of my persistence layer. If you need to load this file, you can import it in the applicationContext.xml file: <import resource="spring-dal-mysql.xml"></import import> can be.

<?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": When the database connection is not in use, put the connection back into the data pool to facilitate the next call. The connection pool is tomcat's -->
    <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}"/>
        <!-- There are some other properties that need to be set. Such as the maximum number of connections, connection time and so on. . . -->
    </bean>

    <!--Configure mybatis and spring integration, and automatically scan -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="mysqlDataSource"/>
        <!--typeAliases is an array type of a class, this configuration means to scan the --> under this package
        <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>

    <!--Configure Transaction-->
    <bean id="mysqlDataSourceTransManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="mysqlDataSource"/>
    </bean>
</beans>

ps: The parameter values ​​marked in red above (${jdbc.username};${jdbc.password}) are tricky. Because this project is managed by maven, paste a small configuration of this place below and add it to the pom.xml file:
<!--Set to load a different configuration environment-->
    <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>

    <!--Configuration can automatically match the information value of the configuration file-->
    <build>
        <finalName>FistSpringMvcPrj</finalName>
        <!--Scan to file to path, that is, the configuration location of the value of the variable in the different suffix files in each 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 The configuration of the *mapper.xml file is called in this article: UserMapper.xml. But there is one more place to pay attention, that is, the corresponding mapper.java file needs to be associated in this *mapper.xml, " <mapper namespace="FirstDemo .dao.mapper.UserMapper">” sentence. Otherwise, an error will be reported, see Error.

 Code 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 You can configure the mapping relationship-->
    <!--2 The specific mybatis sql, note that the id of the sql tag in this place is the corresponding method name in the corresponding mapper.java file interface -->
</mapper>

Error:

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 Several pits:

  The error that usually occurs when the version of the jar package is inconsistent, such as here, because the version of spring-core should be consistent with other spring packages, but I don't have it here, but a few versions lower. Generally, this error is basically two Reasons: 1⃣️The jar version is wrong, 2⃣️The corresponding jar package is missing:

  

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]

 

  There is no corresponding namespace configured in *mapper.xml:

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; 文件提前结束。

 You need to add the configuration marked by the red department in the above 3. This is the mapper.java file of the corresponding interface. Generally, these two are placed under one package.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326440426&siteId=291194637