mybatis basic use

  MyBatis is an open source project of apache , iBatis. In 2010 , the project was migrated from apache software foundation to google code and renamed MyBatis . Migrated to Github in November 2013 .

    MyBatis is an excellent persistence layer framework . It encapsulates the process of operating the database in jdbc , so that developers only need to pay attention to SQL itself, and do not need to spend energy on registering drivers, creating connections , creating statements , manually setting parameters, Result set retrieval and other jdbc complex process code.

  Mybatis configures the various statements to be executed ( statement , preparedStatemnt , CallableStatement ) through xml or annotation, and generates the final executed sql statement by mapping the java object and the sql in the statement, and finally the mybatis framework executes the sql and converts the result Map to java object and return

Directory Structure

1. Guide package

  Import the mybatis package and its dependent lib packages

2. Create the corresponding entity class according to the table

3. Create the configuration file sqlMapConfig.xml of mybatis

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- 和spring整合后 environments配置将废除 -->
 7     <environments default="development">
 8         <environment id="development">
 9             <!-- 使用jdbc事务管理 -->
10             <transactionManager type="JDBC" />
11             <!-- 数据库连接池 -->
12             <dataSource type="POOLED">
13                 <property name="driver" value="com.mysql.jdbc.Driver" />
14                 <property name="url"
15                     value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
16                 <property name="username" value="root" />
17                 <property name="password" value="root" />
18             </dataSource>
19         </environment>
20     </environments>
21     
22     <!-- 加载映射文件 -->
23     <mappers>
24         <mapper resource="cn/tx/mapper/PersonTestMapper.xml"/>
25     </mappers>
26 </configuration>

 

Second, configure the mapping file PersonTestMapper.xml

1  <? xml version="1.0" encoding="UTF-8" ?> 
2  <! DOCTYPE mapper
 3  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4  "http://mybatis.org /dtd/mybatis-3-mapper.dtd" > 
5  <!-- namespace: namespace, used to isolate sql, the full path of the specification map file package --> 
6  < mapper namespace ="cn.tx.mapper.PersonTestMapper " > 
7      <!-- resultMap is used to resolve the inconsistency between attributes and column names in the database table --> 
8      < resultMap type ="cn.tx.model.Person" id ="BaseResultMapper" >
 9         <id column="id" property="id"/>
10         <result column="name" property="name"/>
11         <result column="gender" property="gender"/>
12         <result column="address" property="address"/>
13         <result column="birthday" property="birthday"/>
14     </resultMap>
15     <!--  
17        id: sql statement unique identifier
16          parameterType:参数的类型
18         resultType:返回结果的类型
19      -->
20     <select id="selectPersonById" parameterType="java.lang.Integer" resultType="cn.tx.model.Person">
21         select * from person_test where id = #{id}
22     </select>
23     
24     <select id="selectPersonCount" resultType="java.lang.Integer">
25         select count(*) from person_test
26     </select>
27 </mapper>

3. Run the test

 1 public class TestMyBatis {
 2     private SqlSessionFactory sqlSessionFactory;
 3     @Before
 4     public  void setUp() throws Exception {
 5         //读取配置文件
 6         InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
 7         //创建sqlSessionFactory
 8         sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 9     }
10 
11     @Test
12     public void selectPersonById() {
13         //获得sqlSession
14         SqlSession sqlSession = sqlSessionFactory.openSession();
15         try {
16             Person person = sqlSession.selectOne("cn.tx.mapper.PersonTestMapper.selectPersonById", 1);
17             System.out.println(person);
18         } catch (Exception e) {
19             e.printStackTrace();
20         }finally {
21             sqlSession.close();
22         }
23     }
24     @Test
25     public void selectPersonCount(){
26         SqlSession sqlSession = sqlSessionFactory.openSession();
27         try {
28             Integer count = sqlSession.selectOne("cn.tx.mapper.PersonTestMapper.selectPersonCount");
29             System.out.println(count);
30         } catch (Exception e) {
31             e.printStackTrace();
32         }finally {
33             sqlSession.close();
34         }
35     }
36 
37 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324847604&siteId=291194637