What is MyBatis?
MyBatis is an excellent persistence layer framework that supports the customization of SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually sets parameters and gets the result set. MyBatis can use simple XML or annotations for configuration and native maps to map interfaces and Java POJOs (Plain old Java Objects, ordinary Java objects) to records in a database.
Mybastis is a semi-automated ORM tool that makes the spelling of SQL in the config file XML, which is of course a big difference from the Entity Framework entityframework.
First use eclipse to build a Java project, database using MySQL, reference mybastis external is 3.4.1
First the establishment of the entity
Packagemodel; Public classStudent { PublicString name,address; Public intAge ; Publicstudent () {//constructor Function } PublicString getname () {return This. Name; } Public voidSetName (String name) { This. name=name; } PublicString getaddress () {return This. Address; } Public voidsetaddress (String address) { This. address=address; } Public intGetage () {return This. Age; } Public voidSetage (intAge ) { This. age=Age ; }}
Then configure config for the root directory
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-co Nfig.dtd "> <Configuration> <typealiases> <Typealiastype= "Model.student"alias= "Student" /> </typealiases> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <TransactionManagertype= "JDBC" /> <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver" /> < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/school?usessl=true" /> < Propertyname= "username"value= "root" /> < Propertyname= "Password"value= "123456" /> </DataSource> </Environment> </Environments> <mappers> <MapperResource= "Mapper/mappers.xml" /> </mappers> </Configuration>
And then to build the mapper.
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd"
> <Mappernamespace= "DAO." Studentmapper "> <SelectID= "Selectsudent"ParameterType= "String"Resulttype= "Student">Select name,age,address from student where Name=#{name}</Select> <InsertID= "Insertstudent"ParameterType= "Student">INSERT into student (name,age,address) value (#{name},#{age},#{address})</Insert> </Mapper>
When defining related interfaces
Package DAO; Import model.student; Public Interface studentmapper { / * * Returns a model*/public student Selectsudent (String Id); /* * Insert a data * /public void insertstudent (student model); }
PackageMybastis;Importjava.io.IOException;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;ImportDAO. Studentmapper;Importmodel.student; Public classStudentsmain {Private Staticsqlsessionfactory getsessionfactory () {sqlsessionfactory sessionfactory=NULL; String Resource= "Config + +"; Try{sessionfactory=NewSqlsessionfactorybuilder (). Build (Resources. Getresourceasreader (Resource)); } Catch(IOException e) {E.printstacktrace (); } returnsessionfactory; } Public Static voidMain (string[] args) {Sqlsession sqlsession =getsessionfactory (). Opensession (); Studentmapper Studentmapper= Sqlsession.getmapper (studentmapper.class); Student User= Studentmapper.selectsudent ("dddd"); System.out.println (user.getaddress ()+user.getage () +User.getname}}
To be continued .....
MyBatis Official website: http://www.mybatis.org/mybatis-3/zh/
MyBatis Simple Using