—————————————————————————————1MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(PlainOldJavaObjects,普通的Java对象)映射成数据库中的记录.JDBC-dbutils(自动封装)-MyBatis-Hibernate编写第一个基于mybaits的测试例子:2.1.添加jar包【mybatis】mybatis-3.1.1.jar【MYSQL驱动包】xml—————————————————————————————2mysql-connector-java-5.1.7-bin.jar2.2.建库+表createdatabasemybatis;usemybatis;CREATETABLEusers(idINTPRIMARYKEYAUTO_INCREMENT,NAMEVARCHAR(20),ageINT);INSERTINTOusers(NAME,age)VALUES('Tom',12);INSERTINTOusers(NAME,age)VALUES('Jack',11);2.3.添加Mybatis的配置文件conf.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEconfigurationPUBLIC-//mybatis.org//DTDConfig3.0//EN=developmentenvironmentid=developmenttransactionManagertype=JDBC/dataSourcetype=POOLEDpropertyname=drivervalue=com.mysql.jdbc.Driver/propertyname=urlvalue=jdbc:mysql://localhost:3306/mybatis/propertyname=usernamevalue=root/propertyname=passwordvalue=root//dataSource/environment/environments/configuration2.4.定义表所对应的实体类publicclassUser{privateintid;privateStringname;privateintage;//get,set方法}—————————————————————————————32.5.定义操作users表的sql映射文件userMapper.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN=com.tocean.mybatis_test.test1.userMapperselectid=getUserparameterType=intresultType=com.tocean.mybatis_test.test1.Userselect*fromuserswhereid=#{id}/select/mapper2.6.在conf.xml文件中注册userMapper.xml文件mappersmapperresource=com/tocean/mybatis_test/test1/userMapper.xml//mappers2.7.编写测试代码:执行定义的select语句publicclassTest{publicstaticvoidmain(String[]args)throwsIOException{Stringresource=conf.xml;//加载mybatis的配置文件(它也加载关联的映射文件)Readerreader=Resources.getResourceAsReader(resource);//构建sqlSession的工厂SqlSessionFactorysessionFactory=newSqlSessionFactoryBuilder().build(reader);//创建能执行映射文件中sql的sqlSessionSqlSessionsession=sessionFactory.openSession();//映射sql的标识字符串Stringstatement=com.tocean.mybatis.bean.userMapper+.selectUser;//执行查询返回一个唯一user对象的sqlUseruser=session.selectOne(statement,1);System.out.println(user);}}—————————————————————————————43.1.XML的实现1).定义sql映射xml文件:insertid=insertUserparameterType=com.tocean.ibatis.bean.Userinsertintousers(name,age)values(#{name},#{age});/insertdeleteid=deleteUserparameterType=intdeletefromuserswhereid=#{id}/deleteupdateid=updateUserparameterType=com.tocean.ibatis.bean.Userupdateuserssetname=#{name},age=#{age}whereid=#{id}/updateselectid=selectUserparameterType=intresultType=com.tocean.ibatis.bean.Userselect*fromuserswhereid=#{id}/selectselectid=selectAllUsersresultType=com.tocean.ibatis.bean.Userselect*fromusers/select2).在config.xml中注册这个映射文件mapperresource=com/tocean/ibatis/bean/userMapper.xml/3).在dao中调用:publicUsergetUserById(intid){SqlSessionsession=sessionFactory.openSession();Useruser=session.selectOne(URI+.selectUser,id);returnuser;}—————————————————————————————53.2.注解的实现1).定义sql映射的接口publicinterfaceUserMapper{@Insert(insertintousers(name,age)values(#{name},#{age}))publicintinsertUser(Useruser);@Delete(deletefromuserswhereid=#{id})publicintdeleteUserById(intid);@Update(updateuserssetname=#{name},age=#{age}whereid=#{id})publicintupdateUser(Useruser);@Select(select*fromuserswhereid=#{id})publicUsergetUserById(intid);@Select(select*fromusers)publicListUsergetAllUser();}2).在config中注册这个映射接口mapperclass=com.tocean.ibatis.crud.ano.UserMapper/3).在dao类中调用publicUsergetUserById(intid){SqlSessionsession=sessionFactory.openSession();UserMappermapper=session.getMapper(UserMapper.class);Useruser=mapper.getUserById(id);returnuser;}—————————————————————————————64.1.连接数据库的配置单独放在一个properties文件中##db.propertiespropertiesresource=db.properties/propertyname=drivervalue=${driver}/propertyname=urlvalue=${url}/propertyname=usernamevalue=${username}/propertyname=passwordvalue=${password}/4.2.为实体类定义别名,简化sql映射xml文件中的引用typeAliasestypeAliastype=com.tocean.ibatis.bean.Useralias=_User//typeAliases4.3.可以在src下加入log4j的配置文件,打印日志信息1.添加jar:log4j-1.2.16.jar2.1.log4j.properties(方式一)log4j.properties,log4j.rootLogger=DEBUG,Console#Consolelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=%d[%t]%-5p[%c]-%m%nlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.org.apache=INFOlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUG2.2.log4j.xml(方式二)—————————————————————————————7?xmlversion=1.0encoding=UTF-8?!DOCTYPElog4j:configurationSYSTEMlog4j.dtdlog4j:configurationxmlns:log4j==STDOUTclass=org.apache.log4j.ConsoleAppenderlayoutclass=org.apache.log4j.PatternLayoutparamname=ConversionPatternvalue=%-5p%d{MM-ddHH:mm:ss,SSS}%m(%F:%L)\n//layout/appenderloggername=java.sqllevelvalue=debug//loggerlo