当有多个用户要对应多条数据时需要实现myBatista的数据表多对多
首先创建三张表:用户表(User),组表(Groups),用户表和组表关联的表(User_Groups)
User表:
Groups表:
组表和用户表的关联表:
1.创建User对应表的实体类,映射文件和接口
1.1实体类:
package com.zhiyuan.pojo;
import java.util.List;
public class User {
private int id;
private String username;
private int mobile;
private List<Groups> groups;
public List<Groups> getGroups() {
return groups;
}
public void setGroups(List<Groups> groups) {
this.groups = groups;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getMobile() {
return mobile;
}
public void setMobile(int mobile) {
this.mobile = mobile;
}
}
1.2映射文件:
当要使用自定义的类型则用map,当要使用基本数据类型就用type。根据Select中的查询语句返回的类型和需要的参数类型定义resultMap中type属性值和Select中的parameterType值。property中写实体类中的属性名,column中写表中对应的字段名。namespace写对应的接口路径,没有接口可以写实体类的路径。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.zhiyuan.maper.UserMaper">
<parameterMap type="User" id="parameterUserMap">
<parameter property="id"/>
<parameter property="username"/>
<parameter property="mobile"/>
</parameterMap>
<insert id="insertUser" parameterMap="parameterUserMap">
insert into user(username,mobile) value(#{username},#{mobile});
</insert>
<resultMap type="User" id="getUser">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="mobile" column="mobile"/>
<collection property="groups" column="id" select="com.zhiyuan.maper.UserGroupsMaper.getUserAll"/>
</resultMap>
<select id="getUserByID" resultMap="getUser" parameterType="int">
select*from user where id=#{id};
</select>
</mapper>
1.3对应的接口:
package com.zhiyuan.maper;
import com.zhiyuan.pojo.User;
public interface UserMaper {
public void insertUser(User user);
public User getUserByID(int id);
}
2.创建Groups对应表的实体类,映射文件和接口
2.1实体类
package com.zhiyuan.pojo;
import java.util.List;
public class Groups {
private int group_id;
private String group_name;
private List<User> user;
public int getGroup_id() {
return group_id;
}
public void setGroup_id(int group_id) {
this.group_id = group_id;
}
public String getGroup_name() {
return group_name;
}
public void setGroup_name(String group_name) {
this.group_name = group_name;
}
public List<User> getUser() {
return user;
}
public void setUser(List<User> user) {
this.user = user;
}
}
2.2映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.zhiyuan.maper.GroupsMaper">
<parameterMap type="Groups" id="parameterUserMap">
<parameter property="group_name"/>
</parameterMap>
<insert id="insertGroupUser" parameterMap="parameterUserMap">
insert into groups(group_names)value(#{group_name});
</insert>
</mapper>
2.3对应接口
package com.zhiyuan.maper;
import com.zhiyuan.pojo.Groups;
public interface GroupsMaper {
public void insertGroupUser(Groups groups);
}
3.创建User_Group对应表的实体类,映射文件和接口
3.1实体类package com.zhiyuan.pojo;
public class User_Group {
private User user_id;
private Groups group_id;
public User getUser_id() {
return user_id;
}
public void setUser_id(User user_id) {
this.user_id = user_id;
}
public Groups getGroup_id() {
return group_id;
}
public void setGroup_id(Groups group_id) {
this.group_id = group_id;
}
}
3.2映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.zhiyuan.maper.UserGroupsMaper">
<resultMap type="Groups" id="getResultMap">
<result property="group_id" column="group_id"/>
<result property="group_name" column="group_names"/>
</resultMap>
<select id="getUserAll" resultMap="getResultMap" parameterType="int">
select g.*, u.user_id from groups g, user_group u where g.group_id=u.group_id AND u.user_id=#{user_id};
</select>
</mapper>
3.3对应接口
package com.zhiyuan.maper;
import java.util.List;
import com.zhiyuan.pojo.User_Group;
public interface UserGroupsMaper {
public void insertUserGroups(User_Group ug);
public List getUserAll(int id);
}
4.MyBatist主配置文件
typeAliases中type属性写实体类的路径,alias为别名。environment中的default属性有两种值分别为development(开发)和work(工作)。mappers中写对应的映射文件路径
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<typeAlias type="com.zhiyuan.pojo.User" alias="User"/>
<typeAlias type="com.zhiyuan.pojo.Groups" alias="Groups"/>
<typeAlias type="com.zhiyuan.pojo.User_Group" alias="User_Group"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/work"/>
<property name="username" value="root"/>
<property name="password" value="1223"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zhiyuan/maper/UserMapper.xml"/>
<mapper resource="com/zhiyuan/maper/GroupsMapper.xml"/>
<mapper resource="com/zhiyuan/maper/UserGroupsMaper.xml"/>
</mappers>
</configuration>
package com.zhiyuan.text;
import java.io.IOException;
import java.io.Reader;
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.zhiyuan.maper.UserMaper;
import com.zhiyuan.pojo.Groups;
import com.zhiyuan.pojo.User;
public class MainApp {
public static Reader reader;
public static SqlSessionFactory sessionFactory;
static{
try {
reader=Resources.getResourceAsReader("config.xml");
sessionFactory=new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory sessions(){
return sessionFactory;
}
public static void main(String[] args) {
SqlSession session=sessionFactory.openSession();
// insertUser();
// insertGroupUser();
// insertUserGroups();
selectAllUser();
session.close();
}
//User表插入数据操作
// public static void insertUser(){
// SqlSession session=sessionFactory.openSession();
// UserMaper user=session.getMapper(UserMaper.class);
// User users=new User();
// users.setUsername("路人乙");
// users.setMobile(1658);
// user.insertUser(users);
// session.commit();
// System.out.println("OK");
// session.close();
// }
// Groups表插入数据操作
// public static void insertGroupUser(){
// SqlSession session=sessionFactory.openSession();
// GroupsMaper groups=session.getMapper(GroupsMaper.class);
// Groups rou=new Groups();
// rou.setGroup_name("Group_02");
// groups.insertGroupUser(rou);
// System.out.println("OK");
// session.commit();
// session.close();
// }
//查询所有
public static void selectAllUser(){
SqlSession session=sessionFactory.openSession();
UserMaper iuser=session.getMapper(UserMaper.class);
User user=iuser.getUserByID(2);
System.out.println("用户名:"+user.getUsername()+",");
List<Groups> groups=user.getGroups();
for(Groups group:groups){
System.out.print("组ID:"+group.getGroup_id()+",");
System.out.println("组名:"+group.getGroup_name()+",");
}
}
}
初入江湖还什么都不懂,各路大神多提点意见。蟹蟹