Java development in those small problems can not be ignored -1

No. Honestly public write quite a long time, and therefore know a lot of friends, what advice has, in short, more or less help to some people. Recently like a long time, I should write back some of what types of articles?

1, more theoretical?

In an article inside to feel overview of some of the more complex theory, say say finished Leaving aside, as a reader most certainly no patience to watch, and watching the absorption will not be many, this kind of thing or have assiduously by the way might be better.

2, partial code hierarchies?

Prior also wrote several articles inside the code a lot, but in fact very few people really seriously studied those codes, there is no proven, because we might be the way to work, during a meal or look at, so just a glance too.

So I was thinking, what we can share some of the things that can make everyone in high school stuff fragmented time can be implemented, I think these things have to involve the following facets, and rapid implementation aspects, looked at something, can quickly digest, and can be implemented into the code, I think is the best.

Ado, a few days to share a few small problems usually encountered in the development;

First, the empty set Java provided in the end is not helpful?

This is after I read a few articles, and then combine the code in their own projects as understood, the first thing is certainly useful . Let's look at a piece of code.

// 伪代码
public List<MallProvincesPO> listProvincesAllDao() {

        // 从数据库获取省份信息
        List<MallProvincesPO> provincesPOList = provincesMapper.selectAll();

        if (provincesPOList == null || provincesPOList.size() <= 0) {
                return null;
        }
        return provincesPOList;
}复制代码

Now basically based interface if the front does not do any RESTful style, if this method available to others, then certainly there will be a problem, there may be a null pointer exception (really experience) to go fetch the data collection front end, non-empty judgment, taken directly from the collection, then return null when the time will inevitably result in a null pointer exception.

So not query the data, we should judge the collection is not for null, if null we should return an empty set, if we direct a new ArrayList is actually very wasteful, this time provided in the Java Collections. emptyList (); on rafts handy, it creates a not allowed to add, delete, modify the empty set;

The revised Code

public List<MallProvincesPO> listProvincesAllDao() {

        // 从数据库获取省份信息
        List<MallProvincesPO> provincesPOList = provincesMapper.selectAll();

        if (provincesPOList == null) {
                return Collections.emptyList();
        }
        return provincesPOList;
}复制代码

Two, Mybatis bulk modification statements

This is a problem I encountered this morning, batch modify our previous batch of data is changed to a field with a value, such as batch tombstone, status value is actually the data to be deleted or changed to 0 other values, but that we are like this, a good few editing data, then the value of each data is not the same, in order to reduce the number of connections to the database, we have to be modified directly through mybatis dynamic sql;

code show as below:

<update id="updateAll" parameterType="java.util.List">

    <foreach collection="list" separator=";" item="item">

            update product_specification

            <set>

                    <if test="item.status != null and item.status != ''">
                            status = #{item.status},
                    </if>

                    <if test="item.updateTime != null and item.updateTime != ''">
                            update_time = #{item.updateTime},
                    </if>
            </set>

            where id = #{item.id}
    </foreach>
</update>复制代码

But the statement point of view, really no problem, but I am in the process of commissioning in ( on SpringBoot project ), is really tough whiteboard, it has been an error. Grammar has been said that there is a problem, please consult the manual.

Error message:

### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update mall_product_specification复制代码

Problem Cause:
This is because mysql does not support batch modify this way of operation, so when we modify batch data and they will report this error, if you modify a single piece of data, the error does not occur.

Solution: configuration parameters after the connection to the database & allowMultiQueries = true to
file

Guess you like

Origin juejin.im/post/5dc55a17518825592c566851