A long time ago to learn mybatis, it is said that many companies use this framework. I used to use the old dbcp,hibernate feeling too big, now
To learn this neutral frame.
First is the configuration of the environment, I use MAVEN to create the project, the Pom.xml file is as follows
<project xmlns= "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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.bird</groupId> <artifactid>concursey </artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name >concursey</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> <ver sion>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupi D>org.mybatis</groupid> <artifactId>mybatis</artifactId> <version>3.2.7</version> </dependency> <dependency> <groupId>mysql</groupId> <a
Rtifactid>mysql-connector-java</artifactid> <version>5.1.32</version> </dependency>
</dependencies> </project>
You have to create a database and a corresponding table to do a demonstration, here is not written, directly on the corresponding JavaBean
Package Com.bird.mybatis.bean;
public class Users {
private int id;
private String name;
private int age;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
@Override public
String toString () {return
"Users [id= + ID +", name= "+ name +", age= "+ Age +"] ";
}
}
Then you create the corresponding Mapper.xml file for the corresponding JavaBean, as follows
<?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" >
<mapper namespace= "Com.bird.mybatis.bean.userMapper" >
<!--get the corresponding value based on the ID -->
< Select Id= "GetUser" parametertype= "int" resulttype= "com.bird.mybatis.bean.Users" >
select * from Users where id = #{id}
</select>
</mapper>
Of course there are mybatis global files, conf.xml content as follows
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE configuration Public
"-//mybatis.org//dtd Config 3.0//en"
"http://mybatis.org/dtd/ Mybatis-3-config.dtd ">
<configuration>
<!--development: Development mode work: Working mode-->
< Environments default= "Development" >
<environment id= "Development" >
<transactionmanager type= " JDBC "/>
<datasource type=" Pooled "> <property name=" Driver "value=" Com.mysql.jdbc.Driver "
/ >
<property name= "url" value= "Jdbc:mysql://localhost:3306/mybatis"/>
<property Username "value=" root "/> <property name=" password "value="
root "/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource= "Com/bird/mybatis /bean/usermapper.xml "/>
</mappers>
</configuration>
Finally, the test code
Package Com.bird.mybatis.bean;
Import Java.io.Reader;
Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import org.apache.ibatis.session.SqlSessionFactory;
Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Test {public
static void Main (string[] args) throws Exception {
String resource = "Conf.xml";
Reader reader = Resources.getresourceasreader (Resource);
Sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). build (reader);
sqlsession session = Sessionfactory.opensession ();
String statement = "Com.bird.mybatis.bean.userMapper.getUser";
Users user = Session.selectone (statement, 1);
SYSTEM.OUT.PRINTLN (user);
}
In general, it's very simple.