网站站建设网络营销的优势和劣势
- Mybatis注解开发单表操作
- Mybatis的常用注解
- Mybatis的增删改查
- MyBatis注解开发的多表操作
- MyBatis的注解实现复杂映射开发
- 一对一查询
- 一对多查询
- 多对多查询
- 构建sql
- sql构建对象介绍
- 查询功能的实现
- 新增功能的实现
- 修改功能的实现
- 删除功能的实现
Mybatis注解开发单表操作
Mybatis的常用注解
使用注解开发方式,可以减少编写Mapper映射文件
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
Mybatis的增删改查
步骤一:创建mapper接口
public interface StudentMapper {//查询全部@Select("SELECT * FROM student")public abstract List<Student> selectAll();//新增操作@Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")public abstract Integer insert(Student stu);//修改操作@Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")public abstract Integer update(Student stu);//删除操作@Delete("DELETE FROM student WHERE id=#{id}")public abstract Integer delete(Integer id);
}
步骤二:测试类
public class Test01 {@Testpublic void selectAll() throws Exception{//1.加载核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取StudentMapper接口的实现类对象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//5.调用实现类对象中的方法,接收结果List<Student> list = mapper.selectAll();//6.处理结果for (Student student : list) {System.out.println(student);}//7.释放资源sqlSession.close();is.close();}@Testpublic void insert() throws Exception{//1.加载核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取StudentMapper接口的实现类对象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//5.调用实现类对象中的方法,接收结果Student stu = new Student(4,"赵六",26);Integer result = mapper.insert(stu);//6.处理结果System.out.println(result);//7.释放资源sqlSession.close();is.close();}@Testpublic void update() throws Exception{//1.加载核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取StudentMapper接口的实现类对象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//5.调用实现类对象中的方法,接收结果Student stu = new Student(4,"赵六",36);Integer result = mapper.update(stu);//6.处理结果System.out.println(result);//7.释放资源sqlSession.close();is.close();}@Testpublic void delete() throws Exception{//1.加载核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取StudentMapper接口的实现类对象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//5.调用实现类对象中的方法,接收结果Integer result = mapper.delete(4);//6.处理结果System.out.println(result);//7.释放资源sqlSession.close();is.close();}
}
加载使用了注解的Mapper接口
<mappers><!--扫描使用注解的类--><mapper class="com.itheima.mapper.UserMapper"></mapper>
</mappers>
或者指定扫描包含映射关系的接口所在的包
<mappers><!--扫描使用注解的类所在的包--><package name="com.itheima.mapper"></package>
</mappers>
MyBatis注解开发的多表操作
MyBatis的注解实现复杂映射开发
一对一查询
对应sql语句
SELECT * FROM card;SELECT * FROM person WHERE id=#{id};
创建PersonMapper接口
public interface PersonMapper {//根据id查询@Select("SELECT * FROM person WHERE id=#{id}")public abstract Person selectById(Integer id);
}
使用注解配置Mapper
public interface CardMapper {//查询全部@Select("SELECT * FROM card")@Results({@Result(column = "id",property = "id"),@Result(column = "number",property = "number"),@Result(property = "p", // 被包含对象的变量名javaType = Person.class, // 被包含对象的实际数据类型column = "pid", // 根据查询出的card表中的pid字段来查询person表/*one、@One 一对一固定写法select属性:指定调用哪个接口中的哪个方法*/one = @One(select = "com.itheima.one_to_one.PersonMapper.selectById"))})public abstract List<Card> selectAll();
}
测试类
public class Test01 {@Testpublic void selectAll() throws Exception{//1.加载核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取CardMapper接口的实现类对象CardMapper mapper = sqlSession.getMapper(CardMapper.class);//5.调用实现类对象中的方法,接收结果List<Card> list = mapper.selectAll();//6.处理结果for (Card card : list) {System.out.println(card);}//7.释放资源sqlSession.close();is.close();}}
总结
@Results:封装映射关系的父注解。Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。column 属性:查询出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被包含对象的数据类型one 属性:一对一查询固定属性@One:一对一查询的注解。select 属性:指定调用某个接口中的方法
一对多查询
使用注解配置Mapper
public interface ClassesMapper {//查询全部@Select("SELECT * FROM classes")@Results({@Result(column = "id",property = "id"),@Result(column = "name",property = "name"),@Result(property = "students", // 被包含对象的变量名javaType = List.class, // 被包含对象的实际数据类型column = "id", // 根据查询出的classes表的id字段来查询student表/*many、@Many 一对多查询的固定写法select属性:指定调用哪个接口中的哪个查询方法*/many = @Many(select = "com.itheima.one_to_many.StudentMapper.selectByCid"))})public abstract List<Classes> selectAll();
}
总结
@Results:封装映射关系的父注解。Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。column 属性:查询出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被包含对象的数据类型many 属性:一对多查询固定属性
@Many:一对多查询的注解。select 属性:指定调用某个接口中的方法
多对多查询
使用注解配置Mapper
public interface StudentMapper {//查询全部@Select("SELECT DISTINCT s.id,s.name,s.age FROM student s,stu_cr sc WHERE sc.sid=s.id")@Results({@Result(column = "id",property = "id"),@Result(column = "name",property = "name"),@Result(column = "age",property = "age"),@Result(property = "courses", // 被包含对象的变量名javaType = List.class, // 被包含对象的实际数据类型column = "id", // 根据查询出student表的id来作为关联条件,去查询中间表和课程表/*many、@Many 一对多查询的固定写法select属性:指定调用哪个接口中的哪个查询方法*/many = @Many(select = "com.itheima.many_to_many.CourseMapper.selectBySid"))})public abstract List<Student> selectAll();
}
总结
@Results:封装映射关系的父注解。Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。column 属性:查询出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被包含对象的数据类型many 属性:一对多查询固定属性
@Many:一对多查询的注解。select 属性:指定调用某个接口中的方法
构建sql
sql构建对象介绍
MyBatis 给我们提供了 org.apache.ibatis.jdbc.SQL 功能类,专门用于构建 SQL 语句
查询功能的实现
-
定义功能类并提供获取查询的 SQL 语句的方法。
-
@SelectProvider:生成查询用的 SQL 语句注解。
type 属性:生成 SQL 语句功能类对象
method 属性:指定调用方法
新增功能的实现
-
定义功能类并提供获取新增的 SQL 语句的方法。
-
@InsertProvider:生成新增用的 SQL 语句注解。
type 属性:生成 SQL 语句功能类对象
method 属性:指定调用方法
修改功能的实现
-
定义功能类并提供获取修改的 SQL 语句的方法。
-
@UpdateProvider:生成修改用的 SQL 语句注解。
type 属性:生成 SQL 语句功能类对象
method 属性:指定调用方法
删除功能的实现
-
定义功能类并提供获取删除的 SQL 语句的方法。
-
@DeleteProvider:生成删除用的 SQL 语句注解。
type 属性:生成 SQL 语句功能类对象
method 属性:指定调用方法