0Mybatis的简单介绍
(资料图片)
MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundation迁移到了googlecode,并且改名为MyBatis。2013年11月迁移到Github。
iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQLMaps和DataAccessObjects(DAOs)
MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的POJOs(PlainOrdinaryJavaObject,普通的Java对象)映射成数据库中的记录。
思想:ORM:ObjectRelationalMapping
Mybatis基本框架:
1环境搭建
新建SpringBoot项目,引入依赖
pom.xml
DB-sql
CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENT,`name`varchar(255)DEFAULTNULL,`age`int(11)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=13DEFAULTCHARSET=utf8;复制代码
项目结构
2整合方式一:注解版
2.1配置
server:port:8081spring:datasource:#配置数据库username:rootpassword:12345driver-class-name:com.mysql.jdbc.Driverurl:jdbc:mysql://localhost:3306/test#数据库名type:com.alibaba.druid.pool.DruidDataSource复制代码
2.2编码
/***注解版*/@Mapper@RepositorypublicinterfaceJavaStudentMapper{/***添加一个学生**@paramstudent*@return*/@Insert("insertintostudent(name,age)"+"values(#{name},#{age})")publicintsaveStudent(Studentstudent);/***根据ID查看一名学生**@paramid*@return*/@Select("select*"+"fromstudent"+"whereid=#{id}")publicStudentfindStudentById(Integerid);/***查询全部学生**@return*/@Select("select*"+"fromstudent")@Results({@Result(property="id",column="id"),@Result(property="name",column="name"),@Result(property="age",column="age")})publicList
2.3测试
@AutowiredprivateJavaStudentMapperstudentMapper;@TestvoidtestJavaMapperInsert{Studentstudent=newStudent("张三",22);System.out.println(studentMapper.saveStudent(student));}@TestvoidtestJavaMapperFind{System.out.println(studentMapper.findStudentById(2));}@TestvoidtestJavaMapperFindAll{System.out.println(studentMapper.findAllStudent);}@TestvoidtestJavaMapperUpdate{Studentstudent=newStudent(2,"张三",22);System.out.println(studentMapper.updateStudentById(student));}@TestvoidtestJavaMapperDelete{System.out.println(studentMapper.removeStudentById(2));}复制代码
3整合方式二:XML版
3.1配置
server:port:8081spring:application:name:hospitalManager#项目名datasource:#配置数据库username:rootpassword:12345driver-class-name:com.mysql.jdbc.Driverurl:jdbc:mysql://localhost:3306/test#数据库名type:com.alibaba.druid.pool.DruidDataSourcemybatis:mapper-locations:classpath:mapper/*Mapper.xml#扫描resources下的mapper文件夹下的xml文件type-aliases-package:org.ymx.sp_mybatis.pojo#实体类所在包,定义别名复制代码
主启动类:
@SpringBootApplication@MapperScan("org.ymx.sp_mybatis.xmlMapper")//扫描的mapperpublicclassSpMybatisApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpMybatisApplication.class,args);}}复制代码
3.2编码
/***xml版*/@Mapper@RepositorypublicinterfaceXmlStudentMapper{/***添加一个学生**@paramstudent*@return*/publicintsaveStudent(Studentstudent);/***根据ID查看一名学生**@paramid*@return*/publicStudentfindStudentById(Integerid);/***查询全部学生**@return*/publicList
xml文件(StudentMapper.xml)
3.3测试
@AutowiredprivateXmlStudentMapperstudentMapper;@TestvoidtestJavaMapperInsert{Studentstudent=newStudent("张三",22);System.out.println(studentMapper.saveStudent(student));}@TestvoidtestJavaMapperFind{System.out.println(studentMapper.findStudentById(2));}@TestvoidtestJavaMapperFindAll{System.out.println(studentMapper.findAllStudent);}@TestvoidtestJavaMapperUpdate{Studentstudent=newStudent(2,"张三",22);System.out.println(studentMapper.updateStudentById(student));}@TestvoidtestJavaMapperDelete{System.out.println(studentMapper.removeStudentById(2));}复制代码
4总结
基本步骤:
注意事项:
(1)相比注解方式,更推荐使用xml方式,因为注解方式将SQL语句嵌套到Java代码中,一旦需要修改则需要重新编译项目,而xml方式则不需要重新编译项目
(2)xml方式需要在主启动函数或配置类中配置接口的扫描路径
Copyright © 2015-2022 今日动漫网版权所有 备案号:沪ICP备2022005074号-40 联系邮箱:5 85 59 73 @qq.com