MyBatis HelloWorld

一. 配置映射文件方式

1. 导入 jar 包

MyBatis 下载

  • 导入 mybatis-3.4.5.jar
  • 导入 mysql-connector-java-5.1.44-bin.jar

2. 创建实体类

public class Student {

    private String stu_name;
    private Integer stu_id;
    private Integer stu_age;

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public Integer getStu_id() {
        return stu_id;
    }

    public void setStu_id(Integer stu_id) {
        this.stu_id = stu_id;
    }

    public Integer getStu_age() {
        return stu_age;
    }

    public void setStu_age(Integer stu_age) {
        this.stu_age = stu_age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stu_name='" + stu_name + '\'' +
                ", stu_id=" + stu_id +
                ", stu_age=" + stu_age +
                '}';
    }
}

3.MyBatis 配置

  1. 编写全局配置文件
<?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:///test"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="studentMapper.xml"/>
    </mappers>
</configuration>
  1. 编写映射文件
<?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="test">
    <select id="testId" resultType="com.mybatis.test.Student">
        select * from student where stu_id = #{id}
    </select>

</mapper>

4. 单元测试


public class MyBatisHelloWorld {

    @Test
    public void test() throws IOException {

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Object o = sqlSession.selectOne("test.testId", 1);
        System.out.println(o);
    }
}
  • 注意数据库有对应的记录
  • 若实体类的字段和数据库的字段名不一致可采用别名方式对应

二. 面向接口方式

1. 创建一个接口

创建接口,定义抽象方法.

public interface IStudent {

    Student getStudent(Integer id);
}

2. 修改映射文件

<?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.test.IStudent">
    <select id="getStudent" resultType="com.mybatis.test.Student">
        select * from student where stu_id = #{id}
    </select>

</mapper>

修改以下信息:

  • namespace 改为接口的全类名
  • id 改为接口中定义的方法名

3. 编写测试方法

 @Test
    public void test2() throws IOException {

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IStudent mapper = sqlSession.getMapper(IStudent.class);
        System.out.println(mapper.getStudent(1));

    }
  • 使用 sqlSession.getMapper(IStudent.class);方法获取接口实现的代理对象
  • sqlSession 是非线程安全的,所以不能做成员变量.每次需要时重新获取.

MyBatis中文手册

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值