MyBatis对多关系:显示该用户的所有角色
只要在一边的UserMapper.xml 配置好就可以了
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <!--需求:搜索该用户的所有角色和该用户的信息--> 7 <mapper namespace="mybatis.UserMapper"> 8 <!--为返回的结果集和pojo属性,做对应关系 --> 9 <!--MyBatis中与hibernate不同的orm的地方,mybatis是返回的结果集映射到pojo中的属性,而不是在建立表的时候映射--> 10 <resultMap type="com.free.mybatis.domain.User" id="qwe"> 11 <!--所以他需要column来定位具体的属性 --> 12 <id property="uid" column="UID"/> 13 <result property="user_name" column="USER_NAME"/>
<!--下面是根据sql返回的结果集依次设置到pojo,不写则无值-->
14 <!-- <result property="user_password" column="USER_PASSWORD"/>--> 15 <!-- <result property="user_desc" column="USER_DESC"/>--> 16 <!-- <result property="e_mail" column="E_MAIL"/>--> 17 <!-- <result property="phone" column="PHONE"/>--> 18 <!-- 配置关联(应该采用extends的方式) --> 19 20 </resultMap> 21 22 <resultMap type="com.free.mybatis.domain.User" id="rolemapper" extends="qwe"> 23 <!--对多关系 collection --> 24 <!--对一关系 association--> 25 <collection property="roles" ofType="com.free.mybatis.domain.Role"> 26 <id property="rid" column="RID"/> 27 <result property="name" column="NAME"/> 28 <result property="pid" column="PID"/> 29 <result property="isParent" column="ISPARENT"/> 30 <result property="checked" column="CHECKED"/> 31 </collection> 32 </resultMap> 33 34 <select id="findUserRole" parameterType="map" resultMap="rolemapper"> 35 SELECT u.*,r.* 36 FROM USER u 37 LEFT JOIN user_role ur 38 ON u.uid = ur.uid 39 LEFT JOIN role r 40 ON ur.rid = r.rid 41 <where> 42 <if test="username!=null">u.user_name=#{username}</if> 43 </where> 44 </select> 45 </mapper>
package com.free.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; 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 com.free.mybatis.domain.Role; import com.free.mybatis.domain.User; @SuppressWarnings(value = { "unused" }) public class testDemo1 { @Test public void test1() throws IOException { String resource="sqlMapConfig.xml"; // InputStream is = testDemo1.class.getClassLoader().getResourceAsStream(resource); // resource.get InputStream is = Resources.getResourceAsStream(resource); SqlSessionFactory sfb = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sfb.openSession(); Map<String,Object> map = new HashMap<String,Object>(); // map.put("username", "') // new HashMap<>(); // map.put("username", "freedom"); List<User> list = sqlSession.selectList("mybatis.UserMapper.findUserRole", map); for(User user : list) { // System.out.println(user.getRoles()); System.out.println(user.toString()); // for(Role roles :user.getRoles()) // System.out.println(user.getUser_name()+":"+roles.getName()); } // sqlSession.commit(); // System.out.println(list); } }
<?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> <!--datasource和transaction --> <!--test/development--> <environments default="test"> <!--可以有多个环境,测试和部署 --> <environment id="test" > <!-- 事务:JDBC/MANAGED jdbc默认事务部自动提交--> <transactionManager type="JDBC"> </transactionManager> <!-- 数据源:POOLED/UNPOOLED/JNDI --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/gyl" /> <property name="username" value="root" /> <property name="password" value="freedom2012" /> </dataSource> </environment> <environment id="test" > <!-- 事务:JDBC/MANAGED jdbc默认事务部自动提交--> <transactionManager type="JDBC"> </transactionManager> <!-- 数据源:POOLED/UNPOOLED/JNDI --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/gyl" /> <property name="username" value="root" /> <property name="password" value="freedom2012" /> </dataSource> </environment> </environments> <mappers> <!--加载domain的映射 --> <mapper resource="mybatis/UserMapper.xml"/> </mappers> </configuration>
posted on 2015-11-05 14:57 freedom's_blog 阅读(696) 评论(0) 编辑 收藏 举报