7.2 Mybatis
Usually we are using JDBC to access the database, in addition to write their own SQL, you must also operate Connection, Statement, ResultSet class which is only an auxiliary means. Moreover, different access to the table, but also write a lot of similar code, it is tedious and boring.
So after using Mybatis, only need to provide their own SQL statements, other work, such as establishing a connection, Statement, JDBC-related exception handling, etc., to Mybatis do it, those repetitive tasks Mybatis also gave out to do, and we just we need to focus on the CRUD and other operational level, while the technical details are encapsulated in our sight.
Creating mybatis master configuration file in the src directory mybatis-config.xml (equivalent to hibernate.cfg.xml)
1. Application to find data Mybatis
public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession(); List<Category> cs=session.selectList("listCategory"); for (Category c : cs) { System.out.println(c.getName()); }
2. mybatis got data from the database
2.1 Which database located by mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.how2java.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="admin"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/how2java/pojo/Category.xml"/> </mappers> </configuration>
2.2 implementation of the corresponding select statement by Category.xml
<?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="com.how2java.pojo"> <select id="listCategory" resultType="Category"> select * from category_ </select> </mapper>
2.3 Based on the returned database record Category.xml encapsulated object in Category
Over 2.4 Category Category objects contained in a set of
Returns a set of Category 2.5
3.mappper in CRUD.
Mappper wherein the label corresponds to invoke methods Dao layer. In running the project, if id do not have access to. Or repetition, injected bean will be reported error
indicates the type of entity or parameterType required injection
resultType represent an entity of type SQL queries or calls returned
<?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="com.how2java.pojo"> <insert id="addCategory" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="deleteCategory" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="getCategory" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="updateCategory" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="listCategory" resultType="Category"> select * from category_ </select> </mapper>
3.1 Fuzzy query
mysql:select * from category_ where name like concat('%',#{0},'%')
oracle:select * from category_ where name like '%'||#{0}||'%'
More than 320 criteria query
In dao plurality @parm layer parameters, and according to the conditions of the plurality of query parameters map
<select id=
"listCategoryByIdAndName"
parameterType=
"map"
resultType=
"Category"
>
select * from category_ where id> #{id} and name like concat(
'%'
,#{name},
'%'
)
</select>
<select id="listProduct" resultType="Product"> select * from product_ <if test="name!=null"> where name like concat('%',#{name},'%') </if> </select>
4.2 where labels
Conditions typically used when a plurality of
<select id="listProduct" resultType="Product"> select * from product_ <where> <if test="name!=null"> and name like concat('%',#{name},'%') </if> <if test="price!=null and price!=0"> and price > #{price} </if> </where> </select>
4.3 set label; and where labels Similarly, in the update statement will encounter related problems in a number of fields. In this case, you can use the set label:
<update id="updateProduct" parameterType="Product" > update product_ <set> <if test="name != null">name=#{name},</if> <if test="price != null">price=#{price}</if> </set> where id=#{id} </update>
4.4trim used to customize the desired function, such as where the label can be used to replace the
run set the tag code, the effect is the same.
<select id="listProduct" resultType="Product"> select * from product_ <trim prefix="WHERE" prefixOverrides="AND |OR "> <if test="name!=null"> and name like concat('%',#{name},'%') </if> <if test="price!=null and price!=0"> and price > #{price} </if> </trim> </select> <update id="updateProduct" parameterType="Product" > update product_ <trim prefix="SET" suffixOverrides=","> <if test="name != null">name=#{name},</if> <if test="price != null">price=#{price}</if> </trim> where id=#{id} </update>
4.5 Mybatis else there will be no label, the label may be used when otherwise to achieve this effect.
<select id="listProduct" resultType="Product"> SELECT * FROM product_ <where> <choose> <when test="name != null"> and name like concat('%',#{name},'%') </when> <when test="price !=null and price != 0"> and price > #{price} </when> <otherwise> and id >1 </otherwise> </choose> </where> </select>
4.6 foreach tag, the query is equivalent to running a set of conditions in use.
<select id="listProduct" resultType="Product"> SELECT * FROM product_ WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
4.7 bind tag, bind label like to do it again string concatenation, convenient subsequent use
<!-- 本来的模糊查询方式 --> <!-- <select id="listProduct" resultType="Product"> --> <!-- select * from product_ where name like concat('%',#{0},'%') --> <!-- </select> --> <select id="listProduct" resultType="Product"> <bind name="likename" value="'%' + name + '%'" /> select * from product_ where name like #{likename} </select>