After the basic framework is set, it is the problem of database. In the framework, we choose the now popular Mybatis framework.

Mybatis integration with Spring is simple:

<! SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <! Instantiate the sqlSessionFactory using the above configured data source and SQL mapping file --> <property name="dataSource" ref="dataSource"/ > <! --> <property name="mapperLocations" value="classpath:/mybatis/*.xml" />
    </bean>
Copy the code

However, some problems and doubts have been solved in the process of use

Mybatis cannot use a table name as a parameter

This is a bit of a bummer. Stems from the desire to write a common delete interface. Delete the specified id from the specified table.

According to baidu’s results, Mybatis cannot take the table name as a parameter.

The final solution was to mix spring’s jdbcTemplate. Just one configuration:

<bean id = "jdbcTemplate" class = 
"org.springframework.jdbc.core.JdbcTemplate"> 
<property name = "dataSource" ref="dataSource"/> 
</bean>
Copy the code

It’s easy to use.

The jdbcTemplate simply encapsulates JDBC operations, making it easy to execute entity-independent SQL.

By design, there are only two types of id: increment and string, so the public delete method is quite simple:

public void commonDelete(String table,Object id){
        if(id instanceof String)
            jdbcTemplate.execute("delece from "+table+" where id=\""+id+"\" ");
        else
            jdbcTemplate.execute("delete from "+table+" where id="+id+"");
    }
Copy the code

##2: Beans

Mybatis requires a bunch of beans to do all kinds of storage: query conditions, query results, database entities blah blah blah…

When I first encountered this question, it really scared me. It’s like seeing a project where 80 out of 100 classes are beans…

For now, this is an inevitable problem. So get ready to use mybatis and get ready to face a lot of beans.

But then again, with all the code completion, generating a Bean isn’t that much of a hassle. But organizing these beans is the tricky part. To avoid future confusion, we have a set of rules for naming beans with database table names as base entities, identified by prefixes, suffixes, and package names.

##3, unexpected learning: Mybatis joint query

In the bean when the accident baidu to Mybatis joint query, found or quite powerful.

The general process is like this:

(1) Define the result Bean of a federated query, for example, there are now two beans, User and Blog, do a UserBlogs

Then copy the parameters of User in UserBlogs and add List blogs.

(2) Mapping. The main reason for this is to avoid having the same name as the parameter in the User and the parameter in the Blog, which would cause the framework to not know who to hand the field to

(3) Query according to the mapping. Use a simple federated query SQL statement. Note that the name of the field in the query result must be given an alias, which corresponds to the mapped field.

You can then use SQL to perform a federated query and get a user’s information with a list of blogs.

Specific code or look at somebody else’s blog is better:

http://www.cnblogs.com/luxiaoxun/p/4035276.html

##4, beans again

Sometimes there are too many database fields, write insert, modify statements and so on is quite annoying…

So I made a simple insert statement generator:

@param * @param */ public static void genAddSql(String table,Class bean) { StringBuilder builder =new StringBuilder(); builder.append("insert into ").append(table);
        StringBuilder preffix = new StringBuilder("(");
        StringBuilder surffix = new StringBuilder("(");
        Method[] methods = bean.getMethods();
        for (Method method : methods) {
            String name = method.getName();
            if(name.equals("setId"))
                continue;
            if(name.startsWith("set")){
                preffix.append(name.substring(3).toLowerCase()).append(",");
                surffix.append("# {").append(name.substring(3).toLowerCase()).append("},");
            }
        }
        preffix.deleteCharAt(preffix.length()-1);
        surffix.deleteCharAt(surffix.length()-1);
        preffix.append(") values ");
        surffix.append(")"); builder.append(preffix).append(surffix); System.out.println(builder); } @param * @param */ public static void genUpdateSql(String table,Class bean) { StringBuilder builder =new StringBuilder(); builder.append("update ").append(table).append(" set ");
        Method[] methods = bean.getMethods();
        for (Method method : methods) {
            String name = method.getName();
            if(name.equals("setId"))
                continue;
            if(name.startsWith("set")){
                String p = name.substring(3).toLowerCase();
                builder.append("<if test=\"#{").append(p).append("}! =null\">");
                builder.append(p).append("= # {").append(p).append("}").append("</if>");
            }
        }
        builder.append("where id=#{id}");
        System.out.println(builder);
    }
Copy the code