Welcome to join 214821336!!!
Statement: With mybatis, you don't need to write the implementation class of dao. In order to maintain the integrity of the three-tier architecture, you add the implementation class of interface dao.
1. Increase
- sql statement --> has default return type, usually int.
<!-- New Classes -->
<insert id="addClass" parameterType="Clazz">
<!-- Use Sequences -->
<selectKey keyProperty="cid" order="BEFORE" resultType="int">
select seq_class1.nextval from dual
</selectKey>
insert into class1 values(#{cid},#{cname})
</insert>
- Corresponding methods
/**
* New class, where the return value type int/void can be
*/
public int addClass(Clazz clazz) {
SqlSession session = super.getSqlSession();
return session.insert("com.dao.IClassDao.addClass",clazz);
}
2. Modification
Modifications need to be made in two steps
Step 1: According to the id of the entity to be modified, all the information of the entity is queried from the database.
- sql statement --> need to specify return value type
<!-- according to id Getting Class Information -->
<select id="getClassById" parameterType="int" resultType="Clazz">
select * from class1 where cId=#{cid}
</select>
- Corresponding methods
/**
* All information about the class must be obtained before revision.
*/
public Clazz getClassById(int cId) {
SqlSession session = super.getSqlSession();
return session.selectOne("com.dao.IClassDao.getClassById",cId);
}
Step 2.2: The modified entity is re-inserted into the database
- sql statement --> need to specify parameter type, default return type is int, do not declare return value in sql statement, otherwise error will be reported
<!-- Modifying Class Information -->
<update id="updateClass" parameterType="Clazz">
update class1 set cName=#{cname} where cId=#{cid}
</update>
- Corresponding methods
/**
* Modify the class information, where the return value type int/void can be used
*/
public int updateClass(Clazz clazz) {
SqlSession session = super.getSqlSession();
return session.update("com.dao.IClassDao.updateClass",clazz);
}
3. Query
Queries are mainly based on query conditions: generally divided into id-based queries; queries based on name (the field name in the table here can be repeated); combination queries; unconditional queries
Type 1: Query according to id
The example is the same as step 1 of the modification.
Type 2: Query by name
- sql statement --> need to specify parameter type and return value type
<!-- Get student information by name -->
<select id="getStudentsBysName" parameterType="String" resultType="Student">
select * from student1 where sName=#{sname}
</select>
- Corresponding methods
/**
* Get student information by name
*/
public List<Student> getStudentsBysName(String sName) {
SqlSession session = super.getSqlSession();
return session.selectList("com.dao.IStudentDao.getStudentsBysName",sName);
}
Summary: The difference between type 1 and type 2 is that the return value of the method is different, the former is unique, and the latter is generally a set.
Type 3: Combination Query
Specify mapping relationships and types --> map keys and values whose names are consistent with the properties of entity classes, otherwise runtime errors will occur
<parameterMap type="java.util.Map" id="paramUserMap">
<parameter property="userName" javaType="String"></parameter>
<parameter property="userPass" javaType="String"></parameter>
</parameterMap>
- sql statement --> need to specify parameter type and return value type
<!-- id The value of this class corresponds to the method name in that class -->
<select id="getUsersByNamePass" parameterMap="paramUserMap" resultType="User">
select * from user1 where 1=1
<if test="userName != null and userName != ''">
and userName=#{userName}
</if>
<if test="userPass != null and userPass != '' ">
and userPass=#{userPass}
</if>
</select>
- The corresponding method - > passes in a map set
/**
* Composite Query: Query User Information Based on User Name and Identity
*/
public List<User> getUsersByNamePass(Map user) {
SqlSession session = super.getSqlSession();
return session.selectList("com.dao.IUserDao.getUsersByNamePass", user);
}
Type 4: Get all the data in the table
- sql statement --> need to specify return value type
<!-- Get all class information -->
<select id="getAllClasses" resultType="Clazz">
select * from class1
</select>
- Corresponding methods
/**
* Get all the classes
*/
public List<Clazz> getAllClasses() {
SqlSession session = super.getSqlSession();
return session.selectList("com.dao.IClassDao.getAllClasses");
}
4. Delete
Deletion can be divided into single data deletion and multiple data deletion.
4.1 Type 1: Single data deletion
- sql statement --> need to specify parameter type (default return int type)
<!-- Delete student information -->
<delete id="deleteStudentBysId" parameterType="int">
delete from student1 where sId=#{sid}
</delete>
- Corresponding methods
/**
* Delete student information
*/
public int deleteStudentBysId(int sId) {
SqlSession session = super.getSqlSession();
return session.delete("com.dao.IStudentDao.deleteStudentBysId", sId);
}
4.2 Type 2: Delete Multiple Data - Untested
- sql statement --> need to specify parameter type (default return int type)
<delete id = "delete" parameterType = "java.util.List">
delete from tests where id in
<foreach collection="list" item = "item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>