欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-13 21:11:47
...

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> -->

变成:

@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;
    }

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>

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

public interface BlogMapper {
    //one2one
    Blog findByID(Long id);
    //one2many
    Blog findByID2(Long id);
    Blog findByID3(Long id);
}

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

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

相关标签: 框架