温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis接口式编程

发布时间:2020-08-03 13:42:03 来源:网络 阅读:527 作者:Adam的blog 栏目:开发技术

代码清单及总结

接口

/**
 * 这是一个接口
 * @author guozhenZhao
 * @date 2018年7月16日
 */
public interface EmployeeMapper {

    public Employee getEmployeeById(Integer id);
}

全局配置文件

<?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?useSSL=false" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>

    <!-- 将我们写好的sql映射文件(testEmployeeMapper.xml)注册到全局配置文件(mybatis-config.xml)中 -->
    <mappers>
        <mapper resource="testEmployeeMapper.xml" />
    </mappers>
</configuration>

sql映射文件

<?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.zgz.MyBatis.dao.EmployeeMapper">
<!--
    namespace: 名称空间
    id: 唯一标识
    resultType: 返回值类型 
    #{id}: 从传递过来的参数中取出id值

    在MyBatis中,接口可以与配置文件实现动态绑定,绑定方式:
        1. 把namespace指定为接口的全类名
        2. 把id换成接口中的方法,可以把select标签和接口中的方法进行绑定
 -->
    <select id="getEmployeeById" resultType="com.zgz.MyBatis.bean.Employee">
        select id,last_name lastName,email,gender from tbl_employee where id = #{id}
    </select>
</mapper>

测试方法:

/**
 * 两种方式:
 *  1. 普通的HelloWorld
 *  2. 接口式编程(推荐)
 *      原生:         Dao     ===》      DaoImpl
 *      MyBatis:    Mapper  ===》      xxMapper.xml
 *  3. SqlSession代表和数据库的一次会话,使用完必须关闭
 *  4. SqlSession和connection一样都是非线程安全的,每次使用都应该去获取新的对象,不能使用成员变量的形式去创建他
 *      SqlSession sqlSession = null; 是错误的
 *  5. mapper接口没有实现类,将该接口和配置文件绑定后,MyBatis会生成该接口的一个代理对象
 *  6. 两个重要的配置文件:
 *      6.1 mybatis全局配置文件:包含数据库连接池信息,事务管理信息...系统运行环境信息等等
 *      6.2 sql映射文件,保存了每一个sql语句的映射信息
 * @author guozhenZhao
 * @date 2018年7月16日
 */
public class MyBatisTest {

    // 获取SqlSessionFactory对象
    private 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 {
        // 1. 根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 2.1 获取sqlSession实例,能直接执行已经映射的sql语句
        SqlSession openSession = sqlSessionFactory.openSession();

        /**
         * 2.2 第一个参数sql的唯一标识:名称空间加上id 第二个参数是执行sql要用的参数
         */
        try {
            Employee employee = openSession.selectOne("com.zgz.mybatis.EmployeeMapper.selectEmp", 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. 获取接口的实现类对象
            // 打印结果:$Proxy5,是一个代理对象,发现MaBatis会为接口自动创建一个代理对象,代理对象去执行CRUD
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            System.out.println(mapper.getClass());

            Employee employee = mapper.getEmployeeById(1);
            System.out.println(employee);
        } finally {
            openSession.close();
        }
    }

}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI