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

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




    

变成:

@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.具体配置文件:

"mybatis.mapper.BlogMapper">

    -- one2one -->
    

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

    -- one2many -->

    

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


    

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

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

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

        

    

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

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,熟悉了解各个部分的作用,注释来的简单,容易忘记

你可能感兴趣的:(整合)