sm(spring+mybatis)注释版的整合
程序员文章站
2022-07-13 21:11:41
...
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,熟悉了解各个部分的作用,注释来的简单,容易忘记