目录
1、导入坐标pom文件
2、编写Mybatis核心配置文件
3、SQL映射文件(写sql语句的地方)
4、创建用户类
5、创建使用类
1、导入坐标pom文件
org.mybatis
mybatis
3.5.5
mysql
mysql-connector-java
5.1.46
junit
junit
4.13
test
org.slf4j
slf4j-api
1.7.20
ch.qos.logback
logback-classic
1.2.3
ch.qos.logback
logback-core
1.2.3
2、编写Mybatis核心配置文件
3、SQL映射文件(写sql语句的地方)
select * from tb_user;
4、创建用户类
package com.itheima.pojo;
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer 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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
", gender='" + gender + ''' +
", address='" + address + ''' +
'}';
}
}
5、创建使用类
public class MybatisDemo {
public static void main(String[] args) throws IOException {
//1、加载Mybatis核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2、获取sqlSession对象,用来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3、利用sqsession执行sql语句
List users = sqlSession.selectList("test.selectAll");
System.out.println(users);
//4、释放资源
sqlSession.close();
}
}
暑期编程PK赛
得CSDN机械键盘等精美礼品! 3、SQL映射文件(写sql语句的地方)
select * from tb_user;
4、创建用户类
package com.itheima.pojo;
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer 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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
", gender='" + gender + ''' +
", address='" + address + ''' +
'}';
}
}
5、创建使用类
public class MybatisDemo {
public static void main(String[] args) throws IOException {
//1、加载Mybatis核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2、获取sqlSession对象,用来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3、利用sqsession执行sql语句
List users = sqlSession.selectList("test.selectAll");
System.out.println(users);
//4、释放资源
sqlSession.close();
}
}
package com.itheima.pojo;
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer 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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
", gender='" + gender + ''' +
", address='" + address + ''' +
'}';
}
}



