1, first create a project, add the following jar configuration under Pom.xml
<!--MyBatis Core package--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependenc
y> <!--mysql driver package--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> &L t;/dependency> <!--junit Test Kit--> <dependency> <groupid>junit</groupid&
Gt <artifactId>junit</artifactId> <version>4.11</version> <SCOPE>TEST&L T;/scope> </dependency> <!--log file Management Pack--> <dependency> <groupid >log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</ver Sion>
</dependency> <dependency> <groupId>org.slf4j</groupId> & Lt;artifactid>slf4j-api</artifactid> <version>1.7.12</version> </dependency>
; <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-log4j12</ar Tifactid> <version>1.7.12</version> </dependency>
2, under Src/java A new package Com.hgc.pojo, and then under the package to create a Java file, filename user, add code as follows
Package Com.hgc.pojo;
public class User {
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;
}
}
3, create a folder under the Src/main/resources Mapper, under the folder to create a Usermapper.xml file, the following code
<?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.hgc.pojo.User" >
<select id= "FindByID" parametertype= "int" resulttype= "Com.hgc.pojo.User" >
SELECT * from User WHERE id=#{id}
</select>
</mapper>
4, create the Mysql.properties file under the Src/main/resources, the code is as follows
Jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://192.168.9.248:3306/mybatis
jdbc.username=root
jdbc.password=mysql@123456
5, create the Mybatis-config.xml file under the Src/main/resources, the code is 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> <properties resource=" mysql.properties "/> <settings> <!--global Sex setting lazy load. If set to ' false ', all associated are initialized to load, the default value is false--> <setting name= "lazyloadingenabled" value= "true"/> <!- -When set to ' true ', lazy loaded objects may be loaded with any lazy properties. Otherwise, each property is loaded on demand. The default value is true--> <setting name= "aggressivelazyloading" value= "false"/> </settings> <typealias Es> <!--is actually replacing the bean with a short name--> <typealias type= "Com.hgc.pojo.User" alias= "User"/> < /typealiases> <!--the management of transactions and the configuration of connection pools--> <environments default= "Development" > <environment id= "Development" > <transactionmanager type= "JDBC" ></transactionManager> <datasource Type= "Pooled" ><!--pooled: Using MyBatis fromWith a database connection pool to manage database connections--> <property name= "Driver" value= "${jdbc.driver}"/> <property
Name= "url" value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.username}"/> <property name= "Password" value= "${jdbc.password}"/> </dataSource> </environment&
Gt </environments> <!--mapping file path configuration--> <mappers> <mapper resource= "Mapper/usermapper.xm L "/> </mappers> </configuration>
6, create a database MyBatis, set up a table user, the code is as follows
CREATE TABLE ' User ' (
' id ' int () NOT NULL auto_increment,
' name ' varchar (255) DEFAULT null,
' age ' int (one) DE FAULT NULL,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=3 DEFAULT Charset=utf8;
INSERT into ' User ' VALUES (1, ' Test ',);
INSERT into ' User ' VALUES (2, ' John ', 25);
7, under the Src/main/java to create a package com.hgc.test, under the package to create Usertest.java files, the following code
Package test;
Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import org.apache.ibatis.session.SqlSessionFactory;
Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
Import Com.hgc.pojoUser;
Import java.io.IOException;
Import Java.io.Reader;
/**
* Created by 47500 on 2017/8/28.
*/Public
class Usertest {public
static void Main (string[] args) {
String resource = "Mybatis-config.xml" ;
Reader reader = null;
try {
reader = Resources.getresourceasreader (Resource);
Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). build (reader);
sqlsession session = Sqlsessionfactory.opensession ();
User user = Session.selectone ("FindByID", 2);
Session.commit ();
System.out.println (User.getname ());
} catch (IOException e) {
e.printstacktrace ();}}}
8, the final test whether there is data output