一、项目的目录结构:
二、回顾Mybatis的项目搭建步骤
1、导入相关的包(11个jar包) 1
2、在src目录下编写编写配置文件(mybatis-cfg.xml)
3、编写实体类(User.java)
4、编写映射文件(UserMapper.xml)
5、编写测试文件
6、调用JUnit 5进行调试
相关jar包截图
mybatis-cfg.xml
<?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="dev">
<environment id="dev">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/java505"/>
<property name="username" value="root"/>
<property name="password" value="szxy"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/bjsxt/mapper/usermapper.xml"/>
</mappers>
</configuration>
注:java505为mysql下的数据库名。驱动器driver的值不能更改。
实体类:
设置私有变量,
实现序列化,
带参无参构造函数,
取值赋值方法,
改写toString方法,
hashCode()和equals()方法。
import java.io.Serializable;
public class User implements Serializable{
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public User() {
super();
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
}
UserMapper.xml
<?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="cn.bjsxt.mapper.UserMapper">
<select id="selAll" resultType="cn.bjsxt.pojo.User">
select * from t_user
</select>
<select id="selOne" resultType="cn.bjsxt.pojo.User">
select * from t_user where id=1
</select>
</mapper>
*select id="selAll": 定义查询方法的名称
select from t_user:SQL语句为具体操作的内容
测试类testSelect.java
package cn.bjsxt.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 cn.bjsxt.pojo.User;
public class TestSelect {
@Test
public void testSelectList() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
//1、selectList
/**List<User> list = session.selectList("cn.bjsxt.mapper.UserMapper.selAll");
for (User user : list) {
System.out.println(user);
}*/
//2、selectOne
/**User user = session.selectOne("cn.bjsxt.mapper.UserMapper.selOne");
System.out.println(user);*/
//3、selectMap
Map<Integer, User> map = session.selectMap("cn.bjsxt.mapper.UserMapper.selAll", "id");
System.out.println(map);
Set<Integer> set = map.keySet();
for (Integer k : set) {
System.out.println(map.get(k));
}
session.close();
}
}
注:1、使用Junit 5需要在类里面添加一段代码@Test
,并导入import org.junit.Test;
2、selectAll查询的是所有对象,selectOne查询的是一个对象并且需要设置条件,selectMap查询的是具有键值关系的对象
提取码:gl59
复制这段内容后打开百度网盘手机App,操作更方便哦
- 相关包本来本来准备上传到博客的资源上,结果多次上传失败。放到百度云分享给大家吧!链接:https://pan.baidu.com/s/1rW6M5Xo9q8pIqTc5DnK9sA ↩