MyBatis学习笔记

MyBatis配置文件

mybatis-config.xml 元素可以指定properties文件位置,导入里面配置的值 定义了一些别名,如student,用来代替全名com..Student 元素配置mapper.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 resource="com/endless/mybatis/helloworld/config/jdbc.properties" />
    <typeAliases>
        <typeAlias alias="student" type="com.endless.mybatis.helloworld.po.Student"></typeAlias>
    </typeAliases> 
    <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="com/endless/mybatis/helloworld/config/student-mapper.xml" />
    </mappers>
</configuration>

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/school
username=root
password=1234

mapper配置文件

student-mapper.xml 这个文件里面配置sql,namespace为对于DAO接口 定义了一条select语句,id对应上面namespace定义接口里的方法,parameterType和resultType分别对应该方法的参数和返回类型

<?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.endless.mybatis.helloworld.mapper.StudentMapper">
    <!-- student是在mybatis-config.xml中配置的Alias -->
    <select id="getStudent" parameterType="String" resultType="student">
        select * from student where id=#{studentId}
    </select>
</mapper>

Mapper接口

public interface StudentMapper {
    public Student getStudent(String studentId);
}
//这里Student的属性名称对和数据库的字段一致,会自动填充到Student对象中返回
public class Student {
    private int id;
    private String name;
    private int age;
    private String gender;
     //省略get,set
}

测试程序

public class MyBatisTest {
    public static void main(String[] args){
        String resource="com/endless/mybatis/helloworld/config/mybatis-config.xml";
        SqlSession sqlSession=null;        
        try{
            //SqlSessionFactoryBuilder读取配置文件创建SqlSessionFactory对象
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
            //sqlSessionFactory对象用来创建session,相当于JDBC的Connection对象
            sqlSession=sessionFactory.openSession();
            StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
            Student student=studentMapper.getStudent("10001");
            System.out.println(student);
        }catch(Exception e){
            e.printStackTrace();            
        }finally{        
            if(sqlSession!=null)
                sqlSession.close();
        }
    }
}

总结

输入图片说明

评论

暂无评论