public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
@Select("Select & from user where id = #{id}")
User getUserByID(@Param("id") int id,@Param("name") String name);
//方法存在多个参数的时候,所有的参数前面必须加上@Param注解
//Param里面类似于取别名,基本类型都要加上
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{password}")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{password} where id=#{id}")
int updateUser(User user);
@Delete("delete from user where id = #{uid}")
int deleteUser(@Param("uid") int id);
@Data
public class Student{
private int id;
private String name;
//学生需要一个老师
private Teacher teacher;
}
@Data
public class Teacher(){
private int id;
private String name;
}
2.接口(dao目录下):
public interface StudentMapper{
//查询所有的学生信息,以及对应的老师的信息
public List getStudent();
}
public interface TeacherMapper{
}
3.在配置目录下
mybatis-config.xml配置
十、一对多
比如:一个老师拥有多个学生,对于老师来说就是一对多的关系。
1.实体类(pojo目录下):
@Data
public class Student{
private int id;
private String name;
//学生需要一个老师
private int tid;
}
@Data
public class Teacher(){
private int id;
private String name;
//一个老师可以拥有多个学生
private List students;
}
2.接口(dao目录下):
public interface StudentMapper{
}
public interface TeacherMapper{
//获取老师
//public List getTeacher();
//获取指定老师下的所有学生及老师的信息
Teacher getTeacher(@Param("tid")int id);
}
3.在配置目录下
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid = t.id and t.id = #{tid}
mybatis-config.xml配置
小结:
关联-association
集合-collection
javaType & ofType
javaType 用来指定实体类中属性的类型
ofType 用来指定映射到List或者集合中的pojo类型,泛型中的约束类型
注意点:
SQL的可读性。
注意一对多和多对一,属性名和字段的问题。
问题不好排查的时候,可以使用日志
慢SQL 1s 1000s
面试高频
Mysql引擎
InnoDB底层原理
索引
索引优化
十一、动态SQL
动态SQL就是指根据不同的条件生成不同的SQL语句
if
choose(when,otherwise)
trim(where,set)
foreach
搭建环境
CREATE TABLE blog(
id varchar(50) NOT NULL COMMENT '博客id'
title varchar(100) NOT NULL COMMENT '博客标题'
author varchar(30) NOT NULL COMMENT '博客作者'
create_time datetime NOT NULL COMMENT '创建时间'
views int (30) NOT NULL COMMENT '浏览量'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
创建一个基础工程
导包(MybatisUtils)
编写一个配置文件
编写实体类
编写实体类对应Mapper接口和Mapper.XML文件
编写实体类
public class Blog{
private int id;
private String title;
private String author;
private Date date;
private int views;
}
编写实体类对应Mapper接口和Mapper.XML文件
public interface BlogMapper{
int addBlog(Blog blog);
}
insert into mybatis.blog(id,title,author,create_time,views)
values(#{id},#{title},#{author},#{create_time},,#{views})
MyTest.java
public class MyTest{
@Test
public void addInitBlog(){
SqlSession session = MybatisUtils.getSqlSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = new Blog();
blog.setId(IDutils.getId());
blog.setTitle("Title");
blog.setAuthor("TuNan");
blog.serCreateTime(new Date());
blog.setViews(999);
mapper.addBlog(blog);
blog.setId(IDutils.getId());
blog.setTitle("Title2");
blog.setViews(666);
mapper.addBlog(blog);
session.close();
}
}
if
//接口
//查询博客
List queryBlogIF(Mapper mapper);e
select * from mybatis.blog where 1=1
and title = #{title}
and author = #{author}
choose(when,otherwise)
select * from mybatis.blog
title = #{title}
and author = #{author}
and views = #{views}
trim(where,set)
select * from mybatis.blog
title = #{title}
and author = #{author}
update Author
title = #{title},
author = #{author}
where id = #{id}
所谓的动态SQL,本质还是SQL语句,只是可以在SQL层面, 取执行一个逻辑代码
Foreach
SELECT *
FROM POST P
WHERe ID in
#{item}
select * from mybatis.blog
id = #{id}
SQL片段
有点时候,可能需要将一些片段抽取出来,方便复用
title = #{title},
author = #{author}
select * from mybatis.blog
评论列表(0条)