MyBatis如何入门以及实现一个简单的增删改查
工具/原料
mybatis
eclipse
方法/步骤
1、首先新建一个MyBatisMaven项目
2、新建一个Employee类。pac氯短赤亻kagecom.gwolf.bean;publicclassEmployeei罪焐芡拂mplementsjava.io.Serializable{privateIntegerempId;privateStringempName;privateStringemail;privateStringgender;publicIntegergetEmpId(){returnempId;}publicvoidsetEmpId(IntegerempId){this.empId=empId;}publicStringgetEmpName(){returnempName;}publicvoidsetEmpName(StringempName){this.empName=empName;}publicStringgetEmail(){returnemail;}publicvoidsetEmail(Stringemail){this.email=email;}publicStringgetGender(){returngender;}publicvoidsetGender(Stringgender){this.gender=gender;}@OverridepublicStringtoString(){return"Employee[empId="+empId+",empName="+empName+",email="+email+",gender="+gender+"]";}}
3、在maven配置文件中添加mybatis和mysql数据库驱动的依赖包。并且为了能驴织裕篡够看到sql的打印日志,我们还需要加入log4枣娣空郅j依赖包。<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gwolf</groupId><artifactId>Mybatis3</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>Mybatis3</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!--MyBatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies></project>
4、创建Employee映射的xml配置文件:EmployeeMapper.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.gwolf.dao.EmployeeMapper"><!--namespace:名称空间id:唯一标识resultType:返回值类型#{empId}:从传递过来的参数取出empId值--><selectid="selectEmployee"resultType="com.gwolf.bean.Employee">select*fromtbl_empwhereemp_id=#{empId}</select></mapper>
5、创建一个mybatis-config.xml文件,用于创建SqlSessionFacto筠续师诈ry:<?xmlversion=&qu泠贾高框ot;1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environmentsdefault="development"><environmentid="development"><transactionManagertype="JDBC"/><dataSourcetype="POOLED"><propertyname="driver"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/ssm_crud"/><propertyname="username"value="root"/><propertyname="password"value="root"/></dataSource></environment></environments><mappers><mapperresource="EmployeeMapper.xml"/></mappers></configuration>
6、新建一个java测试类AppTest,用户测试mybatis的增删改查:创建一个SqlSessionFactory对象。packagecom.gwolf;importjava.io.InputStream;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Test;publicclassAppTest{@Testpublicvoidtest()throwsException{Stringresource="mybatis-config.xml";InputStreaminputStream=Resources.getResourceAsStream(resource);SqlSessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder().build(inputStream);}}
7、下一步是从SqlSessionFactory中获取SqlSession,能直接执行已经映射的sql语句。packagecom.gwolf;importjava.io.InputStream;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Test;importcom.gwolf.bean.Employee;publicclassAppTest{@Testpublicvoidtest()throwsException{Stringresource="mybatis-config.xml";InputStreaminputStream=Resources.getResourceAsStream(resource);SqlSessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder().build(inputStream);SqlSessionsqlSession=sqlSessionFactory.openSession();try{Employeeemployee=sqlSession.selectOne("com.gwolf.dao.EmployeeMapper.selectEmployee",1);System.out.println(employee);}finally{sqlSession.close();}}}
8、执行单元测试,查看程序运行结果: