MyBatis简单源码分析1 - 环境搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis简单源码分析1 - 环境搭建相关的知识,希望对你有一定的参考价值。

本文以MyBatis独立使用的情形简单地分析MyBatis的源码,记录自己学习的过程

重要的Java代码如下:

主程序:

 1 package com.suntao.learning.debug;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 
12 import com.suntao.learning.dao.EmployeeDao;
13 import com.suntao.learning.dto.Employee;
14 
15 public class DebugMyBatis {
16     private final static String CONFIGURATION_XML = "config.xml";
17 
18     public static void main(String[] args) {
19         try {
20             InputStream inputStream = Resources.getResourceAsStream(CONFIGURATION_XML);
21             SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
22             SqlSession sqlSession = sqlSessionFactory.openSession(true);
23             EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
24             Employee employee = new Employee("test", "M", "XXXXXXXXXXX");
25             int ret = employeeDao.insertEmployee(employee);
26             System.out.println(ret);
27             List<Employee> employees = employeeDao.findAllEmployees();
28             for (Employee e : employees) {
29                 System.out.println(e);
30             }
31         } catch (IOException e) {
32             e.printStackTrace();
33         }
34     }
35 }

EmployeeDao.java:

 1 package com.suntao.learning.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.suntao.learning.dto.Employee;
 6 
 7 public interface EmployeeDao {
 8     Employee findEmployeeById(String employeeId);
 9     List<Employee> findAllEmployees();
10     int insertEmployee(Employee e);
11 }

Employee.java:

 1 package com.suntao.learning.dto;
 2 
 3 public class Employee {
 4     private int id;
 5     private String name;
 6     private String gender;
 7     private String tel;
 8 
 9     public Employee() {}
10     
11     public Employee(String name, String gender, String tel) {
12         this.name = name;
13         this.gender = gender;
14         this.tel = tel;
15     }
16 
17     public int getId() {
18         return id;
19     }
20 
21     public void setId(int id) {
22         this.id = id;
23     }
24 
25     public String getName() {
26         return name;
27     }
28 
29     public void setName(String name) {
30         this.name = name;
31     }
32 
33     public String getGender() {
34         return gender;
35     }
36 
37     public void setGender(String gender) {
38         this.gender = gender;
39     }
40 
41     public String getTel() {
42         return tel;
43     }
44 
45     public void setTel(String tel) {
46         this.tel = tel;
47     }
48 
49     @Override
50     public String toString() {
51         return id + " " + name + " " + gender + " " + tel;
52     }
53     
54 }

重要的配置文件如下:

config.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 3 <configuration>
 4     <properties resource="jdbc.properties"></properties>
 5     <!-- 配置别名 -->
 6     <typeAliases>
 7         <typeAlias type="com.suntao.learning.dto.Employee" alias="Employee" />
 8     </typeAliases>
 9 
10     <!-- 配置环境变量 -->
11     <environments default="development">
12         <environment id="development">
13             <transactionManager type="JDBC" />
14             <dataSource type="POOLED">
15                 <property name="driver" value="${driver}" />
16                 <property name="url" value="${url}" />
17                 <property name="username" value="${username}" />
18                 <property name="password" value="${password}" />
19             </dataSource>
20         </environment>
21     </environments>
22 
23     <!-- 配置mappers -->
24     <mappers>
25         <mapper resource="employee.xml" />
26     </mappers>
27 </configuration>

Mapper文件employee.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.suntao.learning.dao.EmployeeDao">
 4     <resultMap id="EmployeeMap" type="Employee">
 5         <result column="ID" jdbcType="INTEGER" property="id" />
 6         <result column="NAME" jdbcType="VARCHAR" property="name" />
 7         <result column="GENDER" jdbcType="VARCHAR" property="gender" />
 8         <result column="TEL" jdbcType="VARCHAR" property="tel" />
 9     </resultMap>
10     <select id="findEmployeeById" parameterType="string" resultType="Employee">  
11         select * from employee t where t.id = #{employeeId} 
12     </select>
13     <select id="findAllEmployees" resultType="Employee">  
14         select * from employee t
15     </select>
16     <insert id="insertEmployee" parameterType="Employee" useGeneratedKeys="true" keyProperty="id">
17         insert into employee
18         <trim prefix="(" suffix=")" suffixOverrides=",">
19             <if test="name != null">
20                 name,
21             </if>
22             <if test="gender != null">
23                 gender,
24             </if>
25             <if test="tel != null">
26                 tel,
27             </if>
28         </trim>
29         <trim prefix="values (" suffix=")" suffixOverrides=",">
30             <if test="name != null">
31                 #{name, jdbcType=VARCHAR},
32             </if>
33             <if test="gender != null">
34                 #{gender, jdbcType=VARCHAR},
35             </if>
36             <if test="tel != null">
37                 #{tel, jdbcType=VARCHAR},
38             </if>
39         </trim> 
40     </insert>
41 </mapper>

 

以上是关于MyBatis简单源码分析1 - 环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis源码解析-搭建调试环境

mybatis分析

mybatis分析

mybatis复习

mybatis拦截器源码分析

阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_03.jdbc操作数据库的问题分析