01—mybatis之Helloworld

2018-06-27  本文已影响0人  Lesie_zwc

1、ORM框架发张历史

image

2、Helloword编写

2-1、建立数据表和对应的javabean

CREATE TABLE tbl_employee(
    id INT(11) PRIMARY KEY AUTO_INCREMENT,
    last_name VARCHAR(255),
    gender CHAR(1),
    email VARCHAR(255)
)
public class Employee {
     
     private Integer id;
     private String lastName;
     private String email;
     private String gender;
     // get and set
}

2-2、修改全部配置文件 mybaits-config.xml

<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="123456" />
              </dataSource>
         </environment>
     </environments>
     <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
     <mappers>
         <mapper resource="EmployeeMapper.xml" />
     </mappers>
</configuration>

2-3、编写mapper接口以及相应的mapper.xml

public interface EmployeeMapper {
     public Employee getEmpById(Integer id);
}
<!--
namespace:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
 -->

<mapper namespace="com.leslie.dao.EmployeeMapper">
     <select id="getEmpById" resultType="com.leslie.bean.Employee">
         select id,last_name lastName,email,gender from tbl_employee where id = #{id}
     </select>
</mapper>

2-4、测试代码

/**
 * 1、接口式编程
 * 原生:      Dao      ====>  DaoImpl
 * mybatis:  Mapper   ====>  xxMapper.xml
 * <p>
 * 2、SqlSession代表和数据库的一次会话;用完必须关闭;
 * 3、SqlSession和connection一样她都是非线程安全。每次使用都应该去获取新的对象。
 * 4、mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象。
 * (将接口和xml进行绑定)
 * EmployeeMapper empMapper =     sqlSession.getMapper(EmployeeMapper.class);
 * 5、两个重要的配置文件:
 * mybatis的全局配置文件:包含数据库连接池信息,事务管理器信息等...系统运行环境信息
 * sql映射文件:保存了每一个sql语句的映射信息:
 * 将sql抽取出来。
 */
public class MyBatisTest {
    /**
     *  根据xml配置文件(全局配置文件)创建一个sqlSessionFactory对象
     * @return
     * @throws IOException
     */
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }
    /**
     * 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息
     * 2、sql映射文件;配置了每一个sql,以及sql的封装规则等。
     * 3、将sql映射文件注册在全局配置文件中
     * 4、写代码:
     * 1)、根据全局配置文件得到SqlSessionFactory;
     * 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查
     * 一个sqlSession就是代表和数据库的一次会话,用完关闭
     * 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。
     *
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        // 2、获取sqlSession实例,能直接执行已经映射的sql语句
        // sql的唯一标识:statement Unique identifier matching the statement to use.
        // 执行sql要用的参数:parameter A parameter object to pass to the statement.
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            Employee employee = openSession.selectOne("com.leslie.dao.EmployeeMapper.getEmpById", 1);
            System.out.println(employee);
        } finally {
            openSession.close();
        }
    }


    @Test
    public void test01() throws IOException {
        // 1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        // 2、获取sqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            // 3、获取接口的实现类对象
            //会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            Employee employee = mapper.getEmpById(1);
            System.out.println(mapper.getClass());
            System.out.println(employee);
        } finally {
            openSession.close();
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读