mybatis整合spring 之 基于接口映射的多对一关系
转载自:http://my.oschina.net/huangcongmin12/blog/83731
mybatis整合spring 之 基于接口映射的多对一关系。
项目用到俩个表,即student表和school表。表结构如下:
school表:

student表:

项目结构如下:

1)applicationContext.xml
|
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
|
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 加载JDBC配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --> <context:component-scan base-package="com.springbatis.dao" /> <context:component-scan base-package="com.springbatis.service" /> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass"> <value>${jdbc.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${jdbc.url}</value> </property> <property name="user"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <property name="minPoolSize"> <value>8</value> </property> <property name="maxPoolSize"> <value>200</value> </property> <property name="initialPoolSize"> <value>10</value> </property> <property name="maxIdleTime"> <value>60</value> </property> <property name="acquireIncrement"> <value>5</value> </property> <property name="maxStatements"> <value>10</value> </property> <property name="idleConnectionTestPeriod"> <value>60</value> </property> <property name="acquireRetryAttempts"> <value>30</value> </property> <property name="breakAfterAcquireFailure"> <value>true</value> </property> <property name="testConnectionOnCheckout"> <value>false</value> </property> </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定sqlMapConfig总配置文件 --> <property name="configLocation" value="classpath:mybatis-configuration.xml"/> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.springbatis.dao" /> </bean> <!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用注解控制事务 --> <tx:annotation-driven /> </beans> |
2)mybatis-configuration.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <!-- 别名 --> <typeAliases> <typeAlias alias="School" type="com.springbatis.domain.School"/> <typeAlias alias="Student" type="com.springbatis.domain.Student"/> </typeAliases> <!-- ORM映射文件 --> <mappers> <mapper resource="com/springbatis/domain/SchoolMapper.xml" /> <mapper resource="com/springbatis/domain/StudentMapper.xml" /> </mappers> </configuration> |
3)School Entity
|
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
|
package com.springbatis.domain;import java.io.Serializable;import java.util.ArrayList;import java.util.List;@SuppressWarnings("serial")public class School implements Serializable { private int id; private String schoolNumber; private String schoolName; private List<Student> students = new ArrayList<Student>(); public School(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSchoolNumber() { return schoolNumber; } public void setSchoolNumber(String schoolNumber) { this.schoolNumber = schoolNumber; } public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } } |
4)Student Entity
|
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
|
package com.springbatis.domain;import java.io.Serializable;@SuppressWarnings("serial")public class Student implements Serializable { private int id; private String studentNumber; private String studentName; private String sex; private School school; public Student(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStudentNumber() { return studentNumber; } public void setStudentNumber(String studentNumber) { this.studentNumber = studentNumber; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } } |
5)SchoolMapper.xml
|
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
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.springbatis.dao.SchoolMybatisDao"> <!-- 一对多映射 --> <resultMap id="schoolResult" type="School"> <id property="id" column="school_id" /> <result property="schoolNumber" column="school_number"/> <result property="schoolName" column="school_name"/> <collection property="students" ofType="Student"> <id property="id" column="student_id"/> <result property="studentNumber" column="student_number"/> <result property="studentName" column="student_name"/> <result property="sex" column="student_sex"/> </collection> </resultMap> <select id="loadSchoolWithStudent" parameterType="int" resultMap="schoolResult"> select sch.id as school_id, sch.schoolNumber as school_number, sch.schoolName as school_name, stu.id as student_id, stu.studentNumber as student_number, stu.studentName as student_name, stu.sex as student_sex from school sch left outer join student stu on sch.id=stu.school_id where sch.id=#{school_id} </select> </mapper> |
6)StudentMapper.xml
|
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
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.springbatis.dao.StudentMybatisDao"> <!-- 多对一映射 --> <resultMap id="studentResult" type="Student" > <id property="id" column="student_id"/> <result property="studentNumber" column="student_number"/> <result property="studentName" column="student_name"/> <result property="sex" column="student_sex"/> <association property="school" javaType="School"> <id property="id" column="school_id"/> <result property="schoolNumber" column="school_number"/> <result property="schoolName" column="school_name"/> </association> </resultMap> <select id="loadStudentWithSchool" parameterType="int" resultMap="studentResult"> select stu.id as student_id, stu.studentNumber as student_number, stu.studentName as student_name, stu.sex as student_sex, sch.id as school_id, sch.schoolNumber as school_number, sch.schoolName as school_name from student stu left outer join school sch on sch.id=stu.school_id where stu.id=#{student_id} </select> </mapper> |
7)SchoolMybatisDao Interface
|
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.springbatis.dao;import org.springframework.stereotype.Repository;import com.springbatis.domain.School;@Repositorypublic interface SchoolMybatisDao { School loadSchoolWithStudent(int school_id); } |
8)StudentMybatisDao Interface
|
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.springbatis.dao;import org.springframework.stereotype.Repository;import com.springbatis.domain.Student;@Repositorypublic interface StudentMybatisDao { Student loadStudentWithSchool(int student_id); } |
9)SchoolService Interface
|
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.springbatis.service;import org.springframework.stereotype.Repository;import com.springbatis.domain.School;@Repositorypublic interface SchoolService { public School loadSchoolWithStudent(int school_id); } |
10)StudentServiec Interface
|
1
2
3
4
5
6
7
8
9
|
package com.springbatis.service;import com.springbatis.domain.Student;public interface StudentService { public Student loadStudentWithSchool(int student_id); } |
11)SchoolService Implement
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.springbatis.service.implement;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.springbatis.dao.SchoolMybatisDao;import com.springbatis.domain.School;import com.springbatis.service.SchoolService;@Service@Transactionalpublic class SchoolServiceImpl implements SchoolService { @Autowired private SchoolMybatisDao schoolDao; @Override public School loadSchoolWithStudent(int school_id) { return schoolDao.loadSchoolWithStudent(school_id); }} |
12)StudentService Implement
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.springbatis.service.implement;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.springbatis.dao.StudentMybatisDao;import com.springbatis.domain.Student;import com.springbatis.service.StudentService;@Service@Transactionalpublic class StudentServiceImpl implements StudentService { @Autowired private StudentMybatisDao studentDao; @Override public Student loadStudentWithSchool(int student_id) { return studentDao.loadStudentWithSchool(student_id); }} |
13)test/StudentServiceTest
|
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
|
package com.springbatis.service;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.springbatis.domain.Student;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="/applicationContext.xml")public class StudentServiceTest { @Autowired private StudentService studentService; @Test public void testLoadStudentWithSchool(){ Student stu = studentService.loadStudentWithSchool(2); if(stu!=null){ System.out.println("=====》studentId:"+stu.getId()+" studentNumber:"+stu.getStudentNumber()+" studentName:"+stu.getStudentName()+" sex:"+stu.getSex()); System.out.println("=====》schoolId:"+stu.getSchool().getId()+" schoolNumber:"+stu.getSchool().getSchoolNumber()+" schoolName:"+stu.getSchool().getSchoolName()); } else{ System.out.println("id不存在!!"); } }} |
14)test/SchoolServiceTest
|
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
|
package com.springbatis.service;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.springbatis.domain.School;import com.springbatis.domain.Student;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="/applicationContext.xml")public class SchoolServiceTest { @Autowired private SchoolService schoolService; @Test public void testLoadSchoolWithStudent(){ School school = schoolService.loadSchoolWithStudent(1); if(school!=null){ System.out.println("=====》schoolId:"+school.getId()+" schoolNumber:"+school.getSchoolNumber()+" schoolName:"+school.getSchoolName()); if(school.getStudents().size()>0){ for(Student stu : school.getStudents()){ System.out.println("=====》studentId:"+stu.getId()+" studentNumber:"+stu.getStudentNumber()+" studentName:"+stu.getStudentName()+" sex:"+stu.getSex()); } }else{ System.out.println("无学生!"); } } else{ System.out.println("id不存在!!"); } } } |
15) 执行testLoadSchoolWithStudent()
输出:
|
1
2
3
4
|
==》schoolId: 1 schoolNumber:100001 schoolName:清华大学=====》studentId:1 studentNumber:1007300220 studentName:露西 sex:女=====》studentId:2 studentNumber:1007300222 studentName:杰克 sex:男=====》studentId:4 studentNumber:1007300225 studentName:露露 sex:女 |
mybatis整合spring 之 基于接口映射的多对一关系的更多相关文章
- JAVAEE——Mybatis第二天:输入和输出映射、动态sql、关联查询、Mybatis整合spring、Mybatis逆向工程
1. 学习计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Foreach标签 3.关联查询 a) 一对 ...
- (转)MyBatis框架的学习(六)——MyBatis整合Spring
http://blog.csdn.net/yerenyuan_pku/article/details/71904315 本文将手把手教你如何使用MyBatis整合Spring,这儿,我本人使用的MyB ...
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
- 160330、Mybatis整合Spring
转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...
- 不需要怎么修改配置的Mybatis整合Spring要点
首先对于Mybatis的主配置文件,只需要修改一处地方,将事务交给Spring管理,其它地方可以原封不动. <?xml version="1.0" encoding=&quo ...
- Mybatis整合spring(适合小白)
目录 1.整合思路 2.整合需要的jar包 3.整合的步骤 4.Dao的开发的两种实现方式 6.Dao的开发的实现方式总结图 @ Mybatis整合spring其实就是SSM框架中SM的整合集成. 1 ...
- Mybatis整合spring详细教程(适合小白童鞋)
目录 1.整合思路 2.整合需要的jar包 3.整合的步骤 4.Dao的开发的两种实现方式 6.Dao的开发的实现方式总结图 @ Mybatis整合spring其实就是SSM框架中SM的整合集成. 1 ...
- mybatis整合Spring编码
mybatis整合Spring的核心代码 spring-dao.xml <?xml version="1.0" encoding="UTF-8"?> ...
- MyBatis整合Spring原理分析
目录 MyBatis整合Spring原理分析 MapperScan的秘密 简单总结 假如不结合Spring框架,我们使用MyBatis时的一个典型使用方式如下: public class UserDa ...
随机推荐
- BIEE 创建一个简单的分析(2)
步骤: 1.如果BIEE安装在本机,直接登录http://localhost:9704/analytics/ 点击右上方导航菜单中的“新建->分析” 2.选择上节创建的RPD文件中的SCOTT主 ...
- BZOJ-4300 绝世好(蛋疼)题 DP(递推)
翻zky学长的blog时翻出来的..... 4300: 绝世好题 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 736 Solved: 393 [Sub ...
- Jfreechart 乱码
整个图标分成三部分chart title,chart 的plot还有chart的 legend三个部分需要对他们分别设置字体就对了. 先看解决方法( 把这几个全部设置了,都搞定了就可以了): ...
- Unix 目录结构的来历
Unix(包含Linux)的初学者,常常会很困惑,不明白目录结构的含义何在.Unix 目录结构的来历举例来说,根目录下面有一个子目录/bin,用于存放二进制程序.但是,/usr子目录下面还有/usr/ ...
- 使用Android Studio搭建Android集成开发环境
有很长一段时间没有更新博客了,最近实在是太忙了,没有时间去总结,现在终于可以有时间去总结一些Android上面的东西了,很久以前写过这篇关于使用Android Studio搭建Android集成开发环 ...
- iOS Xcode 调试技巧 全局断点这样加才有意思
http://blog.sina.com.cn/s/blog_876a2c9901016ezh.html
- 自定义NSLog无时间
#define SXLog(FORMAT, ...) fprintf(stderr,"file --\t%s\nline --\t%d\nmethd --\t%s\noutput --\t\ ...
- 锋利的jQuery-4--给事件添加命名空间
可以把为元素绑定的多个事件用命名空间规范起来. $(function(){ $("p").bind("mouseover.plugin", function() ...
- ssh中getHibernateTemplate()的方法
spring接着又把业务类中的查询也封装成了find() //用来实现分页 /*HibernateTemplate ht=this.getHibernateTemplate(); DetachedCr ...
- [pdf.js]预览pdf时,中文名称乱码的问题
在项目中使用了pdf.js的方式预览pdf,但针对中文名称的时候会出现乱码,导致找不到该文件而出现错误. 解决办法 <script src="viewer.js" chars ...