您的位置:365bet体育备用网址器 > 应用 > 、、、、、、、、、、、、2. 猪配置文件,auth

、、、、、、、、、、、、2. 猪配置文件,auth

2019-10-29 14:53

MyBatis从入门到放弃四:一对多关联查询,mybatis入门

前言

      上篇学习了一对一关联查询,这篇我们学习一对多关联查询。一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collection属性,别忽略了ofType属性。

 

paip.环境设置 mybatis ibatis cfg 环境设置

搭建开发环境

     创建表author、表blog,构建一对多的查询场景。

365bet在线官网 1

     创建author、blog model。author类中主要是添加属性List<Blog> blogs属性。

public class Author {
    private int id;
    private String name;
    private List<Blog> blogs;

    public List<Blog> getBlogs() {
        return blogs;
    }

    public void setBlogs(List<Blog> blogs) {
        this.blogs = blogs;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

public class Blog {
    private int id;
    private String title;
    private String category;
    private int author_id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getAuthor_id() {
        return author_id;
    }

    public void setAuthor_id(int author_id) {
        this.author_id = author_id;
    }
}

  在mybatis.xml创建alias、引用resource mapper.xml

365bet在线官网 2 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "; 5 <configuration> 6 7 <!-- MyBatis针对SqlServer进行的配置 --> 8 <typeAliases> 9 <typeAlias alias="User" type="com.autohome.model.User"/> 10 <typeAlias alias="Teacher" type="com.autohome.model.Teacher" /> 11 <typeAlias alias="Student" type="com.autohome.model.Student" /> 12 <typeAlias alias="Author" type="com.autohome.model.Author" /> 13 <typeAlias alias="Blog" type="com.autohome.model.Blog" /> 14 </typeAliases> 15 <environments default="development"> 16 <environment id="development"> 17 <transactionManager type="JDBC"/> 18 <dataSource type="POOLED"> 19 <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> 20 <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"/> 21 <property name="username" value="sa"/> 22 <property name="password" value="0"/> 23 </dataSource> 24 </environment> 25 </environments> 26 27 28 <mappers> 29 <mapper resource="mapper/Author.xml"/> 30 <mapper resource="mapper/User.xml"/> 31 <mapper resource="mapper/Student.xml"/> 32 </mappers> 33 </configuration> View Code

 

三部分
//////////1. 电泳。。。。
、、、、、、、、、、、、2. 猪配置文件  com/mijie/homi/search/util/ibatiascfg.xml
、、、、、、、、、、、、、、、、、3.mappers 配置文件  ibatis/index.xml

创建Mapper.xml(Author.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 <mapper namespace="com.autohome.mapper.Author">
 6     <resultMap id="authorResultMap" type="Author">
 7         <id property="id" column="id"/>
 8         <result property="name" column="name"/>
 9         <collection property="blogs" ofType="Blog">
10             <id column="bid" property="id"/>
11             <result column="title" property="title"/>
12             <result column="category" property="category"/>
13         </collection>
14     </resultMap>
15 
16     <select id="getAuthorBlogsById" parameterType="int" resultMap="authorResultMap">
17         SELECT a.id,name,b.id bid,title,category FROM t_author a
18         LEFT JOIN t_blog b on a.id=b.author_id
19         WHERE a.id=#{id}
20 
21     </select>
22 </mapper>

 


单元测试

 @Test
    public void getAuthorBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession=sqlSessionFactory.openSession();

            Author author = sqlSession.selectOne("com.autohome.mapper.Author.getAuthorBlogsById",1);
            System.out.println("作者信息 id:" author.getId() ",name:" author.getName());
            System.out.println("作者博客:");
            for(Blog blog:author.getBlogs()){
                System.out.println("id:" blog.getId() ",title:" blog.getTitle() ",category:" blog.getCategory());
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }

附单元测试截图

365bet在线官网 3

 

前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询。一对多关联查询关键...

//////////1. 电泳。。。。
 public static void main(String[] args) {
       // TODO Auto-generated method stub
       try {
           String resource = "com/mijie/homi/search/util/ibatiascfg.xml";
           Reader reader;

           reader = Resources.getResourceAsReader(resource);

           SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                   .build(reader);
           SqlSession sqlSession = sqlSessionFactory.openSession();
        List li=   sqlSession.selectList("getRecently100", 9999999);
//            StudentDao studentDao =sqlSession.getMapper(StudentDao.class);
//            Student st = studentDao.getstudent(1);
           System.out.println( li.size());

作者Attilax  艾龙,  EMAIL:1466519819@qq.com
来源:attilax的专栏
地址:

、、、、、、、、、、、、2. 猪配置文件  com/mijie/homi/search/util/ibatiascfg.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
";
<configuration>
<!--  
<properties resource="config/config.properties"></properties>
-->
<typeAliases>
<!--  
<package name="pojo" />
-->
<typeAlias alias="Topic" type="com.mijie.homi.search.domain.topic.Topic" />
<typeAlias alias="Topic" type="com.mijie.homi.search.domain.topic.Topic"  />
<typeAlias alias="TopicOptions" type="com.mijie.homi.search.domain.topic.TopicOptions"  />
<typeAlias alias="User" type="com.mijie.homi.common.domain.user.User"  />
365bet在线官网,<typeAlias alias="UserExtInfo" type="com.mijie.homi.common.domain.user.UserExtInfo"  />
<typeAlias alias="Mood" type="com.mijie.homi.common.domain.mood.Mood"  />
<typeAlias alias="MoodUser" type="com.mijie.homi.search.domain.user.MoodUser"  />
</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://192.168.0.132:3306/homi" />
<property name="username" value="root" />
<property name="password" value="password" />
<property name="poolMaximumActiveConnections" value="15" />
<property name="poolMaximumCheckoutTime" value="5000" />
<!-- 
<property name="username" value="${username}" />
<property name="password" value="${password}" />
-->
</dataSource>
</environment>
</environments>

  <mappers>
        <mapper resource="ibatis/index.xml" />
    </mappers>
</configuration>

、、、、、、、、、、、、、、、、、3.mappers 配置文件  ibatis/index.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  ";

<mapper namespace="com.mijie.homi.domain.mood.Mood4full">
   
   <!-- where id  <=#{id} and timelineId=0 -->
   <select id="getRecently100" parameterType="int" resultType="java.util.HashMap">
      
         <![CDATA[ select    * from mood4full  where id  <=#{id}  
         order by id desc   limit 100  ]]>
   </select>
   
      <select id="listRecentlyLoginUserMood" parameterType="map" resultType="Mood">
    select m.* from  userExtInfo as e join mood as m
on m.id=e.moodId
where e.lastLoginAt >= #{atleastLastLoginAt}
order by e.lastLoginAt desc 
limit #{offset},#{limit}
   </select>
   
   
      <select id="moodsByIds" parameterType="string" resultType="java.util.HashMap">
    select *,u.id uid,e.lastloginat from mood m,user u,userExtInfo e  where  m.userid=u.id and u.id=e.id and m.id in ( ${_parameter} ) order by m.id desc limit 100 

   </select>
   
   </mapper>    

本文由365bet体育备用网址器发布于应用,转载请注明出处:、、、、、、、、、、、、2. 猪配置文件,auth

关键词:

  • 上一篇:没有了
  • 下一篇:没有了