一对多的模型:
查询用户的同时查询账号的信息对应的 SQL :
SELECT * FROM `user`SELECT * FROM `account` where user_id = #{user_id}
示例:
Account.java
package com.github.entity; import java.io.Serializable; /** * @author maxiaoke.com * @version 1.0 * */ public class Account implements Serializable { private Integer id; private Double money; public Account(Integer id, Double money) { this.id = id; this.money = money; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; }@Override public String toString() { return "Account{" + "id=" + id + ", money=" + money + '}'; } }
User.java
package com.github.entity; import java.io.Serializable; import java.util.Date; import java.util.List; /** * @author maxiaoke.com * @version 1.0 * */ public class User implements Serializable { private Integer id; private String username; private Date birthday; private String gender; private String address; // 一个用户有多个账号 private List<Account> accountList; 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } 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; } public List<Account> getAccountList() { return accountList; } public void setAccountList(List<Account> accountList) { this.accountList = accountList; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", gender='" + gender + '\'' + ", address='" + address + '\'' + ", accountList=" + accountList + '}'; }}
AccountMapper.java
package com.github.mapper; import com.github.entity.Account; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; /** * @author maxiaoke.com * @version 1.0 * */ @Mapperpublic interface AccountMapper {@Select("SELECT * FROM `account` where user_id = #{userId}")@Results({@Result(id = true, property = "id", column = "id"), @Result(property = "money", column = "money") }) List < Account > findByUserId(Integer userId); }
UserMapper.java
package com.github.mapper; import com.github.entity.User; import org.apache.ibatis.annotations. * ; import java.util.List; /** * @author maxiaoke.com * @version 1.0 * */ @Mapperpublic interface UserMapper {@Select(" SELECT * FROM `user`")@Results({@Result(id = true, property = "id", column = "id"), @Result(property = "username", column = "username"), @Result(property = "birthday", column = "birthday"), @Result(property = "gender", column = "gender"), @Result(property = "address", column = "address"), @Result(property = "accountList", // 被包含对象的变量名 column = "id", // 根据查询出来的user表中的id去根据user_id查询 many = @Many(select = "com.github.mapper.AccountMapper.findByUserId")), }) List<User> findAll();}
测试:
package com.github.mybatis; import com.github.entity.User; import com.github.mapper.UserMapper; 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.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * @author maxiaoke.com * @version 1.0 * */ public class MybatisTest { SqlSession sqlSession;@Before public void before() throws IOException { // 使用Mybatis的Resources读取mybatis-config.xml配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); sqlSession = sqlSessionFactory.openSession(); } @Test public void test() throws IOException { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.findAll(); userList.forEach(System.out::println); } @After public void after() { if (null != sqlSession) { sqlSession.close(); } }}