sm(spring+mybatis)注释版的整合

1.导入所需的包 
2.mybatis配置文档与spring整合 
(1)数据源的配置:

<!-- <properties resource="db.properties">
    </properties> -->
<!-- <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />

            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClassName}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments> -->
<!-- 将包名下的映射文件下所有xml导入 , 也可以单个添加 -->
    <!-- <mappers>
        <package name="mybatis.mapper"/>
    </mappers> -->
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

变成:

@Configuration  //beans
@ComponentScan("mybatis")  //component
@EnableTransactionManagement  //事务  
@PropertySource("db.properties")  //引入properties文件
@MapperScan("mybatis.mapper")   //扫描具体xml UserMapper.xml
//工厂配置
@Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        factoryBean.setDataSource(dataSource);
        return factoryBean;
    }
    //数据源
    @Bean                       // 依赖Environment
    public DataSource dataSource(Environment env) {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        // env.getProperty("someKey") 获得属性值
        ds.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        ds.setUrl(env.getProperty("jdbc.url"));
        ds.setUsername(env.getProperty("jdbc.username"));
        ds.setPassword(env.getProperty("jdbc.password"));
        return ds;
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3.具体配置文件:

<mapper namespace="mybatis.mapper.BlogMapper">

    <!-- one2one -->
    <select id="findByID" resultMap="blogid">
        select b.id blog_id,a.id author_id,b.title blog_title,a.name author_name 
        from author a inner join blog b 
        on a.id = b.id 
        where b.id = #{id}
    </select>

    <resultMap type="Blog" id="blogid">
        <id property="id" column="blog_id"/>
        <result property="title" column="blog_title"/>
        <!-- 链接另外一张表(一对一) -->
        <association property="author" javaType="Author">
            <id property="id" column="author_id"/>
            <result property="name" column="author_name"/>
        </association>
    </resultMap>

    <!-- one2many -->

    <select id="findByID2" resultMap="blogid2">
        select b.id blog_id,b.title blog_title,
          a.id author_id,a.name author_name ,
          p.subject post_subject,p.id post_id
        from author a inner join blog b 
        on b.id = a.blog_id 
        left join post p
        on b.id = p.blog_id
        where b.id = #{id}
    </select>

    <resultMap type="Blog" id="blogid2">
        <id property="id" column="blog_id"/>
        <result property="title" column="blog_title"/>
        <association property="author" javaType="Author">
            <id property="id" column="author_id"/>
            <result property="name" column="author_name"/>
        </association>
        <!-- collection(ofType)一对多  类型也不同余association(javaType) -->
        <collection property="postlist" ofType="Post">
            <id property="id" column="post_id"/>
            <result property="subject" column="post_subject"/>
        </collection>
    </resultMap>


    <select id="findByID3" resultMap="blogid3">
        select b.id blog_id,b.title blog_title,
          a.id author_id,a.name author_name ,
          p.subject post_subject,p.id post_id,
          c.id comment_id,c.content comment_content
        from author a inner join blog b 
        on b.id = a.blog_id 
        left join post p
        on b.id = p.blog_id
        left join comment c
        on c.post_id = p.id
        where b.id = #{id}
    </select>

    <resultMap type="Blog" id="blogid3">
        <id property="id" column="blog_id"/>
        <result property="title" column="blog_title"/>
        <!-- <association property="author" javaType="Author">
            <id property="id" column="author_id"/>
            <result property="name" column="author_name"/>
        </association> -->
        <association property="author" resultMap="authorResult"></association>

        <collection property="postlist" ofType="Post">
            <id property="id" column="post_id"/>
            <result property="subject" column="post_subject"/>

            <collection property="commentlist" ofType="Comment">
            <id property="id" column="comment_id"/>
            <result property="content" column="comment_content"/>
            </collection>

        </collection>

    </resultMap>

    <!-- 对于相同的链接另外一张表  可进行抽取, 但是限制了别名格式  列如:column="author_id"  column="author_name" -->
    <resultMap type="Author" id="authorResult">
        <id property="id" column="author_id" />     
        <result property="name" column="author_name"/>  
    </resultMap>
</mapper>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

4.BlogMapper —>dao 没有具体的实现类,只有接口,具体实现都写在BlogMapper.xml中的具体sql语句了,执行方法被Mybatis封装了

public interface BlogMapper {
    //one2one
    Blog findByID(Long id);
    //one2many
    Blog findByID2(Long id);
    Blog findByID3(Long id);
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5.关于带参数的crud,mybatis只接受一个参数,基本类型一个,类类型,map类型。当多个参数时,应该用代码封装成集合,或者注释@Param

相比于xml整合的简单很多。还是推荐xml,熟悉了解各个部分的作用,注释来的简单,容易忘记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VR(Virtual Reality)即虚拟现实,是一种可以创建和体验虚拟世界的计算机技术。它利用计算机生成一种模拟环境,是一种多源信息融合的、交互式的三维动态视景和实体行为的系统仿真,使用户沉浸到该环境中。VR技术通过模拟人的视觉、听觉、触觉等感觉器官功能,使人能够沉浸在计算机生成的虚拟境界中,并能够通过语言、手势等自然的方式与之进行实时交互,创建了一种适人化的多维信息空间。 VR技术具有以下主要特点: 沉浸感:用户感到作为主角存在于模拟环境中的真实程度。理想的模拟环境应该使用户难以分辨真假,使用户全身心地投入到计算机创建的三维虚拟环境中,该环境中的一切看上去是真的,听上去是真的,动起来是真的,甚至闻起来、尝起来等一切感觉都是真的,如同在现实世界中的感觉一样。 交互性:用户对模拟环境内物体的可操作程度和从环境得到反馈的自然程度(包括实时性)。例如,用户可以用手去直接抓取模拟环境中虚拟的物体,这时手有握着东西的感觉,并可以感觉物体的重量,视野中被抓的物体也能立刻随着手的移动而移动。 构想性:也称想象性,指用户沉浸在多维信息空间中,依靠自己的感知和认知能力获取知识,发挥主观能动性,寻求解答,形成新的概念。此概念不仅是指观念上或语言上的创意,而且可以是指对某些客观存在事物的创造性设想和安排。 VR技术可以应用于各个领域,如游戏、娱乐、教育、医疗、军事、房地产、工业仿真等。随着VR技术的不断发展,它正在改变人们的生活和工作方式,为人们带来全新的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值