mybatis入门

列出项目的目录:

1.首先创建数据库和表:mybatis数据库 的 t_user 表

2.创建User.java 实体类:

package com.yunqing.mybatis.bean;

public class User {
    private Integer id;
    private String name;
    private String gender;
    private String email;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

3.创建 mybatis-config.xml 全局配置文件:(sqlSessionFactory是从此配置文件中获取)

<?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="5678"/>
            </dataSource>
        </environment>
    </environments>
    <!--将写好的sql映射文件注册到全局配置文件中,类路径下直接写 UserMapper.xml就行,要是在包中则写com/yunqing/.../UserMapper.xml-->
    <mappers>
        <mapper resource="conf/UserMapper.xml"/>
    </mappers>
</configuration>

4.创建最重要的  操作数据库的 针对User实体类  对应的 userMapper.xml (写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">
<!--
namespace:命名空间,指定为接口的全类名
selectUserById:唯一标识
resultType:返回值类型
-->
<mapper namespace="com.yunqing.mybatis.dao.UserMapper">
    <select id="getUserById" resultType="com.yunqing.mybatis.bean.User">
        select * from t_user where id = #{id}
    </select>
</mapper>

5.创建User实体对应的  接口,接口式编程

package com.yunqing.mybatis.dao;

import com.yunqing.mybatis.bean.User;

public interface UserMapper {

    public User getUserById(Integer id);//不需要写实现类,userMapper.xml会自动创建一个虚拟的实现类
}

6编写测试方法:

package com.yunqing.mybatis.test;

import com.yunqing.mybatis.bean.User;
import com.yunqing.mybatis.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class MybatisTest {

    @Test
    public void test() throws IOException {

        //从xml中获取sqlSessionFactory
        String resource = "conf/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //之前的mybatis的写法
        /*User user = sqlSession.selectOne("com.yunqing.mybatis.UserMapper.selectUserById",1);
        System.out.println(user);*/

        //现在的mybatis接口式编程写法
        //相当于接口的实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUserById(1);//查询第一条记录
        System.out.println(user);
        sqlSession.close();
    }
}

7.测试结果

小结:

posted @ 2017-12-13 17:12  云晴  阅读(172)  评论(0编辑  收藏  举报