Eclipse上创建第一mybatis工程实现数据库查询
步骤:
1.创建一个java工程
2.创建lib文件夹,加入mybatis核心包、依赖包、数据驱动包。并为jar包添加路径
3.创建resources资源文件夹,加入log4j.properties和SqlMapConfig.xml配置文件
4.创建POJO类
5.在resources下的sqlmap目录下创建sql映射文件User.xml
6.在SqlMapConfig.xml中加载映射文件
7.在resources下的sqlmap目录下创建sql映射文件User.xml实现根据id查询用户
8.编写测试程序
9.测试成功
1.创建一个java工程
2.创建lib文件夹,加入mybatis核心包、依赖包、数据驱动包。并为jar包添加路径
mybaits的代码由github.com管理
下载地址:https://github.com/mybatis/mybatis-3/releases
mybatis-3.4.6.jar mybatis的核心包
lib文件夹 mybatis的依赖包所在
mybatis-3.4.6.pdf mybatis使用手册
3.创建resources资源文件夹,加入log4j.properties和SqlMapConfig.xml配置文件
log4j.properties文件下加入如下头文件
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SqlMapConfig.xml加入如下信息,连接数据库
4.创建POJO类 //此处pojo类的属性和类型与数据库User表中的属性和类型相同
package com.mybatis.pojo;
import java.sql.Date;
public class User {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
+ address + "]";
}
}
5.在resources下的sqlmap目录下创建sql映射文件User.xml
6.在SqlMapConfig.xml中加载映射文件
7.在resources下的sqlmap目录下创建sql映射文件User.xml实现根据id查询用户
8.编写测试程序
public class MybatisTest {
private SqlSessionFactory sqlSessionFactory = null;
@Before
public void init() throws Exception {
// 1. 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2. 加载SqlMapConfig.xml配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 3. 创建SqlSessionFactory对象
this.sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
}
@Test
public void testQueryUserById() throws Exception {
// 4. 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5. 执行SqlSession对象执行查询,获取结果User
// 第一个参数是User.xml的statement的id,第二个参数是执行sql需要的参数;
Object user = sqlSession.selectOne("queryUserById", 1);
// 6. 打印结果
System.out.println(user);
// 7. 释放资源
sqlSession.close();
}
}
9.测试成功
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 107915280.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@66ea810]
DEBUG [main] - ==> Preparing: SELECT * FROM user WHERE id = ?
DEBUG [main] - ==> Parameters: 10(Integer)
DEBUG [main] - <== Total: 1
User [id=10, username=张三, sex=1, birthday=2014-07-10, address=北京市]