小例子(一)

最近有点懒散,决定看看mybatis的源码,给自己找点学习的节奏,源码这种东西,需要找一个切入点去看,不然真的是无从下手,所以,我们先从一个小例子说起.

首先我们需要一个mybatis非常重要的配置文件mybatis-config.xml(名字可以随便取):

<?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>

    <!-- 指定properties配置文件 -->
    <properties resource="dbConfig.properties"></properties>

    <!-- 指定Mybatis使用log4j -->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!-- 上面指定了数据库配置文件, 配置文件里面也是对应的这四个属性 -->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>

            </dataSource>
        </environment>
    </environments>

    <!-- 映射文件 -->
    <mappers>
        <mapper resource="mapper/UserDao.xml"/>
    </mappers>

</configuration>

然后一个mapper映射文件UserDao.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.tree.study.mybatis.dao.UserDao">

    <select id="findUserById" resultType="org.tree.study.mybatis.entity.UserEntity">
          select * from user where id = #{id}
    </select>

    <insert id="insertUser" parameterType="org.tree.study.mybatis.entity.UserEntity">
        insert into user(name, address) values(#{name}, #{address})
    </insert>

</mapper>

之后创建一个UserEntity类:

package org.tree.study.mybatis.entity;

public class UserEntity {
    private Integer id;
    private String name;
    private String address;

    // 省略get.set及构造方法...
}

还有和映射文件相对应的Dao类:

package org.tree.study.mybatis.dao;
import org.tree.study.mybatis.entity.UserEntity;

public interface UserDao {

    UserEntity findUserById(Integer id);

}

这样就差不多了,现在我们就可以使用啦(当然你需要自己在dbConfig.properties配置文件中配置好数据库的地址和账号密码,并创建好表):

public class UserTest {

    @Test
    public void testFindUserUseMapper() {
        // 通过mapper代理去访问数据库
        SqlSession sqlSession = getSqlSessionFactory().openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        UserEntity userById = mapper.findUserById(1);

        System.out.println(userById.getName());
        System.out.println(userById.getAddress());

    }

    @Test
    public void testFindUserUseStatement() {
        // 直接指定语句去访问数据库,和上面通过mapper代理的方式本质是一样的
        SqlSession sqlSession = getSqlSessionFactory().openSession();
        UserEntity userById = sqlSession.selectOne("org.tree.study.mybatis.dao.UserDao.findUserById", 1);

        System.out.println(userById.getName());
        System.out.println(userById.getAddress());

    }

    @Test
    public void insertUser() {
        // 插入一条数据,并commit
        SqlSession sqlSession = getSqlSessionFactory().openSession();
        sqlSession.insert("org.tree.study.mybatis.dao.UserDao.insertUser", new UserEntity("小陈", "地址"));
        sqlSession.commit();
    }

    public SqlSessionFactory getSqlSessionFactory() {
        SqlSessionFactory sqlSessionFactory = null;
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder()
                    .build(Resources.getResourceAsReader("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSessionFactory;
    }
}

好了,到这里,这个例子就结束了.我们可以从UserTest类中发现,我们通过mybatis去访问数据库都要通过配置文件来生成SqlSessionFactory,然后通过SqlSessionFactory生成SqlSession,然后通过SqlSession去访问数据库.那么,我们就找到了切入点—SqlSessionFactory.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值