MyBatis之HelloWorld

1.新建一个java工程导入下面的jar包:

    mybatis-* .* .*. jar
    mysql-connector-java-*.* .* - bin .jar
    //打印sql语句需要的jar包和配置文件
     log4j -* .* .*. jar
    log4j.xml

2.创建数据库表及对应的POJO类及Mapper
接口,并在接口中添加一个查询的方法 
public interface EmployeeMapper {
     public Employee getEmployeeById(Integer id );
}
3.创建Mapper接口的映射文件EmployeeMapper.xml(从官方文档中复制例子)
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE mapper
 PUBLIC "-// mybatis.org//DTD Mapper 3.0//EN"
<!--
     namespace :要实现的接口的全类名
 -->  
< mapper namespace = "it.test.mybatis.mapper.EmployeeMapper" >
     <!--
         id:必须是接口中方法的方法名,不能随便指定
         resultType:接口中方法的返回值类型的全类名
     -->
     < select id = "getEmployeeById" resultType = "employee" >
         select id,last_name lastName,email,salary,dept_id deptId
         from employees where id = #{id}
     </ select >
</ mapper >
4.创建全局配置文件mybatis-config.xml(从官方文档中复制例子)
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
< 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 >
     <!-- 注册 Mapper 接口的映射文件 -->
     < mappers >
          < mapper resource = "EmployeeMapper.xml" />
     </ mappers >
</ configuration >
5.测试(仍然参考官方文档)
//创建SqlSessionFactory的方法
     public SqlSessionFactory getSqlSessionFactory() throws IOException {
         String resource = "mybatis-config.xml" ;
         InputStream inputStream = Resources. getResourceAsStream ( resource );
         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build( inputStream );
          return sqlSessionFactory ;
    }
     //面向 Mapper 接口开发:通过 Mapper 接口的方式获取Employee
     @Test
     void testMapper() throws IOException {
          // 1.获取SqlSessionFactory对象
         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
          // 2.获取SqlSession,相当于JDBC中的Connection
         SqlSession session = sqlSessionFactory .openSession();
          try {
              // 3.获取 Mapper 对象
             EmployeeMapper employeeMapper = session .getMapper(EmployeeMapper. class );
              //4.调用employeeMapper中获取一个对象的方法
             Employee employeeById = employeeMapper .getEmployeeById(1);
             System. out .println( employeeById );
         } finally {
              //5.关闭sqlSession
              session .close();
         }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Diligently_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值