当前位置:网站首页>Mybaits: common database operations (Neusoft operations)

Mybaits: common database operations (Neusoft operations)

2022-06-22 17:16:00 Liu Chu, Ge Nian

1. Common database operations

The teacher didn't give the database and code for the class , The following is not running , If you don't understand, you can skip this article , Learn the next one

1.1. Multiconditional query

<select id="listEmp" parameterType="Emp" resultType="Emp">
    select * 
    from emp 
    where job like concat('%',#{job},'%') and deptno=#{deptno} 
    order by empno
</select>

Look up the employee table ,job contain Emp.job Chinese characters and deptno yes Emp.deptno The employees' , Finally, according to empno Sort

Emp emp = new Emp();
emp.setJob(" the ");
emp.setDeptno(20);
List<Emp> list = mapper.listEmp(emp);
for(Emp e : list) {
    
    System.out.println(e);
}

Be careful :

  1. parameterType only one . therefore , When there are multiple parameters, use the object to pass values ( This is it. Input mapping ).
  2. #{} What is written in the book is the attribute name of the entity object , So be strictly case sensitive .

1.2. Escape character query

because <( Less than no. ) Is a tag keyword , Therefore, the less than number cannot be identified . therefore MyBatis Some escape characters are designed in , To replace some special characters :

Code Escape character explain
&lt;< Less than
&gt;> Greater than
&amp;& And
&apos; Single quotation marks
&quot;" Double quotes
<select id="listEmpBySal" parameterType="double" resultType="Emp">
    select * from emp where sal &lt; #{sal} order by empno
</select>
List<Emp> list = mapper.listEmpBySal(2000.0);
for(Emp e : list) {
    
    System.out.println(e);
}

1.3. Returns a single valued query

<select id="listEmpCount" resultType="int">
    select count(*) from emp
</select>
int count = mapper.listEmpCount();
System.out.println(count);

Be careful : Only one row and one column are returned ,resultType To use the basic data type

1.4. Insert ( Do not get the primary key )

<insert id="insertEmp1" parameterType="Emp">
    insert into emp(ename,job,hiredate,deptno)
    values(#{ename},#{job},#{hiredate},#{deptno})
</insert>
SqlSession sqlSession = Util.getSqlSessionFactory().openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = new Emp();
emp.setEname(" Zhang San ");
emp.setJob(" staff member ");
emp.setHiredate("2020-09-04");
emp.setDeptno(10);
int result = mapper.insertEmp1(emp);
sqlSession.commit();                  // Be careful : want commit Submit 
System.out.println(result);

Be careful : Any addition, deletion or modification will be returned int value , Indicates the number of rows affected . however ,insert The label cannot be written resultType attribute

1.5. Insert ( Get primary key )

<insert id="insertEmp2" parameterType="Emp">
    <selectKey keyProperty="empno" resultType="int" order="AFTER">
        select LAST_INSERT_ID()
    </selectKey>
    insert into emp(ename,job,hiredate,deptno)
    values(#{ename},#{job},#{hiredate},#{deptno})
</insert>
SqlSession sqlSession = Util.getSqlSessionFactory().openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = new Emp();
emp.setEname(" Zhang San ");
emp.setJob(" staff member ");
emp.setHiredate("2020-09-04");
emp.setDeptno(10);
int result = mapper.insertEmp2(emp);
sqlSession.commit();
System.out.println(result);
System.out.println(emp.getEmpno());      // Get the returned primary key 

Be careful :

  1. selectKey In the tag select LAST_INSERT_ID() Statement to get the generated primary key
  2. selectKey In the tag keyProperty The attribute is the primary key name ,MyBatis The obtained primary key will be automatically encapsulated to this property .
  3. order There are two kinds of values :BEFORE、AFTER
    BEFORE: Get primary key first , And then execute insert; such as Oracle database .
    AFTER: Execute first insert, Then get the primary key ; such as MySql database .

1.6. Insert ( Get primary key )

<insert id="insertEmp3" parameterType="Emp" useGeneratedKeys="true" keyProperty="empno">
    insert into emp(ename,job,hiredate,deptno)
    values(#{ename},#{job},#{hiredate},#{deptno})
</insert>
SqlSession sqlSession = Util.getSqlSessionFactory().openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = new Emp();
emp.setEname(" Zhang San ");
emp.setJob(" staff member ");
emp.setHiredate("2020-09-04");
emp.setDeptno(10);
int result = mapper.insertEmp3(emp);
sqlSession.commit();
System.out.println(result);
System.out.println(emp.getEmpno());      // Get the returned primary key 

useGeneratedKeys Set to true after ,mybatis Will use JDBC Of getGeneratedkeys Method to obtain the primary key automatically generated inside the database , And assign this value to keyProperty Specified properties ; Be careful : This method is only suitable for databases with self growing Columns (mysql、sqlserver etc. )

1.7. modify

<update id="updateEmp" parameterType="Emp">
    update emp set job=#{job},sal=#{sal} where empno=#{empno}
</update>
Emp emp = new Emp();
emp.setEmpno(7934);
emp.setJob(" The manager ");
emp.setSal(2000.0);
int result = mapper.updateEmp(emp);
sqlSession.commit();
System.out.println(result);

Be careful : Any addition, deletion or modification will be returned int value , Indicates the number of rows affected . however ,insert The label cannot be written resultType attribute

1.8. Delete

<delete id="deleteEmp" parameterType="int">
    delete from emp where empno=#{empno}
</delete>
int result = mapper.deleteEmp(7939);
sqlSession.commit();
System.out.println(result);
原网站

版权声明
本文为[Liu Chu, Ge Nian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221538403221.html