Mybatis多对多

Mybatis多对多

首先创建三个表,user,group,user_group.
然后创建对应的类在pojo下。
配置文件configure.xml和一对多没区别。
代码如下:

例group.xml代码:

测试类测试

import java.io.Reader;
import java.text.MessageFormat;
import java.util.List;

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 com.yiibai.maper.GroupMaper;
import com.yiibai.maper.UserGroupMaper;
import com.yiibai.maper.UserMaper;
import com.yiibai.pojo.Group;
import com.yiibai.pojo.User;
import com.yiibai.pojo.UserGroup;

public class Main {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;

static {
    try {
        reader = Resources.getResourceAsReader("config/Configure.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static SqlSessionFactory getSession() {
    return sqlSessionFactory;
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    // testAddGroup();
    // testAddUser();
    // testAddUserGroup();
    testGetGroupAndUsers();

}

public static void testGetGroupAndUsers() {
    UserGroup userGroup = new UserGroup();
    SqlSession session = sqlSessionFactory.openSession();
    try {
        GroupMaper groupMaper = session.getMapper(GroupMaper.class);
        Group group = groupMaper.getGroup(1);
        System.out.println("Group => " + group.getGroupName());
        List<User> users = group.getUsers();
        for (User user : users) {
            System.out.println("\t:" + user.getId() + "\t"
                    + user.getUsername());
        }
    } finally {
        session.close();
    }
}

public static void testAddUserGroup() {
    UserGroup userGroup = new UserGroup();
    userGroup.setGroupId(1);
    userGroup.setUserId(2);
    SqlSession session = sqlSessionFactory.openSession();
    try {
        UserGroupMaper userGroupMaper = session
                .getMapper(UserGroupMaper.class);
        userGroupMaper.insertUserGroup(userGroup);

        session.commit();
    } finally {
        session.close();
    }

}

public static void testAddUser() {
    // TODO Auto-generated method stub
    SqlSession session = sqlSessionFactory.openSession();
    try {
        User user = new User();
        user.setUsername("User-name-1");
        user.setMobile("13838009988");
        UserMaper userMaper = session.getMapper(UserMaper.class);
        userMaper.insertUser(user);
        session.commit();
        // System.out.println(user.getGroupId());
    } finally {
        session.close();
    }
}

public static void testAddGroup() {
    // TODO Auto-generated method stub
    SqlSession session = sqlSessionFactory.openSession();
    try {
        Group group = new Group();
        group.setGroupName("用户组-1");
        GroupMaper groupMapper = session.getMapper(GroupMaper.class);
        groupMapper.insertGroup(group);
        session.commit();
        System.out.println(group.getGroupId());
    } finally {
        session.close();
    }
}

}

运行结果如下

Group => Group-1
:1 yiibai
:2 User-name-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值