Mybatis的一个helloworld例子(接口方式)

这篇文章讲述的Mybatis的一个helloworld例子,如有错误或者不当之处,还望各位大神批评指正。

配置

Mybatis支持SQL集的mapper对应相关操作的接口,比较方便。

数据库中的表

create table student(
    id number(6) primary key not null ,
    name varchar(20) not null ,
    sex char(2) ,
    age number(3)
)

对应的类

public class Student {
    private Integer id;
    private String name;
    private char sex;
    private int age;

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }

    //省略get和set方法
}

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"> 
<!-- mybatis环境配置 -->
<configuration>  
     <!-- 环境:配置mybatis的环境 -->  
    <environments default="development">  
     <!-- 环境变量:可以配置多个环境变量,比如使用多数据源时,就需要配置多个环境变量 -->    
        <environment id="development">
            <!-- 事务管理器 -->        
            <transactionManager type="JDBC"/>  
            <!-- 数据源 -->    
            <dataSource type="POOLED"> 
                <!-- 驱动器 -->      
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>        
                <!-- 数据库实例地址 -->
                <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>        
                <!-- 用户名 -->
                <property name="username" value="scott"/>        
                <!-- 密码 -->
                <property name="password" value="tiger"/>      
            </dataSource>    
        </environment>  
    </environments> 

    <!-- 下边是SQL映射文件 -->

    <mappers>    
        <mapper resource="com/cn/cmc/bean/StudentMapper.xml"/>  
    </mappers> 

</configuration>

StudentMapper.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">
<!-- 
    namespace:对应类的位置
 -->
<mapper namespace="com.cn.cmc.dao.StudentMapper">
    <!-- 
        namespace:名空间
        id:对应接口中的方法名
        resultType:返回对象的类型
     -->
    <select id="getStudentById" resultType="com.cn.cmc.bean.Student"> 
        select * from Student where id= #{id}  
    </select>
</mapper>

接口的配置

import com.cn.cmc.bean.Student;

public interface StudentMapper {

    public  Student getStudentById(Integer id) ;

}

工具类MybatisUtil


public class MybatisUtil {

    static String resource = "mybatis-config.xml"; 
    static InputStream inputStream;
    static SqlSessionFactory sqlSessionFactory ;

    static {
        //初始化SqlSessionFactory
        try {

            inputStream = Resources.getResourceAsStream(resource);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sqlSessionFactory =  new SqlSessionFactoryBuilder().build(inputStream);

    }
    public static SqlSession openSession(){

        return sqlSessionFactory.openSession() ;
    }

    public static void closeSession(SqlSession session){
        session.close();
    }

}

操作

查询学生

SqlSession session = MybatisUtil.openSession() ;

        try{

            StudentMapper mapper = session.getMapper(StudentMapper.class) ;
            Student student = mapper.getStudentById(100001) ;

            System.out.println(student.toString());

        }finally{
            session.close();
        }

猜你喜欢

转载自blog.csdn.net/u013634252/article/details/80761642