Mybatis技术简介MyBatis的前世今生MyBatis的前身就是iBatis,iBatis本是由ClintonBegin开发,后来捐给Apache基金会,成立了iBatis开源项目。2010年5月该项目由Apahce基金会迁移到了GoogleCode,并且改名为MyBatis。MyBatis介绍MyBatis是一个数据持久层(ORM)框架。把实体类和SQL语句之间建立了映射关系,是一种半自动化的ORM实现。MyBatis的优点:1.基于SQL语法,简单易学。2.能了解底层组装过程。3.SQL语句封装在配置文件中,便于统一管理与维护,降低了程序的耦合度。4.程序调试方便。与传统JDBC的比较减少了61%的代码量最简单的持久化框架架构级性能增强SQL代码从程序代码中彻底分离,可重用增强了项目中的分工增强了移植性JDBC与MyBatis直观对比MyBatis就是将上面这几行代码分解包装:前两行是对数据库的数据源的管理包括事务管理,3、4两行MyBatis通过配置文件来管理SQL以及输入参数的映射,6、7、8行MyBatis获取返回结果到Java对象的映射,也是通过配置文件管理。MyBatisMyBatis的映射关系:表结构JavaBeanXml配置MyBatis基本要素一、configuration.xml全局配置文件二、mapper.xml核心映射文件三、SqlSession接口基础配置文件configuration.xmlconfiguration.xml是系统的核心配置文件,包含数据源和事务管理器等设置和属性信息,XML文档结构如下:?xmlversion=1.0encoding=UTF-8?!DOCTYPEconfigurationPUBLIC-//mybatis.org//DTDConfig3.0//EN=developmentenvironmentid=developmenttransactionManagertype=JDBC/dataSourcetype=POOLEDpropertyname=drivervalue=com.microsoft.sqlserver.jdbc.SQLServerDriver/propertyname=urlvalue=jdbc:sqlserver://localhost:1433;DatabaseName=Harmonyhis/propertyname=usernamevalue=sa/propertyname=passwordvalue=123456//dataSource/environmentenvironmentid=“development2”…/environment/environmentsmappersmapperresource=com/demo/model/UserMapper.xml//mappers/configuration基础配置文件—SQL映射文件SQL映射文件://1.使用相对路径mappersmapperresource=com/communityhealth/model/UserMapper.xml//mappers//2.使用全路径mappersmapperurl=file:///var/sqlmaps/AuthorMapper.xml/mapperurl=file:///var/sqlmaps/BlogMapper.xml/mapperurl=file:///var/sqlmaps/PostMapper.xml//mappersSQL映射文件SQL映射文件结构:resultMap–最复杂,也是最有力量的元素,用来描述如何从数据库结果集中来加载对象。sql–可以重用的SQL块,也可以被其他语句引用。insert–映射插入语句update–映射更新语句delete–映射删除语句select–映射查询语句SQL映射文件基本结构?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN!–namespace表示当前所有语句都属于该空间下,防止不同xml内的语句有相同的ID--mappernamespace=com.communityhealth.model.UserMapper/mapperSQL映射文件Select单条记录selectid=selectUserparameterType=intresultType=com.communityhealth.model.Userselect*fromdbo.userswhereusername=#{name}/select映射语句@TestpublicvoidtestSelect(){SqlSessionsession=MybatisUtil.getSqlSession();Stringname=李四;Userus=session.selectOne(com.communityhealth.model.UserMapper.selectUser,name);System.out.println(us.toString());session.close();}SQL映射文件SelectListselectid=selectUsersresultType=com.communityhealth.model.Userselect*fromdbo.users/select映射语句@TestpublicvoidtestSelectList(){SqlSessionsession=MybatisUtil.getSqlSession();ListUserusers=newArrayListUser();users=session.selectList(com.communityhealth.model.UserMapper.selectUsers);for(Useru:users){System.out.println(u);}}SQL映射文件Insertinsertid=insertUserparameterType=com.communityhealth.model.Userinsertintodbo.users(username,age)values(#{username},#{age})/insert映射语句@TestpublicvoidtestAdd(){SqlSessionsession=MybatisUtil.getSqlSession();Useruser=newUser(hhhh,124,32);intresult=session.insert(com.communityhealth.model.UserMapper.insertUser,user);session.commit();System.out.println(result);session.close();}SQL映射文件deletedeleteid=deleteUserparameterType=Stringdeletefromdbo.userswhereusername=#{username}/delete映射语句@TestpublicvoidtestDelete(){SqlSessionsession=MybatisUtil.getSqlSession();Stringstatement=com.communityhealth.model.UserMapper.deleteUser;intresult=session.delete(statement,hhhh);session.commit();System.out.println(result);session.close();}SQL映射文件updateupdateid=“updateUserparameterType=com.communityhealth.model.User“updatedbo.userssetage=#{age}whereusername=#{username}/update映射语句@TestpublicvoidtestUpdate(){SqlSessionsession=MybatisUtil.getSqlSession();Stringstatement=com.communityhealth.model.UserMapper.updateUser;Useruser=newUser(hhhh,30,32);intresult=session.update(statement,user);session.commit();System.out.println(result);session.close();}SQL映射文件表列名跟类属性名不一致1.使用别名selectid=selectClassesparameterType=StringresultType=com.communityhealth.model.ClassesselectclassNamecname,classnocnofromdbo.classeswhereclassNo=#{no}/select2.使用ResultMap映射resultMaptype=com.communityhealth.model.Classesid=classidproperty=cnocolumn=classno/resultproperty=cnamecolumn=className//resultMapselectid=selectClasses01parameterType=StringresultMap=classselectclassName,classnofromdbo.classeswhereclassNo=#{no}/selectSQL映射文件表列名跟类属性名不一致1.映射语句@TestpublicvoidtestDiffientNameAndColumn(){SqlSessionFactoryfactory=(SqlSessionFactory)newSqlSessionFactoryBuilder().build(TestMybatis.class.getClassLoader().getResourceAsStream(mybatis.xml));SqlSessionsession=factory.openSession();Stringno=01;Classescs=session.selectOne(com.communityhealth.model.UserMapper.selectClasses,no);System.out.println(cs.toString());Classescs1=session.selectOne(com.communityhealth.model.UserMapper.selectClasses01,no);System.out.println(cs1.toString());}SQL映射文件查询结果一对一1.Xml配置resultMaptype=com.communityhealth.model.Classesid=classesidproperty=cnocolumn=classno/resultprop