1. 2. xml file using an interface in conjunction with solving the difference field attributes do not match the query list 3. 4. 5. # $ and how the generated returns id add objects

2019-8-29 syllabus

1. Use Interface binding xml file

2. solve fields and attributes do not match

3. list query

4. $ and # differentiated

5. How to add objects generated when id returned

 

 

1. Use Interface binding xml file

1) Create a dao package, create an interface in the package, the interface and mapping files to match

2) the mapping file namespace must package and the name of the interface and the interface where the same

3) label id certain name of the interface and control methods

4) test:

// get the interface implementation class

UserDao userDao=session.getMapper(UserDao.class);

// call the method

User user=userDao.getUser(1);

2. solve fields and attributes do not match

Way1 : SQL statement from the alias alias consistent with the entity class, the name for the field

Way2 : Use resultMap tags to define the correspondence between the entity class field

<select id="getOrder" resultMap="myMap">

      select * from Orders where Order_id=#{id}   

</select>

<resultMap type="com.zhiyou.clg.bean.Order" id="myMap">

<id column="order_id" property="id"/>

<result column="order_no" property="no"/>

<result column="order_price" property="price"/>

</resultMap>

3. list query

3.1 such as class according to the above mentioned id (information with the teacher) or many-to-one class information inquiry

Way1 : SQL statement using a linked table query

* Entity class need to add attributes (data type of the entity class name)

* Mapping files used in association label (properties: Property attribute name (added entity class), the javaType ( full class name of the entity class added property is located ) )

<select id="getClazz" resultMap="myMap">

      select * from class c,teacher t where c.teacher_id=t.t_id and c_id=#{cid}   

</select>

<resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap">

<id column="c_id" property="id"/>

<result column="c_name" property="name"/>

<result column="teacher_id" property="teacherid"/>

<association property="teacher" javaType="com.zhiyou.clg.bean.Teacher">

<id column="t_id" property="id"/>

<result column="t_name" property="name"/>

</association>

</resultMap>

 

 

 

Way2 : nested queries (query results to conditions other queries)

* Entity class need to add attributes (data type of the entity class name)

* Map file using the association tags (attributes: Property , the javaType (to the query result, at this time as a query), column , select (statement of another map ID , i.e., the other select tag ID ))

<select id="getClazz" resultMap="myMap2">

      select * from class where c_id=#{cid}   

</select>

<select id="getTeacher" parameterType="int" resultType="com.zhiyou.clg.bean.Teacher">

   select t_id id,t_name name from teacher where t_id=#{tid}   

</select>

<resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap2">

<id column="c_id" property="id"/>

<result column="c_name" property="name"/>

<result column="teacher_id" property="teacherid"/>

<association property="teacher"   javaType="com.zhiyou.clg.bean.Teacher"

column="teacher_id" select="getTeacher">

<id column="t_id" property="id"/>

<result column="t_name" property="name"/>

</association>

</resultMap>  

3.2 For example, according to the class id Query class information (class students with information) many

Way1 : SQL statement using a linked table query

* Entity class add attributes (data type is set, the generic name for the entity class)

* Mapping files using a collection tags ( property: Property (attribute name added), ofType (full set of generic class name of the entity class) )

 <select id="getClazz" resultMap="myMap3">

select *from class c,teacher t,student s where c.c_id=s.class_id and c.teacher_id=t.t_id and c.c_id=#{cid}  

</select>

<resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap3">

<id column="c_id" property="id"/>

<result column="c_name" property="name"/>

<result column="teacher_id" property="teacherid"/>

<association property="teacher" javaType="com.zhiyou.clg.bean.Teacher">

<id column="t_id" property="id"/>

<result column="t_name" property="name"/>

</association>

<collection property="students" ofType="com.zhiyou.clg.bean.Student">

<id column="s_id" property="id"/>

<result column="s_name" property="name"/>

<result column="class_id" property="cid"/>

</collection>

</resultMap> -->

 

 

 

 

 

 

 

Way2 : nested queries (query results to conditions other queries)

* Entity class add attributes (data type is set, the generic name for the entity class)

* Mapping files using a collection label ( properties: Property , ofType , column , the SELECT)

<select id="getClazz" resultMap="myMap4">

      select * from class where c_id=#{cid}   

</select>

<select id="getTeacher" parameterType="int" resultType="com.zhiyou.clg.bean.Teacher">

   select t_id id,t_name name from teacher where t_id=#{tid}   

</select>

<select id="getStudent" parameterType="int" resultType="com.zhiyou.clg.bean.Student">

select s_id id,s_name name,class_id cid from student where class_id=#{cid}

</select>

<resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap4">

<id column="c_id" property="id"/>

<result column="c_name" property="name"/>

<result column="teacher_id" property="teacherid"/>

<association property="teacher"   javaType="com.zhiyou.clg.bean.Teacher"

column="teacher_id" select="getTeacher">

<id column="t_id" property="id"/>

<result column="t_name" property="name"/>

</association>

<collection property="students" ofType="com.zhiyou.clg.bean.Student"

 column="c_id" select="getStudent">

<id column="s_id" property="id"/>

<result column="s_name" property="name"/>

<result column="class_id" property="cid"/>

</collection>

</resultMap>

 

 

4. $ and # differentiated

$: Does not add to the content parsing ""  He is the sql stitching statement , there is sql injection flaws.

When an incoming table structure is used, when the incoming column or table name can be used

order by group by time with $

$: Generally used for incoming database objects, such as table or column names

#: Add content parsing "" It sql using placeholders, prevent sql injection.

Use # Do not use $

#: General value is passed

5.  How to add objects generated when id returned

<insert  id=”addUser” parameterType=”com.zhiyou.clg.bean.User” useGeneratedKeys="true" keyProperty="id">

</insert>

= useGeneratedKeys "to true"   : indicates the use of auto-generated id

= the keyProperty "id"    : the generated id assigned to an entity corresponding to the attribute class

Both joint use of the above

After generally it is used to add a single number (automatically generated) then immediately added product

 

Guess you like

Origin www.cnblogs.com/jingmochen/p/11432814.html