---Direct code placement
(1) User.java
Package me.gacl.domain;
/**
* @author GaCl
* The entity class corresponding to the Users table
*/
public class User {
property of the entity class and the table field name one by one correspond
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.) {
This.age = age;
}
@Override
Public String toString () {
Return "User [id=" + ID + ", name=" + name + ", age=" + Age + "]";
} }
(2) Usermapper.xml
<?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" >
<!--specifying a unique Namespace,namespace value for this mapper is customarily set to the package name +sql map file name, so that the value of namespace is guaranteed to be unique
For example namespace= "Me.gacl.mapping.userMapper" is me.gacl.mapping (package name) +usermapper (usermapper.xml file removal suffix)
-
<mapper namespace= "Me.gacl.mapping.userMapper" >
<!--The SQL statement that writes the query in the Select tab, set the ID property of the Select tag to be unique to the Getuser,id property value and cannot be repeated
Use the ParameterType property to indicate the type of parameter used when querying, and the Resulttype property to indicate the type of result set returned by the query
Resulttype= "Me.gacl.domain.User" means the object that encapsulates the query result as a User class returns
The user class is the entity class corresponding to the Users table
-
<!--
Get a user object based on ID query
-
<select id= "GetUser" parametertype= "int"
Resulttype= "Me.gacl.domain.User" >
SELECT * from T_user where Id=#{id}
</select>
</mapper>
(3) Conf.xml
<?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>
<environments default= "Development" >
<environment id= "Development" >
<transactionmanager type= "JDBC"/>
<!--Configuring database connection Information--
<datasource type= "Pooled" >
<property name= "Driver" value= "Oracle.jdbc.driver.OracleDriver"/>
<property name= "url" value= "Jdbc:oracle:thin:@132.224.24.118:1521:jsfx"/>
<property name= "username" value= "Sett_analyse"/>
<property name= "Password" value= "G4el_yj_ha"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--register Usermapper.xml file,
Usermapper.xml is located under Me.gacl.mapping This package, so resource written me/gacl/mapping/usermapper.xml-->
<mapper resource= "Me/gacl/mapping/usermapper.xml"/>
</mappers>
</configuration>
(4) Test1.java
Package me.gacl.test;
Import java.io.IOException; Import Java.io.InputStream; Import Java.io.Reader; Import Me.gacl.domain.User; 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 Test1 {
public static void Main (string[] args) throws IOException {
Configuration file for MyBatis
String resource = "Conf.xml";
Load the MyBatis configuration file using the ClassLoader (it also loads the associated mapping file)
InputStream is = Test1.class.getClassLoader (). getResourceAsStream (Resource);
Building a factory in sqlsession
Sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). Build (IS);
Use the resources class provided by MyBatis to load the MyBatis configuration file (It also loads the associated mapping file)
Reader reader = Resources.getresourceasreader (Resource);
Building a factory in sqlsession
Sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). build (reader);
Create a sqlsession that can execute SQL in the mapping file
sqlsession session = Sessionfactory.opensession ();
/** * Map the identity string of SQL,
* Me.gacl.mapping.userMapper is the value of the namespace attribute of the mapper tag in the Usermapper.xml file,
* GetUser is the id attribute value of the select tag, and the value of the id attribute of the SELECT tag can be used to find the SQL to execute
*/
String statement = "Me.gacl.mapping.userMapper.getUser";//Mapping SQL identification string
Executes a query that returns the SQL for a unique user object
User user = Session.selectone (statement, 1);
SYSTEM.OUT.PRINTLN (user);
} }
MyBatis Simple Example