美文网首页
01—mybatis之Helloworld

01—mybatis之Helloworld

作者: Lesie_zwc | 来源:发表于2018-06-27 14:10 被阅读0次

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();
        }
    }
}

相关文章

  • 01—mybatis之Helloworld

    1、ORM框架发张历史 2、Helloword编写 2-1、建立数据表和对应的javabean 2-2、修改全部配...

  • 【十一】Mybatis学习笔记

    《小吴同学的Mybatis学习之路》点击此处查看完整项目 1.Mybatis_01_HelloWorld接口式编程...

  • MyBatis入门之HelloWorld

    第一节 前期准备 1.MyBatis版本3.4.5 2.创建Java项目 3.添加依赖 4.数据库sql 上面算是...

  • MyBatis入门

    MyBatis HelloWorld 目录结构 准备数据库 jar包 使用Maven添加依赖: MyBatis配置...

  • MyBatis HelloWorld

    ☃ 数据库准备 JDBC方式 1.创建maven工程 2.引入mysql驱动依赖 3.准备数据库数据 4.Jdbc...

  • Mybatis(二)HelloWorld

    一、创建一张测试表 二、创建对应的javaBean 三、导入包 mybatis-3.4.1.jar(mybatis...

  • Mybatis-HelloWorld

    我们通过一个简单的 HelloWorld 先来看下 MyBatis 的基本用法。 首先来准备一个数据库: 接下来创...

  • MyBatis 之Helloworld注意事项

    添加两个依赖包 mybatis-3.4.1 ——链接(翻墙) mysql-connector-java-5.1.4...

  • 4-MyBatis 架构介绍

    4. MyBatis 架构介绍 看完前面的 HelloWorld,接下来我们通过一张网络图片来看下 MyBatis...

  • 01、第一个Python程序

    第一个Python程序 01-helloworld.py 在命令行输入 python 01-helloworld....

网友评论

      本文标题:01—mybatis之Helloworld

      本文链接:https://www.haomeiwen.com/subject/xrvbyftx.html