当前位置:网站首页>Mybaits:常用数据库操作(东软的操作)
Mybaits:常用数据库操作(东软的操作)
2022-06-22 15:38:00 【流楚丶格念】
1.常用数据库操作
老师没给上课的数据库和代码,下面的没运行,看不懂可以不学这篇,学下一篇
1.1.多条件查询
<select id="listEmp" parameterType="Emp" resultType="Emp">
select *
from emp
where job like concat('%',#{job},'%') and deptno=#{deptno}
order by empno
</select>
查询员工表,job含有Emp.job中字符并且deptno是Emp.deptno的员工,最后按照empno排序
Emp emp = new Emp();
emp.setJob("经");
emp.setDeptno(20);
List<Emp> list = mapper.listEmp(emp);
for(Emp e : list) {
System.out.println(e);
}
注意:
parameterType只有一个。所以,有多个参数时使用对象传值(这就是输入映射)。#{}中书写的是实体对象的属性名,所以要严格区分大小写。
1.2.转义字符查询
由于 <(小于号)是标签关键词,因此不能识别小于号。所以MyBatis中设计了一些转义字符,来代替一些特殊字符:
| 代码 | 转义字符 | 说明 |
|---|---|---|
< | < | 小于 |
> | > | 大于 |
& | & | 与 |
' | ’ | 单引号 |
" | " | 双引号 |
<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.返回单值查询
<select id="listEmpCount" resultType="int">
select count(*) from emp
</select>
int count = mapper.listEmpCount();
System.out.println(count);
注意:只有返回一行一列,resultType才能使用基本数据类型
1.4.插入(不获取主键)
<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("张三");
emp.setJob("职员");
emp.setHiredate("2020-09-04");
emp.setDeptno(10);
int result = mapper.insertEmp1(emp);
sqlSession.commit(); //注意:要commit提交
System.out.println(result);
注意:增删改都会返回int值,表示影响的行数。但是,insert标签中不能书写resultType属性
1.5.插入(获取主键)
<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("张三");
emp.setJob("职员");
emp.setHiredate("2020-09-04");
emp.setDeptno(10);
int result = mapper.insertEmp2(emp);
sqlSession.commit();
System.out.println(result);
System.out.println(emp.getEmpno()); //获取返回的主键
注意:
- selectKey标签中的 select LAST_INSERT_ID() 语句就能获取生成的主键
- selectKey标签中的keyProperty属性就是主键名,MyBatis会自动将获取的主键封装给此属性。
- order的值有两种:BEFORE、AFTER
BEFORE:先获取主键,然后执行insert; 比如 Oracle数据库。
AFTER:先执行insert,然后获取主键; 比如 MySql数据库。
1.6.插入(获取主键)
<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("张三");
emp.setJob("职员");
emp.setHiredate("2020-09-04");
emp.setDeptno(10);
int result = mapper.insertEmp3(emp);
sqlSession.commit();
System.out.println(result);
System.out.println(emp.getEmpno()); //获取返回的主键
useGeneratedKeys设置为true后,mybatis会使用JDBC的getGeneratedkeys方法获取由数据库内部自动生成的主键,并将该值赋值给由keyProperty指定的属性; 注意:此种方式只适合于有自增长列的数据库(mysql、sqlserver等)
1.7.修改
<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("经理");
emp.setSal(2000.0);
int result = mapper.updateEmp(emp);
sqlSession.commit();
System.out.println(result);
注意:增删改都会返回int值,表示影响的行数。但是,insert标签中不能书写resultType属性
1.8.删除
<delete id="deleteEmp" parameterType="int">
delete from emp where empno=#{empno}
</delete>
int result = mapper.deleteEmp(7939);
sqlSession.commit();
System.out.println(result);
边栏推荐
- Idea installation summary
- [C language] deeply analyze the storage of integer and floating-point types in memory
- Short video source code development, high-quality short video source code need to do what?
- Post to asp Net core length limitation and solution when transferring data
- Oracle database and table
- 联合主键引发的思考
- 每秒处理10万高并发订单的乐视集团支付系统架构分享
- Figure operation flow of HAMA BSP Model
- System throughput, TPS (QPS), user concurrency, performance test concepts and formulas
- 购买指南丨如何购买一台高质量会议平板,这几个方面一定要对比
猜你喜欢

企业级软件开发新模式:低代码

图计算Hama-BSP模型的运行流程

Jsp Learning (2) - - jsp script Elements and instructions

高可用性的ResourceManager

Linux system maintenance: mysql8.0.13 source code download and installation "fool" operation steps (Linux centos6.8) test available series

STM32 ADC acquisition via DMA (HAL Library)
![[pop up box 2 at the bottom of wechat applet package]](/img/31/266e6a1f4200347c9324ea37b78562.png)
[pop up box 2 at the bottom of wechat applet package]

How to add a "security lock" to the mobile office of government and enterprises?

What is restful and what rules should be followed when designing rest APIs?

【进阶自动化测试第一步】1分钟带你了解自动化测试
随机推荐
[Alibaba cloud server - install MySQL version 5.6 and reinstall]
spark与mysql:Did not find registered driver with class com.mysql.jdbc.Driver
Apache ShardingSphere 一文读懂
Docker 之MySQL 重启,提示Error response from daemon: driver failed programming external connectivity on **
On the closure function of Scala
. Net release and support plan introduction
Blazor University (30) form - derived from inputbase
Web technology sharing | [Gaode map] to realize customized track playback
Docker之MySQL主从连接提示:Communications link failure
Problems and recovery of spark streaming checkpoint
Analysis of the writer source code of spark shuffle
Qt笔记-QMap自定义键(key)
What is restful and what rules should be followed when designing rest APIs?
Processing source code of spark executor execution results
每秒處理10萬高並發訂單的樂視集團支付系統架構分享
Team management | how to improve the thinking skills of technical leaders?
The MySQL of docker restarts, prompting error response from daemon: driver failed programming external connectivity on**
QT notes qmap user defined key
[wechat applet to obtain the height of custom tabbar] is absolutely available!!!
vs2017 在调试状态不显示QString值的解决方法