MyBatis no need for me to reports, this series is pure dry, with no nonsense.
1. Create a maven project, introduced mysql maven drive and mybatis of reference
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18 </version> </dependency>
2. Write mybatis configuration files are placed in the following resources directory name at random
<?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="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> </mappers> </configuration>
SqlSessionFactory 3. Create a way to use XML
// your profile name
String Source = "the mybatis-config.xml" ;
// load configuration file InputStream inputStream = Resources.getResourceAsStream (Source); SqlSessionFactory SqlSessionFactory = new new . The SqlSessionFactoryBuilder () Build (inputStream);
4. Create an entity class to complete a basic query
Create a JavaBean-Blog
public class Blog { private int id; private String name; private String remark; 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 String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return this.name+this.remark; } }
Blog creation corresponding Mapper interfaces BlogMapper, define a query method within an interface
public interface BlogMapper { public Blog selectBlog(int id); }
Creating BlogMapper corresponding XML mapping file BlogMapper
namespace to be consistent with the interface name
id select a node and to a method name as defined in the interface
resultType entity type is set to return
<?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="BlogMapper"> <select id="selectBlog" resultType="Blog"> select * from Blog where id=#{id} </select> </mapper>
Then we need to write our new mapper BlogMapper the first step in setting the node configuration file
<mappers> <mapper resource="BlogMapper.xml"></mapper> </mappers>
5. Create a Blog above and the same table in the database and arranged to increment id, adding a test data
Write the following code in the main process, reads from the database and the corresponding data is output to the console
public static void main(String[] str) throws IOException { String source = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(source); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); Blog blog = mapper.selectBlog(1); System.out.println( blog); }
This article describes the Mybatis to read from the factory to create a basic flow of data from the database, the next will be described in detail the role of each node.