Recently, the Mybatis framework was introduced in my work, so I also built a Mybatis environment locally for learning.
1. Development environment preparation
Intellij idea 15.0.2
jdk1.6.0_21
oracle11g
Reference: "Mybatis from entry to mastery" - Liu Zenghui
2. Setting up the environment
Create a new maven project on idea and get the following directory structure:
The relevant maven configuration and dependencies are introduced into the pom.xml file (the mybatis package and the log4j related log jar package are introduced) as follows:
Because the connection is to the oracle database, the introduction of the relevant maven configuration ojdbc14 does not take effect. Baidu said that the oracle will be charged, so the remote maven cannot be downloaded. To save trouble, I directly downloaded the jar package and introduced it into the project. The final project structure is as follows:
3. Write a demo of a simple query
3.1 Configure mybatis-config.xml
3.2 There is a country table in the database
3.3 Create entity class simple/model/Country.java
3.4 Create mapper configuration file simple/mapper/CountryMapper.xml
3.5 Create interface class simple/mapper/CountryMapper
3.6 Create test class simple/mapper/CountryMapperTest.java
The final project directory structure is as follows:
Execute
the and get the following results: