mybatis(面向接口编程、注解开发、@Param注解)
1、面向接口编程
(1)面向接口编程的优点
解耦、可扩展、提高复用、分层开发中,上层不用管具体的实现,大家都遵循共同的标准,提高代码的规范性
(2)对接口的理解
定义(规范、约束)与实现的分离
反映了设计人员对系统的抽象理解
接口有两类:抽象体(对一个个体的抽象)和抽象面(对一个个体某一方面的抽象),一个个体可以有多个抽象面
2、使用注解开发
(1)接口:
@Select("select * from student") List<Student> getStudents();
(2)配置文件(核心配置文件,不用再对mapper.xml进行配置)
<mappers> <mapper class="pers.zhb.mapper.StudentMapper"></mapper> </mappers>
(3)测试:
@Test public void testZhuJie(){ SqlSession sqlSession= MybatisUtils.getSqlSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); List<Student> students=studentMapper.getStudents(); for (Student student : students) { System.out.println(student); } sqlSession.close(); }
DEBUG [main] - Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl‘ adapter. DEBUG [main] - Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl‘ adapter. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 249155636. DEBUG [main] - Setting autocommit to false on JDBC Connection [] DEBUG [main] - ==> Preparing: select * from student DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 11 Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘80501‘, point=‘890‘, phone=‘1234567890‘, email=‘null‘, clas=null} Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘80601‘, point=‘893‘, phone=‘19837372533‘, email=‘null‘, clas=null} Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘80501‘, point=‘892‘, phone=‘19837372534‘, email=‘null‘, clas=null} Student{studentno=‘201814‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘80501‘, point=‘892‘, phone=‘19837372534‘, email=‘null‘, clas=null} Student{studentno=‘201815‘, sname=‘qwerr‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘80501‘, point=‘892‘, phone=‘19837372534‘, email=‘null‘, clas=null} Student{studentno=‘201816‘, sname=‘jiayou‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘80501‘, point=‘892‘, phone=‘19837372534‘, email=‘null‘, clas=null} Student{studentno=‘201817‘, sname=‘null‘, sex=‘null‘, birthday=‘null‘, classno=‘2‘, point=‘null‘, phone=‘null‘, email=‘null‘, clas=null} Student{studentno=‘201818‘, sname=‘null‘, sex=‘null‘, birthday=‘null‘, classno=‘2‘, point=‘null‘, phone=‘null‘, email=‘null‘, clas=null} Student{studentno=‘2‘, sname=‘2‘, sex=‘2‘, birthday=‘null‘, classno=‘null‘, point=‘null‘, phone=‘2‘, email=‘null‘, clas=null} Student{studentno=‘1‘, sname=‘1‘, sex=‘1‘, birthday=‘null‘, classno=‘null‘, point=‘null‘, phone=‘1‘, email=‘null‘, clas=null} Student{studentno=‘21‘, sname=‘21‘, sex=‘21‘, birthday=‘null‘, classno=‘null‘, point=‘null‘, phone=‘21‘, email=‘null‘, clas=null} DEBUG [main] - Resetting autocommit to true on JDBC Connection [] DEBUG [main] - Closing JDBC Connection [] DEBUG [main] - Returned connection 249155636 to pool.
使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。摘自:(https://mybatis.org/mybatis-3/zh/getting-started.html)
3、注解实现增删改查
自动提交事务(mybatis工具类):
public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(true);//不需要手动提交 }
在使用注解的方式进行增删改查的操作的时候,不再需要对mapper.xml进行配置,只需要在核心配置文件中添加如下代码:
<mappers> <mapper class="pers.zhb.mapper.StudentMapper"></mapper> </mappers>
(1)查询:
@Select("select * from student where studentno=#{id}") Student findStudentById(@Param("id") Integer studentno);
@Test public void testZhuJie(){ SqlSession sqlSession= MybatisUtils.getSqlSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); Student student=studentMapper.findStudentById(201816); System.out.println(student); sqlSession.close(); }
(2)添加:
@Insert(" insert into student(studentno,sname,sex,phone) values (#{studentno},#{sname},#{sex},#{phone})") int addStudent(Student student);
@Test public void testZhuJie(){ SqlSession sqlSession= MybatisUtils.getSqlSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setStudentno("20200426"); student.setSname("tom"); student.setSex("女"); student.setPhone("12345678901"); studentMapper.addStudent(student); sqlSession.close(); }
(3)修改:
@Update("update student set phone=#{phone} where studentno=#{studentno}") int updateStudent(Student student);
@Test public void testZhuJie(){ SqlSession sqlSession= MybatisUtils.getSqlSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setStudentno("20200426"); student.setPhone("20191817161"); studentMapper.updateStudent(student); sqlSession.close(); }
(4)删除:
@Delete("delete from student where studentno=#{id}") int deleteStudent(@Param("id") Integer studentno);
@Test public void testZhuJie(){ SqlSession sqlSession= MybatisUtils.getSqlSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); studentMapper.deleteStudent(20200426); sqlSession.close(); }
与使用配置文件的方式相比,在增删改查的时候,使用注解的方式去除了对mapper.xml配置文件的配置,只需要对接口进行定义(在接口中书写注解),以及书写测试类。当然,二者都需要被核心配置文件引入。
4、关于@Param注解
(1)基本数据类型或者String类型,需要添加@Param
(2)引用数据类型不需要
(3)如果只有一个基本的数据类型的话,可以忽略
(4)在SQL中引用的就是我们这里的@Param中设定的属性名
@Select("select * from student where studentno=#{id}") Student findStudentById(@Param("id") Integer studentno);