Before using the Mybtis operation database in the project is to write SQL manually, for my SQL level is not very good for people to die of pain; dynamic Query SQL I mean I can't write it yet!
Fortunately, the tool that uses the reverse generation of database tables can help me solve most of the SQL; (MyBatis generator you know)
The first use of reverse generation can help us to automatically generate entity classes, DAO interfaces, DAO mapping files;
The DAO mapping file resembles the following:
<?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= "Bz.sunlight.dao.UserMapper" >
<resultmap id= "Baseresultmap" type= "Bz.sunlight.entity.User" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
<id column= "id" property= "id" jdbctype= "VARCHAR"/>
<result column= "name" property= "name" jdbctype= "VARCHAR"/>
<result column= "password" property= "password" jdbctype= "VARCHAR"/>
</resultMap>
<sql id= "Example_where_clause" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
<where >
<foreach collection= "Oredcriteria" item= "Criteria" separator= "or" >
<if test= "Criteria.valid" >
<trim prefix= "(" suffix= ")" prefixoverrides= "and" >
<foreach collection= "Criteria.criteria" item= "criterion" >
<choose >
<when test= "Criterion.novalue" >
and ${criterion.condition}
</when>
<when test= "Criterion.singlevalue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test= "Criterion.betweenvalue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondvalue}
</when>
<when test= "Criterion.listvalue" >
and ${criterion.condition}
<foreach collection= "Criterion.value" item= "ListItem" open= "(" close= ")" separator= "," >
#{listitem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id= "Update_by_example_where_clause" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
<where >
<foreach collection= "Example.oredcriteria" item= "Criteria" separator= "or" >
<if test= "Criteria.valid" >
<trim prefix= "(" suffix= ")" prefixoverrides= "and" >
<foreach collection= "Criteria.criteria" item= "criterion" >
<choose >
<when test= "Criterion.novalue" >
and ${criterion.condition}
</when>
<when test= "Criterion.singlevalue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test= "Criterion.betweenvalue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondvalue}
</when>
<when test= "Criterion.listvalue" >
and ${criterion.condition}
<foreach collection= "Criterion.value" item= "ListItem" open= "(" close= ")" separator= "," >
#{listitem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id= "Base_column_list" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
ID, name, password
</sql>
<select id= "Selectbyexample" resultmap= "Baseresultmap" parametertype= "Bz.sunlight.entity.UserExample" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Select
<if test= "distinct" >
Distinct
</if>
<include refid= "Base_column_list"/>
From user
<if test= "_parameter! = null" >
<include refid= "Example_where_clause"/>
</if>
<if test= "Orderbyclause! = null" >
ORDER BY ${orderbyclause}
</if>
</select>
<select id= "Selectbyprimarykey" resultmap= "Baseresultmap" parametertype= "java.lang.String" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Select
<include refid= "Base_column_list"/>
From user
WHERE id = #{id,jdbctype=varchar}
</select>
<delete id= "Deletebyprimarykey" parametertype= "java.lang.String" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Delete from user
WHERE id = #{id,jdbctype=varchar}
</delete>
<delete id= "Deletebyexample" parametertype= "Bz.sunlight.entity.UserExample" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Delete from user
<if test= "_parameter! = null" >
<include refid= "Example_where_clause"/>
</if>
</delete>
<insert id= "Insert" parametertype= "Bz.sunlight.entity.User" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
INSERT into user (ID, name, password
)
VALUES (#{id,jdbctype=varchar}, #{name,jdbctype=varchar}, #{password,jdbctype=varchar}
)
</insert>
<insert id= "insertselective" parametertype= "Bz.sunlight.entity.User" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
INSERT INTO user
<trim prefix= "(" suffix= ")" suffixoverrides= "," >
<IF test= "id! = NULL" >
Id
</if>
<if test= "Name! = NULL" >
Name
</if>
<if test= "Password! = null" >
Password
</if>
</trim>
<trim prefix= "VALUES (" suffix= ")" suffixoverrides= "," >
<IF test= "id! = NULL" >
#{id,jdbctype=varchar},
</if>
<if test= "Name! = NULL" >
#{name,jdbctype=varchar},
</if>
<if test= "Password! = null" >
#{password,jdbctype=varchar},
</if>
</trim>
</insert>
<select id= "Countbyexample" parametertype= "Bz.sunlight.entity.UserExample" resulttype= "Java.lang.Integer" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Select COUNT (*) from user
<if test= "_parameter! = null" >
<include refid= "Example_where_clause"/>
</if>
</select>
<update id= "updatebyexampleselective" parametertype= "Map" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Update user
<set >
<if test= "Record.id! = null" >
id = #{record.id,jdbctype=varchar},
</if>
<if test= "Record.name! = null" >
name = #{record.name,jdbctype=varchar},
</if>
<if test= "Record.password! = null" >
Password = #{record.password,jdbctype=varchar},
</if>
</set>
<if test= "_parameter! = null" >
<include refid= "Update_by_example_where_clause"/>
</if>
</update>
<update id= "updatebyexample" parametertype= "Map" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Update user
Set id = #{record.id,jdbctype=varchar},
name = #{record.name,jdbctype=varchar},
Password = #{record.password,jdbctype=varchar}
<if test= "_parameter! = null" >
<include refid= "Update_by_example_where_clause"/>
</if>
</update>
<update id= "updatebyprimarykeyselective" parametertype= "Bz.sunlight.entity.User" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Update user
<set >
<if test= "Name! = NULL" >
name = #{name,jdbctype=varchar},
</if>
<if test= "Password! = null" >
Password = #{password,jdbctype=varchar},
</if>
</set>
WHERE id = #{id,jdbctype=varchar}
</update>
<update id= "Updatebyprimarykey" parametertype= "Bz.sunlight.entity.User" >
<!--
warning-@mbggenerated
This element is a automatically generated by MyBatis Generator, does not modify.
This element is generated on Sat 23:42:15 CST 2016.
-
Update user
Set name = #{name,jdbctype=varchar},
Password = #{password,jdbctype=varchar}
WHERE id = #{id,jdbctype=varchar}
</update>
</mapper>
Using these generated SQL statements can basically solve all the stories in our single-table query, and we don't have to go to handwritten SQL (too fucked up), is it a bit back to the other big frame?
For example:
I want to add a customer:
Then the code on the service layer can be written like this:
@Override
public void Saveuser (User record) {
Usermapper.insert (record);
}
Directly using the inverted Insert method to insert;
You may feel that there is not much meaning: This SQL statement is too easy to write;
What about the query? According to the conditions of the query, the conditions are more than the case? Update, need to update it dynamically?
Inquire:
@Override
Public list<user> queryuserbyname (String name) {
Userexample example=new userexample ();
Example.createcriteria (). Andnameequalto (name); I just need to add the query conditions here, there are a lot of query conditions to choose from, thecriteria is not very familiar with the object Ah!
list<user> users = usermapper.selectbyexample (example);
return users;
}
This will be based on the name of the query services written, but also can have like,between and other methods to choose;
Update:
Sometimes the data submitted by the form is not very complete, and the update needs to write dynamic SQL to prevent the database data from being updated to null;
Now we can do this:
@Override
public void Updatebyid (User user,string ID) {
Userexample example=new userexample ();
Example.createcriteria (). Andidequalto (ID);
Usermapper.updatebyexampleselective (user, example);
}
Dynamic SQL has been generated in reverse, so we just need to encapsulate the condition that needs updating into the example class.
Of course, the Multi-table association can not be used for the time being, about the multi-table association may follow my other blog post;
In this way, is not mybatis also achieve the full package operation;
MyBatis Single-table operation for full Java code encapsulation