Mybatis环境配置

Mybatis环境搭建


1. 创建项目并引入相关jar包

首先创建项目的目录结构如下图所示:


项目目录结构

这里除了需要引入Mybatis的jar包以外还需要使用到Mysql的数据库驱动jar包。
Mybatis的jar包的下载地址:点击下载

2. 创建Mybatis环境配置文件

如上图文件目录结构所示,src目录中的Configuration.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>
    <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/webtest" />
                <property name="username" value="root"/>           
            </dataSource>
        </environment>
    </environments>
</configuration>

其中<environments>节点是用来配置数据库连接的,<typeAliases>是用来映射与数据库表对应的实体类的。
<mappers>节点主要是用于配置实体类的映射文件的。

3. 编写实体类

Student.java

package com.mybatis.bean;
public class Student {
    private int id;
    private String name;
    private String sex;
    private int classNum;
    private String imgUrl;
    public Student(int id, String name, String sex, int classNum, String imgUrl) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.classNum = classNum;
        this.imgUrl = imgUrl;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    //此处省略get、set方法 
}

4. 配置实体类对应的映射文件

Student.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.mybatis.inte.StudentOperation">
    <select id="selectStudentByID" parameterType="int" resultType="Student">
        select * from `student` where id = #{id}
    </select>
</mapper>

这里的namespace属性是要根据Student实体类的数据库操作接口来配置的,这里填写的是接口类的完整路径。
这里的<select>节点代表一个查询,这里的id必须唯一,#{id}代表方法传入的参数。

4. 编写与映射文件内方法一致的用于方法调用的接口类

根据映射文件编写一个用于实现具体查询操作的接口类,这里为StudentOperation,具体代码如下所示:这里的通过id搜索学生的方法名必须与映射文件中的<select>标签中的id一致。

package com.mybatis.inte;
import com.mybatis.bean.Student;
public interface StudentOperation {
    public Student selectStudentByID(int id);
}

5. 编写测试类

接下来就是编写测试类验证运行结果。

package com.mybatis.test;
import java.io.Reader;
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 com.mybatis.bean.Student;
import com.mybatis.inte.StudentOperation;
public class test {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader; 
     static{
            try{
                reader    = Resources.getResourceAsReader("Configuration.xml");
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            }catch(Exception e){
                e.printStackTrace();
            }
    }
   public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }
public static void main(String[] args) {
       SqlSession session = sqlSessionFactory.openSession();
       try {
               StudentOperation studentOperation=session.getMapper(StudentOperation.class);
           Student student =studentOperation.selectStudentByID(1);
           System.out.println(student.getId());
           System.out.println(student.getName());
       } 
       finally {
           session.close();
               }
            }
}

在上述例子中,实体类的属性与数据表字段的映射是Mybatis自动实现的,也可以通过在映射文件中添加<resultMap>节点实现映射配置,具体实现如下所示:

<?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.mybatis.inte.StudentOperation">
    <resultMap type="Student" id="studentResultMap">    
        <id property="id" column="id"/>
        <result property="name" column="name" />
        <result property="sex" column="sex"/>
        <result property="classNum" column="class_num"/>
        <result property="imgUrl" column="img_url"/>
    </resultMap>
    <select id="selectStudentByID" parameterType="int" resultType="Student" resultMap="studentResultMap">
        select * from `student` where id = #{id}
    </select>
    <select id="getAll" resultType="Student" resultMap="studentResultMap">
        select * from `student`
    </select>
</mapper>

在上述配置文件中,通过<result>节点中的property属性与column属性实现映射关系的配置。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值