MyBatis demo

  1. 導 jar 包:mybatis & mysql 驅動

  2. 建立數據庫、表、插入值
  3. 全局配置文件: mybatis-config.xml
    <?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://localhost:3306/mybatis"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="EmployeeMapper.xml"/>
        </mappers>
    </configuration>

     

  4. 建立數據類型
    Emplyee.java

  5. 建立映射文件

    public interface EmployeeMapper {
    	public Employee getEmpById(Integer id);
    }

     

  6. 設置映射關係

    <?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="EmployeeMapper">  <!-- 任意。如果是接口式,必須是 mapper 文件的全限定名 -->
    
        <select id="getEmpById" resultType="Employee"> <!-- 任意。如果是接口式,必須是方法名 -->
    		select id,last_name lastName,email,gender from employee where id = #{id}
    	</select>
    
    </mapper>

     

  7. 測試

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    public class MyBatisTest {
      public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
      }
    
      @Test
      public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
          Employee employee = openSession.selectOne("EmployeeMapper.getEmpById", 1);
          System.out.println(employee);
        } finally {
          openSession.close();
        }
      }
    
      @Test
      public void test01() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
          EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
          Employee employee = mapper.getEmpById(1);
          System.out.println(employee);
        } finally {
          openSession.close();
        }
      }
    
    }

     

  8. PS:

    1. SqlSession 和 connection 都是非線程安全的,每次使用都應該重新獲取新的對象。

    2.  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章