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 + ‘’’ +
‘}’;
}
}
账户实体类 Account.java
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
// 从表实体应该包含一个主表实体的对象应用,和用户表构成一对一的关系,即一个账号只能由一个用户创建
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return “Account{” +
“id=” + id +
“, uid=” + uid +
“, money=” + money +
‘}’;
}
}
2. 创建 IAccountDao 接口和 IAccountDao.xml 文件
IAccountDao
public interface IAccountDao {
/**
-
查询所有账户信息
-
@return
*/
List findAll();
}
IAccountDao.xml
SELECT
FROM
account
3 .在 Mybatis-config.xml 配置文件中注册映射文件
4. 在 Mybatis-config.xml 配置文件中开启懒加载配置
5. 测试类(只查询账户信息)
AccountTest
public class AccountTest {
private InputStream input;
private SqlSession session;
private IAccountDao accountDao;
/**
-
初始化操作
-
@throws Exception
*/
@Before // 用于在测试方法执行之前执行
public void init() throws Exception {
// 1.读取配置文件
input = Resources.getResourceAsStream(“Mybatis-config.xml”);
// 2.获取SqlSessionFactory工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(input);
// 3.获取SqlSession 对象
session = factory.openSession();
// 4.获取dao接口的代理对象
accountDao = session.getMapper(IAccountDao.class);
}
@After // 用于在测试方法执行之后执行
public void destroy() throws Exception {
// 提交事务
session.commit();
// 释放资源
session.close();
input.close();
}
/**
- 查询所有账户信息
*/
@Test
public void testFindAll() {
List accounts = accountDao.findAll();
}
}
可以看出它只执行了一条语句,只查询了账户的信息,没有查询用户信息。因为本次只是将Account对象查询出来放入List集合中,并没有涉及到User对象,所以就没有发出SQL语句查询账户所关联的User对象的查询。
1.在User实体类中加入List属性
// 一对多关系映射,一个用户能创建多个账号,用户和账号构成一对多的关系
List accounts;
public List getAccounts() {
return accounts;
}
public void setAccounts(List accounts) {
this.accounts = accounts;
}
2. 创建用户和账户持久层接口的方法
public interface IAccountDao {
/**
-
查询所有账户信息
-
@return
*/
List findAll();
/**
-
根据用户id查询账户信息
-
@param uid
-
@return
*/
List findAccountByUid(Integer uid);
}
3. 编写用户持久层映射配置
SELECT
FROM
user
select
id as userId,
username as userName,
address as userAddress,
sex as userSex,
birthday as userBirthday
from
user
where
id=#{uid}
4. 编写账户持久层映射配置
SELECT
FROM
account
WHERE
uid = #{uid}
5. 测试类(只查询用户信息)
public class UserTest {
private InputStream input;
private SqlSession session;
private IUserDao userDao;
写在最后
还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…
4. 编写账户持久层映射配置
SELECT
FROM
account
WHERE
uid = #{uid}
5. 测试类(只查询用户信息)
public class UserTest {
private InputStream input;
private SqlSession session;
private IUserDao userDao;
写在最后
还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…
[外链图片转存中…(img-YszzXBSq-1714420296529)]