mybatis学习一 入门实践

学习java的人对mybatis框架肯定不陌生了,今天就来看下mybatis入门使用。

为什么使用mybatis

在java中操作数据库的一般流程为:
编写javaBean==》编写sql语句==》sql预编译==》设置参数==》执行sql==》封装结果集。
在实际项目中,sql优化往往决定了项目访问数据库的性能,这个步骤往往是程序员无法避免的必要步骤。在myabtis中,编写的sql语句被写入xml配置文件中,其他步骤都由mybatis自动完成,程序员需要关注sql的编写与优化。这也是mybatis能被广泛使用的原因。

mybatis操作数据库基本流程

  1. 编写全局配置文件,这个配置文件中包含了数据源信息以及mapper位置信息
<?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://127.0.0.1:3306/testdemo1"/>
                <property name="username" value="root"/>
                <property name="password" value="steven"/>
            </dataSource>
        </environment>
    </environments>
    <!--注册mapper文件-->
    <mappers>
        <mapper resource="mapper/employee.xml"/>
    </mappers>
</configuration>
  1. 通过全局配置文件获取SqlSessionFactory对象:
 //读取配置文件
            reader = Resources.getResourceAsReader(resource);
            //创建sqlSessionFactory对象
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  1. 编写sql映射文件mapper,配置每个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.steven.entities.EmployeeMapper">

    <select id="selectOne" parameterType="int" resultType="com.steven.entities.Employee">
      select id,last_name lastName,gender,email from employee where id = #{id}
    </select>
</mapper>
  1. 使用SqlSessionFactory对象获取sqlSession,使用sqlSession对象执行增删改查:
 //获取sqlsession对象,能执行已经映射的sql语句
        SqlSession session = sqlSessionFactory.openSession();
       //参数:sql唯一标识, sql参数
  Employee employee = session.selectOne("com.steven.entities.EmployeeMapper.selectOne",1);
 System.out.println(employee);

以上过程没有使用到mapper接口,下面介紹使用使用mapper接口的使用方式:
编写mapper接口:

public interface EmployeeMapper {

     Employee getEmployeeById(int id);
}

改写mapper的xml映射文件与接口绑定:

<?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.steven.mapper.EmployeeMapper">

    <select id="getEmployeeById" parameterType="int" resultType="com.steven.entities.Employee">
      select id,last_name lastName,gender,email from employee where id = #{id}
    </select>
</mapper>

注意:mapper的namespace属性使用mapper接口的全路径名,查询语句的id与接口中的方法名需要对应同名。

获取mapper对象通过mapper对象来执行sql:

//获取sqlsession对象,能执行已经映射的sql语句
        SqlSession session = sqlSessionFactory.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        System.out.println(mapper);
        Employee employee = mapper.getEmployeeById(1);
        System.out.println(employee);

这里打印了mapper的信息,发现mapper实际上是代理对象:
org.apache.ibatis.binding.MapperProxy@4e928fbf
使用这种方式能实现与前面方式相同的结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值