import com.mb.cloudCar.entity.Car;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface CarDao {
//添加的方法
int addCar(Car car);
//修改的方法
int updateCar(Car car);
//删除的方法
int deleteCar(String carId);
//无条件查询
List findCar();
//多条件查询
List findCar1(String carId,String money);
List findCar2(@Param("ci")String carId,@Param("mo")String money);
List findCar3(Map map);
List findCar4(Car car);
//模糊查询
List findCar5(Car car);
}
insert into car(carid,carnumber,money,cartype,dept,gas,safeman,yeartime,
displacement,model,datepurchase,datereg,status,deptcarperson,
finaltime,nextyeartime,power,totalmass,fare,onboarddata,
remarks,filledby,fillingtime)
values
(#{carId},#{carNumber},#{money},#{carType},#{dept},#{gas},#{safeMan},
#{yearTime},#{displacement},#{model},#{datepurchase},#{datereg},
#{status},#{deptCarperson},#{finalTime},#{nextYearTime},#{power},
#{totalMass},#{fare},#{onboarddata},#{remarks},#{filledby},#{fillingTime})
update car set carnumber=#{carNumber},money=#{money},cartype=#{carType},dept=#{dept},
gas=#{gas},safeman=#{safeMan},yeartime= #{yearTime},displacement=#{displacement},
model=#{model},datepurchase=#{datepurchase},datereg=#{datereg},
status=#{status},deptcarperson=#{deptCarperson},finaltime=#{finalTime},
nextyeartime=#{nextYearTime},power=#{power},totalmass= #{totalMass},
fare=#{fare},onboarddata=#{onboarddata},remarks=#{remarks},
filledby=#{filledby},fillingtime=#{fillingTime}
where carid=#{carId}
delete from car where carid=#{carId}
select carid,carnumber,money,cartype,dept,gas,safeman,yeartime,
displacement,model,datepurchase,datereg,status,deptcarperson,
finaltime,nextyeartime,power,totalmass,fare,onboarddata,
remarks,filledby,fillingtime
from car
select carid,carnumber,money,cartype,dept,gas,safeman,yeartime,
displacement,model,datepurchase,datereg,status,deptcarperson,
finaltime,nextyeartime,power,totalmass,fare,onboarddata,
remarks,filledby,fillingtime
from car
where carid=#{arg0} and money=#{arg1}
select carid,carnumber,money,cartype,dept,gas,safeman,yeartime,
displacement,model,datepurchase,datereg,status,deptcarperson,
finaltime,nextyeartime,power,totalmass,fare,onboarddata,
remarks,filledby,fillingtime
from car
where carid=#{ci} and money=#{mo}
select carid,carnumber,money,cartype,dept,gas,safeman,yeartime,
displacement,model,datepurchase,datereg,status,deptcarperson,
finaltime,nextyeartime,power,totalmass,fare,onboarddata,
remarks,filledby,fillingtime
from car
where carid=#{ci} and money=#{mo}
select carid,carnumber,money,cartype,dept,gas,safeman,yeartime,
displacement,model,datepurchase,datereg,status,deptcarperson,
finaltime,nextyeartime,power,totalmass,fare,onboarddata,
remarks,filledby,fillingtime
from car
where carid=#{carId} and money=#{money}
select carid,carnumber,money,cartype,dept,gas,safeman,yeartime,
displacement,model,datepurchase,datereg,status,deptcarperson,
finaltime,nextyeartime,power,totalmass,fare,onboarddata,
remarks,filledby,fillingtime
from car
where carid like CONCAt('%',#{carId},'%') and money like CONCAt('%',#{money},'%')
7、模糊查询
select deptno,dname,loc
from dept
where loc like CONCAt('%',#{loc},'%')
8、主键回填
订单表:order
id 订单id 自增
ordertime 订单时间
userID 下单人
totalprice 总价
1 2022-4-1 taoxiankun 200
订单详情表:orderdetail
id 自增主键
orderid 订单编号
goodsid 商品编号
1 1 kele
2 1 keyboard
主从表结构:
主表:订单基本信息
从表:订单的商品信息
对于主从表结构,在添加时应该在一个事务内,先插入主表,获得自增主键,然后在插入从表是要使用这个主键
如何在插入一个表后,获得我插入的主键?
使用主键回填的方式
主键:int类型,自增主键回填
select LAST_INSERT_ID()
insert into `order`(ordertime,totalprice,userid)
values
(#{orderTime},#{totalPrice},#{userId})
insert into `order`(ordertime,totalprice,userid)
values
(#{orderTime},#{totalPrice},#{userId})
【说明】
1、以上两种方式,只适用于int自增主键
2、本质是执行mysql的函数LAST_INSERT_ID()来获取的主键
主键:字符串类型,需要自己调用数据库的UUID函数,产生主键
顺序: 先产生主键,回填给实体类的属性
然后 执行insert ,获取刚才回填的主键
select REPLACE(UUID(),'-','')
insert into order2 (id,name)
values (#{id},#{name})
select deptno dno,dname `name`,loc location
from dept
【说明】在单表查询中,使用别名可以解决不同名的问题,但是在主外键关系的表中将不适用
方式2:使用resultMap进行ORM映射
使用resultMap
完成一对一的映射:
在类中有个属性,属性是单个对象类型
select empno,ename,job,com,sal,joindate,mgrno,emp.deptno,
dname,loc
from emp
inner join dept on emp.deptno=dept.deptno
完成一对多映射:
一个类中的属性是集合类型,称为一对多映射
select empno,ename,job,com,sal,joindate,mgrno,emp.deptno,
dname,loc
from emp
inner join dept on emp.deptno=dept.deptno
select empno,ename,job,com,sal,joindate,mgrno,emp.deptno,
dname,loc,phone,code,content
from emp
inner join dept on emp.deptno=dept.deptno
left join address on emp.empno=address.userid
完成多对多映射
使用场景
学生表:student:stuno,stuname
课程表:course:courseno,coursename
学生和课程之间就是多对多的关系:一个学生可以选多门课程,一门课程也可以被多个学生选
中间表:选课表,通过中间表把学生和课程之间的多对多的关系进行体现
stu_counse: id stuno courseno
多对多查询3表联查
Students实体类
package com.qf.mybatispro2105.pojo;
import java.util.List;
public class Students {
public Students() {
}
public Students(String stuNo, String stuName) {
this.stuNo = stuNo;
this.stuName = stuName;
}
public Students(String stuNo, String stuName, List courseList) {
this.stuNo = stuNo;
this.stuName = stuName;
this.courseList = courseList;
}
private String stuNo;
private String stuName;
//所选课程的属性
//一对多
private List courseList;
public List getCourseList() {
return courseList;
}
public void setCourseList(List courseList) {
this.courseList = courseList;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
Course实体类
package com.qf.mybatispro2105.pojo;
import java.util.List;
public class Course {
public Course() {
}
public Course(String courseNo, String courseName) {
this.courseNo = courseNo;
this.courseName = courseName;
}
public Course(String courseNo, String courseName, List studentsList) {
this.courseNo = courseNo;
this.courseName = courseName;
this.studentsList = studentsList;
}
private String courseNo;
private String courseName;
//选本门课的学生
//一对多
private List studentsList;
public List getStudentsList() {
return studentsList;
}
public void setStudentsList(List studentsList) {
this.studentsList = studentsList;
}
public String getCourseNo() {
return courseNo;
}
public void setCourseNo(String courseNo) {
this.courseNo = courseNo;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
由于是双向的一对多关系,就是多对多的关系
查询:
1、查询学生,同时查询学生所选课程
select s.stuno,stuname,c.courseno,c.coursename
from students s
left join stucourse sc on s.stuno=sc.stuno
inner join course c on c.courseno=sc.courseno
2、查询课程,同时查询选择该课程的学生
select c.courseno,c.coursename,s.stuno,s.stuname
from course c
left join stucourse sc on c.courseno=sc.courseno
inner join students s on s.stuno=sc.stuno
public interface DeptDao2 {
//添加部门
@Insert("insert into dept(deptno,dname,loc)values(#{deptNo},#{dName},#{loc})")
int addDept(Dept dept);
//修改部门
@Update("update dept set dname=#{dName},loc=#{loc} where deptno=#{deptNo}")
int updateDept(Dept dept);
//删除部门
@Delete("delete from dept where deptno=#{deptNo}")
int deleteDept(int deptNo);
//查询
@Select("select deptno,dname,loc from dept")
List findDept();
}