3、创建user数据表
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(50) DEFAULT NULL,
password varchar(50) DEFAULT NULL,
birthday varchar(50) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
4、编写User实体类
package com.panshenlian.pojo;
public class User {
private int id;
private String username;
private String password;
private String birthday;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return “User{” +
“id=” + id +
“, username=’” + username + ‘’’ +
“, password=’” + password + ‘’’ +
“, birthday=’” + birthday + ‘’’ +
‘}’;
}
}
5、编写映射文件UserMapper.xml
select * from User6、编写核心文件SqlMapConfig.xml
7、编写测试类
package com.panshenlian.service;
import com.panshenlian.pojo.User;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
@Test
public void testQueryUser01() throws IOException {
//加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream(“SqlMapConfig.xml”);
// 获得sqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行sql语句
List userList = sqlSession.selectList(“userMapper.findAll”);
// 打印结果
for (User user : userList) {
System.out.println(user);
}
// 释放资源
on sqlSession = sqlSessionFactory.openSession();
// 执行sql语句
List userList = sqlSession.selectList(“userMapper.findAll”);
// 打印结果
for (User user : userList) {
System.out.println(user);
}
// 释放资源



