I finally figured it out today. I just learned Java, and it was quite difficult.
Create a database mygps in MySQL and the table name is test (ID, age, username, password)
(1) first reference mybatis-3.0.2.jar, mysql-connector-java-5.0.8-bin.jar
(2) configure the XML file used to connect to the database named config. 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"/>
<Datasource
Type = "pooled">
<Property
Name = "driver" value = "com. MySQL. JDBC. Driver"/>
<Property
Name = "url"
Value = "JDBC: mysql: // localhost: 3306/mygps"/> // The Database Name Is mypgs.
<Property
Name = "username" value = "root"/>
<Property
Name = "password"
Value = "root"/>
</Datasource>
</Environment>
</Environments>
<Mappers>
<Mapper
Resource = "org/mybatis/test/tew.apper. xml"/> // write SQL statement XML
</Mappers>
</Configuration>
(3) The XML for configuring SQL statements is named tew.apper. 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
">
<Mapper
Namespace = "tew.apper">
<Select id = "selectbyid"
Parametertype = "int"
Resulttype = "org. mybatis. Test. Test"> // test is a class used to set database-related data types.
Select * from test where
Test. ID = # {ID}
</SELECT>
<Insert id = "insertvalues"
Parametertype = "org. mybatis. Test. Test">
Insert into test (ID, username, password, age) Values
(# {ID}, # {username}, # {password}, # {age })
</Insert>
<Update id = "updatevalue"
Parametertype = "org. mybatis. Test. Test">
Update Test Set username =#{ username} Where ID =#{ ID}
</Update>
</Mapper>
(4) The database connection is named util, which is a class.
Package org. mybatis. test;
Import java. Io. ioexception;
Import java. Io. reader;
Import org. Apache. ibatis. Io. Resources;
Import org. Apache. ibatis. session. sqlsessionfactory;
Import org. Apache. ibatis. session. sqlsessionfactorybuilder;
Public class util {
Public static sqlsessionfactory
Sqlmapper = NULL;
Static
{
String
Rs = "org/mybatis/test/config. xml"; // indicates the configuration file name in step 2.
Reader reader = NULL;
Try
{
Reader = resources. getresourceasreader (RS );
}
Catch (ioexception E)
{
E. printstacktrace ();
}
Sqlmapper = new
Sqlsessionfactorybuilder (). Build (Reader );
}
Public static sqlsessionfactory
Getsqlsessionfactory ()
{
Return sqlmapper;
}
}
(5) execute the SQL statement named gettest as a class.
Package org. mybatis. test;
Import org. mybatis. Test. util;
Import org. mybatis. Test. Test;
Import org. Apache. ibatis. session. sqlsession;
Public class gettest {
Public test getvalue (int
ID)
{
Sqlsession
Session =
Util. getsqlsessionfactory (). opensession ();
Test Ts = NULL;
Try
{
TS = (TEST) Session. selectone ("temo-apper. selectbyid", ID); // select
ID
}
Catch (exception E)
{
E. printstacktrace ();
}
Finally
{
Session. Close ();
}
Return ts;
}
Public void insertvalue (int id, string
Username, string password, int age)
{
Sqlsession session =
Util. getsqlsessionfactory (). opensession ();
Test Ts = new test ();
TS. setid (ID );
TS. setusername (username );
TS. setpassword (password );
TS. setage (AGE );
Try
{
Session. selectone ("testmapper. insertvalues", TS);) // insert
ID
}
Catch (exception
E)
{
E. printstacktrace ();
}
Finally
{
Session. Close ();
}
}
Public void updatevalue (int id, string
Username)
{
Sqlsession session =
Util. getsqlsessionfactory (). opensession ();
Test Ts = new test ();
TS. setid (ID );
TS. setusername (username );
Try
{
Session. selectone ("testmapper. updatevalue", TS);) // update
ID
}
Catch (exception
E)
{
E. printstacktrace ();
}
Finally
{
Session. Close ();
}
}
}
(6) Compile the test class for the data source
Package org. mybatis. test;
Public class test {
Private int ID;
Private int age;
Private string username;
Private string password;
Public int GETID ()
{
Return this. ID;
}
Public void setid (int id)
{
This. ID = ID;
}
Public int getage ()
{
Return this. Age;
}
Public void setage (INT age)
{
This. Age = age;
}
Public String GetUserName ()
{
Return this. Username;
}
Public void setusername (string username)
{
This. Username = username;
}
Public String GetPassword ()
{
Return this. Password;
}
Public void setpassword (string password)
{
This. Password = password;
}
}