Mybatis编程 编写第一个基于mybatis的测试例子

mybatis测试步骤

1. 添加jar包(两个jar包)

添加在WebContent—WEB-INF—lib下,然后选中libr目录下的文件,右键点击buildPath->add Path”

【mybatis】
mybatis-3.1.1.jar
【MYSQL驱动包】

mysql-connector-java-5.1.7-bin.jar

2.建表加库

3.添加Mybatis的配置文件conf.xml

在这里插入图片描述
//localhost:3306是本地路径 xskc是数据库名字

4.定义表所对应的实体类

public class User{
private int id;
private String name;
private int age;
//get.set 方法
在这里插入图片描述
}
要有无参构造方法
toString是取相应的值

5.定义操作users表(数据库中表的名字)的sql映射文件userMapper.xml

在这里插入图片描述

6.在conf.xml文件中注册userMapper.xml文件

mappers>
mapper resource=“com/mybatis_test/userMapper.xml”/>
/mappers>

7.编写测试代码

public class Test {
public static void main(String[] args) throws IOException {
String resource = “conf.xml”;
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new
SqlSessionFac toryBuilder().build(reader);
SqlSession session = sessionFactory.openSession(true);
String statement = “com.mybatis_test.userMapper”+".selectUser";
User user = session.selectOne(statement, 1);
System.out.println(user);
System.out.println(user.getAge());
System.out.println(user.getId());
System.out.println(user.getName());

}

}

String statement = “com.mybatis_test.userMapper”+".selectUser";
//"selectUser"为userMapper.xml中的id值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值