MyBatis框架的主要配置文件:mybatis-config.xml文件和Mapper.xml文件。
select Id, Name, Age, Salary, Sex, Status, AddTime, UpdateTime from RUN_User
select、update、delete、insert标签,分别对应查询、更新、删除、新增操作
(1)基本数据类型:
(2)String数据类型:
(3)包装类,通过id查询:
(4)JavaBean参数:
update RUN_User set name = #{userEntity.name}, age = #{userEntity.age}, salary = #{userEntity.salary}, sex = #{userEntity.sex}, status = 1
where id = #{userEntity.id}
(1)基本数据类型:
(2)包装类型:
(3)String类型:
(4)JavaBean类型:
CREATE TABLE `RUN_Class` (
`Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID(班级)',
`ClassName` varchar(256) NOT NULL COMMENT '班级名称',
`Status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '有效=1,无效=-1',
`AddTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
`UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB COMMENT='班级信息表';
CREATE TABLE `RUN_Student` (
`Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID(学号)',
`Name` varchar(256) NOT NULL DEFAULT '' COMMENT '姓名',
`ClassId` int(11) NOT NULL COMMENT '班级',
`Status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '有效=1,无效=-1',
`AddTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
`UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB COMMENT='学生信息表';
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ClassEntity implements Serializable {
private static final long serialVersionUID = -3337804323389088625L;
private int id; //班级ID
private String className; //班级名称
private int status; //是否有效(1:有效,-1:无效)
private String addTime; //添加时间
private String updateTime; //更新时间
private List studentEntities; //该班级中有哪些学生
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class StudentEntity implements Serializable {
private static final long serialVersionUID = -7497520016303190017L;
private int id; //学号
private String name; //姓名
private int classId; //班级
private int status; //是否有效(1:有效,-1:无效)
private String addTime; //添加时间
private String updateTime; //更新时间
private ClassEntity classEntity; //该学生属于哪个班级
}
public interface StudentRepository {
StudentEntity queryStudent(@Param("id") int id);
StudentEntity queryStudentWithClass(@Param("id") int id);
}
select Id, Name, ClassId, Status, AddTime, UpdateTime from RUN_Student
public class UserTest3 {
public static void main(String[] args) {
//加载MyBatis配置文件
InputStream inputStream = UserTest3.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取实现接口的代理对象
StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
StudentEntity studentEntity = studentRepository.queryStudent(2);
System.out.println(studentEntity);
sqlSession.close();
}
}
public interface ClassRepository {
ClassEntity queryClassByClassId(@Param("id") int id);
}
select c.Id, c.ClassName, c.Status, c.AddTime, c.UpdateTime,
s.Id as sId, s.Name as sName, s.ClassId as sClassId, s.Status as sStatus, s.AddTime as sAddTime, s.UpdateTime as sUpdateTime
from RUN_Class c join RUN_Student s on c.Id = s.classId
public class UserTest3 {
public static void main(String[] args) {
//加载MyBatis配置文件
InputStream inputStream = UserTest3.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取实现接口的代理对象
ClassRepository classRepository = sqlSession.getMapper(ClassRepository.class);
ClassEntity classEntity = classRepository.queryClassByClassId(3);
System.out.println(classEntity);
sqlSession.close();
}
}
(1)实体类 顾客:Customer。实体类 商品:Goods。
@Data
public class Customer {
private long id;
private String name;
private List goods;
}
@Data
public class Goods {
private long id;
private String name;
private List customers;
}
(2)中间表:用来存储顾客和商品之间的关系
@Data
public class Custom_Goods {
private long id;
private long cid;
private long gid;
}
(3)顾客接口:CustomerRepository
public interface CustomerRepository {
public Customer findById(long id);
}
(4)顾客SQL语句:CustomerRepository.xml
(5)商品接口:GoodsRepository
public interface GoodsRepository {
public Goods findById(long id);
}
(6)商品SQL语句:GoodsRepository.xml