Mybatis-4
作者:互联网
一.lombok的使用
1.在pom.xml添加lombok的依赖
org.projectlombok lombok 1.18.12 provided
2.IDEA中安装lombok插件
3.安装完插件之后,重新启动IDEA工具,会在右下角,显示如下窗口,这个窗口千万不要关,然后按enble
如果关闭了就要重新设置,设置如下
4.显示lombok窗口
5.使用lombok的注解声明
@Data:生成setter,getter,equal, toString,hasCode,无参构造方法
@NoArgsConstructor:生成无参构造方法,如果不声明@AllArgsContructor,就不需要写@NoArgsConstructor注解
@Setter:只生成setter方法
@Getter:只生成getter方法
@AllArgsConstructor:生成全参的构造方法
二.MyBatis的单表的增删改差
接口
AdminMapper { getCount(); Admin selectAdminById(id); add(Admin admin); delete(id); update(Admin admin); List<Admin> findAll(); }
映射文件
SELECT () FROM admin SELECT FROM admin WHERE id=#{id} INSERT INTO admin(name,age,password,telephone)VALUES(#{name},#{age},#{password},#{telephone}) DELETE FROM admin WHERE id=#{id} UPDATE admin SET name=#{name},password=#{password},age=#{age},telephone=#{telephone} WHERE id=#{id} SELECT FROM admin
测试代码
AppTest { Logger =Logger.(AppTest.); testAdd(){ SqlSession sqlSession= MyBatisUtil.(); AdminMapper adminMapper=sqlSession.getMapper(AdminMapper.); { Admin admin=Admin(); admin.setName(); admin.setAge(); admin.setPassword(); admin.setTelephone(); adminMapper.add(admin); sqlSession.commit(); } (Exception e) { sqlSession.rollback();e.printStackTrace(); } MyBatisUtil.(sqlSession); } testDelte(){ SqlSession sqlSession= MyBatisUtil.(); AdminMapper adminMapper=sqlSession.getMapper(AdminMapper.); { adminMapper.delete(); sqlSession.commit(); } (Exception e) { e.printStackTrace(); sqlSession.rollback(); } MyBatisUtil.(sqlSession); } testUpdate(){ SqlSession sqlSession= MyBatisUtil.(); AdminMapper adminMapper=sqlSession.getMapper(AdminMapper.); Admin admin=Admin(); admin.setId(); admin.setName(); admin.setAge(); admin.setPassword(); admin.setTelephone(); { adminMapper.update(admin); sqlSession.commit(); } (Exception e) { e.printStackTrace(); sqlSession.rollback(); } MyBatisUtil.(sqlSession); } testFindAll(){ SqlSession sqlSession= MyBatisUtil.(); AdminMapper adminMapper=sqlSession.getMapper(AdminMapper.); List<Admin> all=adminMapper.findAll(); (Admin admin:all) { .info(admin); } MyBatisUtil.(sqlSession); } }
标签:MyBatisUtil,admin,sqlSession,AdminMapper,Mybatis,adminMapper,id 来源: https://www.cnblogs.com/qiao88/p/13785237.html