zoukankan      html  css  js  c++  java
  • mybatis(入门案例)

    mybatis是支持定制化sql、存储过程以及高级映射的优秀持久层框架

     

    mybatis避免了几乎所有的jdbc代码和手动设置参数以及获取结果集

     

    mybatis可以使用 简单的xml或者注解方式用于配置和原始映射,将接口i和java的pojo(普通的java对象)映射成数据库中的记录

     

    mybatis入门:

     

    mapper映射文件

    <?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="com.atguigu.mybatis.dao.EmployeeMapper">
    <!-- 
    namespace:名称空间;指定为接口的全类名
    id:唯一标识
    resultType:返回值类型
    #{id}:从传递过来的参数中取出id值
    
    public Employee getEmpById(Integer id);
     -->
        <select id="getEmpById" resultType="com.bean.Employee">
            select id,last_name lastName,email,gender from tbl_employee where id = #{id}
        </select>
    </mapper>

    mybatis核心配置文件

    <?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="123456" />
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="EmployeeMapper.xml" />
        </mappers>
    </configuration>

    测试用例:

    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 {
    
            // 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.mybatis.EmployeeMapper.selectEmp", 1);
                System.out.println(employee);
            } finally {
                openSession.close();
            }
    
        }
    
    }

     

    接口式编程:

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

    测试:

    @Test
        public void test() 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();
            }
    
        }

              

  • 相关阅读:
    小菜编程成长记(十四 设计模式不能戏说!设计模式怎就不能戏说?)
    小菜编程成长记(十三 有了门面,程序员的程序会更加体面!)
    伍迷随想冷饭集 之 手术前后之随想
    伍迷随想冷饭集 之 自动扶梯之随想
    小菜编程成长记(十一 三层架构,分层开发)
    伍迷随想冷饭集 之 固定晚餐之随想
    伍迷随想冷饭集 之 瞻前顾后之随想
    伍迷随想冷饭集 之 北国冬天之随想
    伍迷随想冷饭集 之 漫游大理国之随想
    伍迷随想冷饭集 之 伍迷运动之随想
  • 原文地址:https://www.cnblogs.com/lzb0803/p/9004526.html
Copyright © 2011-2022 走看看