MyBatis avoids almost all JDBC code and manual setting of parameters and getting result sets. But the cost is to need tedious configuration, but these configurations are almost the same, but they are very complex. With the help of plug-ins, you can write configuration files more easily, or record the configured templates, and then copy them directly.
Install mybatis
You can go to mybatis github download page (ladder may be required) download the jar package of mybatis and send a manual. It can also be downloaded directly with maven. Copy the following code to maven's configuration file.
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency> //The latest version is 3.5.4 (February 24, 2020)
Configure mybatis
Take the city table in the world database of mysql as an example
First, create a java project,
Import the jar package of mybatis and mysql
Create the following files in turn
Files to create | Effect |
---|---|
Class xxx.class mapped to table | Create following the elements of the table, and map with the table |
Configuration file config.xml | Information about the configuration database and the configuration files to be loaded |
map file xxxMapper.xml | Mapping with sql statements |
Test class xxx.class | Run sql statement |
Document structure of the whole project
Create a package in src,
Then create the class City.class mapped to the table in the package. First, look at the columns in city.
Then create the mapping class according to the column, which contains the constructor, the set and get methods of the element, and the tostring method.
import java.util.Arrays; public class City { private int[] ID; private String name; private String CountryCode; private String District; private int[] Population; public City() { } public City(int[] iD, String name, String countryCode, String district, int[] population) { ID = iD; this.name = name; CountryCode = countryCode; District = district; Population = population; } public int[] getID() { return ID; } public void setID(int[] iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCountryCode() { return CountryCode; } public void setCountryCode(String countryCode) { CountryCode = countryCode; } public String getDistrict() { return District; } public void setDistrict(String district) { District = district; } public int[] getPopulation() { return Population; } public void setPopulation(int[] population) { Population = population; } @Override public String toString() { return "City [ID=" + Arrays.toString(ID) + ", name=" + name + ", CountryCode=" + CountryCode + ", District=" + District + ", Population=" + Arrays.toString(Population) + "]"; } }
Create another mapping file CityMapper.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="mybatis.City"><!-- Change namespace to its own mapping class --> <select id="queryCityByName" resultType="mybatis.City" parameterType="String"> <!-- resultType Must use full class name --> select * from city where name= #{name} </select> </mapper>
Create the configuration file config.xml outside the package. Here is the template.
<?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://Server address: port number / database name" /> <!-- mysql Version 8.0 The next one should be url Add in the back?serverTimezone=GMT%2B8 --> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis/CityMapper.xml" /><!-- Change to your own mapping file --> </mappers> </configuration>
Create Test class in package
package mybatis; import java.io.IOException; 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 IOException { //Load profile Reader reader =Resources.getResourceAsReader("config.xml"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); //Equivalent to the connection in JDBC SqlSession session =sqlSessionFactory.openSession(); String statment ="mybatis.City.queryCityByName";//namespace.id in the mapping file City city=session.selectOne(statment,"Kabul"); System.out.println(city); session.close(); } }
Output results after running
Some complex data types return null, which will be solved later.