当前位置:网站首页>Mybaits: common database operations (Neusoft operations)
Mybaits: common database operations (Neusoft operations)
2022-06-22 17:16:00 【Liu Chu, Ge Nian】
List of articles
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 :
parameterTypeonly one . therefore , When there are multiple parameters, use the object to pass values ( This is it. Input mapping ).#{}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 |
|---|---|---|
< | < | Less than |
> | > | Greater than |
& | & | And |
' | ’ | Single quotation marks |
" | " | Double quotes |
<select id="listEmpBySal" parameterType="double" resultType="Emp">
select * from emp where sal < #{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 :
- selectKey In the tag select LAST_INSERT_ID() Statement to get the generated primary key
- selectKey In the tag keyProperty The attribute is the primary key name ,MyBatis The obtained primary key will be automatically encapsulated to this property .
- 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);
边栏推荐
- Defaultifempty for C # -linq source code analysis
- Basic application of scala for
- 以小见大:一个领域建模的简单示例,理解“领域驱动”。
- Hello playwright: (7) simulate keyboard and mouse
- Quartus Prime 18.0软件安装包和安装教程
- Description of new features and changes in ABP Framework version 5.3.0
- What is a flush? Is online account opening safe?
- Blazor University (31)表单 —— 验证
- Quickly master asp Net authentication framework identity - login and logout
- scala-for推导:能够在for表达式中的最初部分定义值,并在(外面)后面的表达式中使用该值
猜你喜欢

Figure operation flow of HAMA BSP Model

System throughput, TPS (QPS), user concurrency, performance test concepts and formulas

Qt筆記-QMap自定義鍵(key)

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

JMeter use case

网传学习通1.7亿密码泄露!有什么补救措施?

STM32 ADC acquisition via DMA (HAL Library)

【阿里云服务器-安装mysql的5.6版本安装,重装】

【进阶自动化测试第一步】1分钟带你了解自动化测试

Partage de l'architecture du système de paiement du Groupe letv pour traiter 100 000 commandes simultanées élevées par seconde
随机推荐
A classmate asked what framework PHP should learn?
从Application提交角度审视Executor
团队管理|如何提高技术 Leader 的思考技巧?
[step 1 of advanced automated testing] 1 minute to introduce you to automated testing
Figure operation flow of HAMA BSP Model
网传学习通1.7亿密码泄露!有什么补救措施?
MTLs guidelines for kubernetes engineers
MySQL master-slave connection prompt of docker: communications link failure
【招聘】[北京中关村/远程][TensorBase][开源数据仓库]等一群人,做一件事
同花顺软件是什么?手机开户安全么?
What are the characteristics of the interactive whiteboard? Function introduction of electronic whiteboard
[cursor nesting] nesting of MySQL stored procedure cursors
同花顺是什么?在线开户安全么?
推荐7款超级好用的终端工具 —— SSH+FTP
Mqtt of NLog custom target
每秒處理10萬高並發訂單的樂視集團支付系統架構分享
Processing source code of spark executor execution results
Qt筆記-QMap自定義鍵(key)
Service or mapper cannot be injected into a multithread
Blazor University (31) form - Validation