1. Download the mybatis and MySQL connecrt jar packages first
:
Link: Https://pan.baidu.com/s/1kVFfF8N Password: ypkb
Import jar package, MAVEN can directly configure POM
2. Create a Directory
1.configuration.xml
Create this mybatis configuration file in the root directory
The code is:
<?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> <typealiases> <!--set Typealias to use the app instead of every time you write the full app class path in Appmapper.xml - <Typealiastype= "domain. App "alias= "APP" /> </typealiases> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <TransactionManagertype= "JDBC" /> <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver"/> < Propertyname= "url"value= "Jdbc:mysql://222.201.145.215/nziotdb"/> < Propertyname= "username"value= "root"/> < Propertyname= "Password"value= "root"/> </DataSource> </Environment> </Environments> <mappers> <!--Configure the path to the Appmapper.xml - <MapperResource= "Mapperxml/appmapper.xml" /> </mappers> </Configuration>
The address, account number and password of the database server are configured, and the path of mapping file Mapperxml is set.
2. Create an entity class under domain
Packagedomain;Importjava.io.Serializable; Public classAppImplementsserializable{/** * */ PrivateString appId; PrivateString Appdesc; PrivateString newestversion; PrivateString Newestversiondesc; PrivateString URL; PrivateString MD5; PublicString getappid () {returnappId; } Public voidsetappid (String appId) { This. AppId =appId; } PublicString Getappdesc () {returnAppdesc; } Public voidSetappdesc (String appdesc) { This. Appdesc =Appdesc; } PublicString getnewestversion () {returnnewestversion; } Public voidsetnewestversion (String newestversion) { This. newestversion =newestversion; } PublicString Getnewestversiondesc () {returnNewestversiondesc; } Public voidSetnewestversiondesc (String newestversiondesc) { This. Newestversiondesc =Newestversiondesc; } PublicString GetUrl () {returnURL; } Public voidseturl (String url) { This. url =URL; } PublicString getMd5 () {returnMD5; } Public voidSetMd5 (String MD5) { This. MD5 =MD5; } PublicString toString () {return"App [appid=" + AppId + ", appdesc=" +Appdesc+ ", newestversion=" + Newestversion + ", newestversiondesc=" + Newestversiondesc + ", url=" + URL + ", MD5 = "+ MD5 +"] "; }}
This class represents a table, the variables within the class can be directly set why the fields in the table are consistent, the inconsistency will be in the mapping file to set the corresponding relationship
3. Write the mapping file under Mapperxml
<?XML version= "1.0" encoding= "UTF-8"?> <!DOCTYPE Mapper Public "-//MYBATIS.ORG/DTD Mapper 3.0" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Mapper." Appmapper "> <Resultmaptype= "APP"ID= "Appresultmap"> <ID Property= "AppId"column= "app_id" /> <result Property= "Appdesc"column= "App_desc" /> <result Property= "Newestversion"column= "Newest_version" /> <result Property= "Newestversiondesc"column= "Newest_version_desc" /> <result Property= "url"column= "url" /> <result Property= "MD5"column= "MD5" /> </Resultmap> <!--SELECT statement - <SelectID= "Selectuserbyid"ParameterType= "int"Resultmap= "Appresultmap">SELECT * from app_t where app_t.app_id = #{appid}</Select></Mapper>
The corresponding relationship is set up in Resultmap, which solves the problem of the variable of entity class and the field inconsistency in the database.
The specific SQL statement is also written in the mapping file, where the ID represents the corresponding method, ParameterType is the type of the passed variable, Resultmap is the type of data returned
4. The corresponding interface is established for each entity class under Mapper, and the method inside the interface corresponds to ID one by one inside the mapping file.
Packagemapper;Importjava.util.List;Importdomain. App; Public InterfaceAppmapper { PublicAPP Selectuserbyid (intID); PublicList<app>selectusersbyname (String userName); Public voidaddUser (App user); Public voidupdateUser (App user); Public voidDeleteUser (intID);}
5. Write a database query helper class for returning database objects
PackageDeal;Importdomain. App;ImportMapper. Appmapper;Importorg.apache.ibatis.session.SqlSession; Public classtest{ Public voidGetuserbyid (intUserID) {sqlsession session=sqlsessionhelper.getsessionfactory (). Opensession (); Try{appmapper useroperation=session. Getmapper (Appmapper.class); APP User=Useroperation.selectuserbyid (UserID); if(User! =NULL) {System.out.println (User.getappdesc ()); } } finally{session.close (); } } Public Static voidMain (string[] args) {Try{test test=NewTest (); Test.getuserbyid (1); } Catch(Exception e) {System.out.println (E.getmessage ()); } }}
The database tables are as follows, and you need to establish the tables in the database before using this tutorial
The query is app_id, corresponding to the APP_DESC
Run test, the resulting interface is also 3
6. Attach the demo project, not clear students can refer to
Link: Https://pan.baidu.com/s/1dFqVtSX Password: jb7j
How to use MyBatis to operate the MySQL database, Batis to delete and change