Spring + SpringMVC + Druid + MyBatis flexible backend solution, springmvcmybatis

Source: Internet
Author: User

Spring + SpringMVC + Druid + MyBatis flexible backend solution, springmvcmybatis

Life is endless.

I think it is growth.

Two months ago, I moved on to a set of JavaWeb backend solutions built using open-source frameworks.

Spring + SpringMVC + Druid + JPA (Hibernate impl) provides you with a secure backend Solution

After being introduced to the project team, after several rounds of polishing, the project team is also very easy to understand.

Recently, I switched Hibernate from the project to Mybatis. After all, people need to be fresh.

I am familiar with Hibernate earlier than MyBatis. As the two most popular ORM frameworks, I personally think many of them share the same idea.

However, the unique ResultMap concept of MyBatis enables more detailed SQL adjustment and optimization.

Hibernate is superior to MyBatis in terms of the development community, version update speed, and supported tools.

Project Git address: https://git.oschina.net/LanboEx/sdm

1. Overall solution Overview

The Controller layer accepts frontend parameters and responds to requests, carrying data to jump pages.

ServiceInter is injected into the Controller layer, and ServiceImpl layer Organizes business data.

Mybatis er is injected into the ServiceImpl layer to access data.

Similar to Hibernate, the entire dao layer can be generated by tools. The org. mybatis. generator plug-in is used in the project.

Web. xmlpom. xml2.

I will not talk about it here. Below we will look at several deep pitfalls.

If you have encountered these problems before and have a better solution than me, please kindly advise.

A. MapperScannerConfigurer initialization in advance causes the spring injection configuration file to fail.

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.rambo.sdm.dao.inter"/>        <property name="sqlSessionFactory " ref="sessionFactory"/>    </bean>

It is expected that Spring can scan the Mapper interface class to load Mapper. xml, automatically generate the proxy class, and inject it into the corresponding ServiceImpl.

At the beginning of the configuration, I found that Spring could not load the information in the configuration file normally.

That is, the content in the properties file cannot be obtained using expressions such as $ {jdbc. username.

After several failed attempts, it was found that MapperScannerConigurer is in the parsing and loading bean definition phase. If sqlSessionFactory is set at this time.

Some classes will be initialized in advance, and PropertyPlaceholderConfigurer has not had time to replace the variables in the definition, resulting in the expression being copied as a string.

Replace sqlSessionFactory with sqlSessionFactoryBeanName. The configuration is as follows:

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.rambo.sdm.dao.inter"/>        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>    </bean>

B. Automatic Generation of primary keys for data tables at the dao Layer

After compiling a project example and running it, the system prompts that the UUID is not NUll.

In the automatically generated mapper. xml, You need to configure the primary key (auto-incrementing sequence/uuid) by yourself, which is indeed a bit low.

Configure the UUID in mapper. xml as follows:

    <selectKey keyProperty="uuid" resultType="String" order="BEFORE">         select replace(uuid(),'-','')  UUID    </selectKey>

During project promotion, After generating the data table configuration file, you must manually add a primary key generation policy to the new method in mapper. xml. This is not only tedious but also has a high probability of problems.

Try to find out if there is any unified configuration, and find one, but still not perfect. Unified configuration in the place where generatorConfig. xml generates tables:

        <table tableName="user" domainObjectName="UserPO">            <generatedKey column="uuid" sqlStatement="SELECT REPLACE(UUID(),'-','') UUID FROM DUAL"/>        </table>

When the primary key generation policy uses SQL statements, it is doomed that Mybatis cannot be perfect in database transplantation.

C. After maven compilation, the xml file is not compiled into the class folder.

The xml of the compilation directory to be output in the project consists of two parts: mapper. xml of each data table and xml of the framework.

Compile and run: org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)

Is it not bound? After half a day, I found that mapper. xml was not compiled into the corresponding folder.

Add sub-resource sub-nodes under maven build --> resources node:

            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.xml</include>                </includes>                <filtering>true</filtering>            </resource>

After the child node is added, mapper. xml is added to the corresponding folder, but the files under Resources in the project are not compiled to classes as before.

Maven build --> Add the sub-resource sub-node under the resources node to solve the problem:

            <resource>                <directory>src/main/resources</directory>                <includes>                    <include>**/*.xml</include>                    <include>**/*.properties</include>                </includes>                <filtering>true</filtering>            </resource>

D. When the jetty plug-in starts a web project, the mybatis reverse engineering plug-in is also started.

When jetty: run is used to start a web project, there will always be some inexplicable problems.

The error message is as follows: java. lang. IllegalArgumentException: Result Maps collection already contains value for com. rambo. sdm. dao. inter. UserPOMapper. BaseResultMap

Following the startup log, we found that the mybatis. generator plug-in runs first and reverse the database engineering every time jetty: run.

If reverse generation exists, skip this step.

The running mechanism of the generator plug-in is still a bit problematic. The class is skipped, but the configuration file will append the content, so the above error is returned.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.