package com.pc.mybatiscomplex;
import java.util.List;
/**
* Created by switch on 16/9/13.
* 职员Bean(POJO)————与user表映射
*/
public class User {
private Integer id;
private String userName;
private String corp;
private List courses;
public User(Integer id, String userName, String corp) {
this.id = id;
this.userName = userName;
this.corp = corp;
}
public User(String userName, String corp) {
this.userName = userName;
this.corp = corp;
}
public Integer getId() {
return id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
public List getCourses() {
return courses;
}
public void setCourses(List courses) {
this.courses = courses;
}
}
package com.pc.mybatiscomplex;
/**
* Created by switch on 16/9/13.
* 课程Bean(POJO)————与course表映射
*/
public class Course {
private Integer id;
private String courseName;
private Teacher teacher;
public Course(Integer id, String courseName, Teacher teacher) {
this.id = id;
this.courseName = courseName;
this.teacher = teacher;
}
public Course(String courseName, Teacher teacher) {
this.courseName = courseName;
this.teacher = teacher;
}
public Course() {
}
public Integer getId() {
return id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
package com.pc.mybatiscomplex;
/**
* Created by switch on 16/9/13.
* 讲师Bean(POJO)————与teacher表映射
*/
public class Teacher {
private Integer id;
private String teacherName;
public Teacher(Integer id, String teacherName) {
this.id = id;
this.teacherName = teacherName;
}
public Teacher(String teacherName) {
this.teacherName = teacherName;
}
public Integer getId() {
return id;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
}
package com.pc.mybatiscomplex;
/**
* Created by switch on 16/9/13.
* 职员操作接口
*/
public interface UserOp {
// 获取职员信息
public User getUser(Integer id);
}
package com.pc.mybatiscomplex;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
/**
* Created by switch on 16/9/13.
* 测试类
*/
public class HelloMyBatisComplex {
public static void main(String[] args) {
// 1. 声明配置文件的目录
String resource = "confComplex.xml";
// 2. 加载应用配置文件
InputStream is = HelloMyBatisComplex.class.getClassLoader().getResourceAsStream(resource);
// 3. 创建SqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
// 4. 获取Session————try-with-resource
try (SqlSession session = sessionFactory.openSession()) {
// 5. 获取操作类
UserOp userOp = session.getMapper(UserOp.class);
// 6. 完成查询操作
User user = userOp.getUser(1);
// 验证、输出结果
// 用户信息
System.out.println("--------用户信息--------");
System.out.println("职员ID:" + user.getId() + " 职员名:" + user.getUserName() + " 公司名:" + user.getCorp());
// 选课信息
System.out.println("--------选课信息--------");
for (Course course : user.getCourses()) {
System.out.println("课程ID:" + course.getId() + " 课程名:" + course.getCourseName() +
" 讲师ID:" + course.getTeacher().getId() + " 讲师名:" + course.getTeacher().getTeacherName());
}
}
}
}
-- 职员表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- 职员id
`userName` varchar(100) NOT NULL, -- 职员名
`corp` varchar(100) DEFAULT NULL, -- 公司
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO user(userName, corp) VALUES("张三", "A公司");
INSERT INTO user(userName, corp) VALUES("李四", "B公司");
INSERT INTO user(userName, corp) VALUES("王五", "C公司");
INSERT INTO user(userName, corp) VALUES("赵六", "B公司");
INSERT INTO user(userName, corp) VALUES("钱七", "A公司");
-- 讲师表
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- 讲师id
`teacherName` varchar(100) DEFAULT NULL, -- 讲师名
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO teacher(teacherName) VALUES("张老师");
INSERT INTO teacher(teacherName) VALUES("李老师");
INSERT INTO teacher(teacherName) VALUES("王老师");
INSERT INTO teacher(teacherName) VALUES("赵老师");
INSERT INTO teacher(teacherName) VALUES("钱老师");
-- 课程表
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- 课程id
`courseName` varchar(100) DEFAULT NULL, -- 课程名
`teacher_id` int(11) DEFAULT NULL, -- 讲师id
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO course(courseName, teacher_id) VALUES("MyBatis", 1);
INSERT INTO course(courseName, teacher_id) VALUES("JDBC", 2);
INSERT INTO course(courseName, teacher_id) VALUES("Spring", 1);
INSERT INTO course(courseName, teacher_id) VALUES("Hibernate", 3);
INSERT INTO course(courseName, teacher_id) VALUES("JAVA", 4);
INSERT INTO course(courseName, teacher_id) VALUES("Oracle", 5);
INSERT INTO course(courseName, teacher_id) VALUES("MySQL", 1);
INSERT INTO course(courseName, teacher_id) VALUES("Struts", 5);
-- 职员选课表
CREATE TABLE `UserCourse` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- 选课id
`user_id` int(11) DEFAULT NULL, -- 职员id
`course_id` int(11) DEFAULT NULL, -- 课程id
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO UserCourse(user_id, course_id) VALUES(1, 1);
INSERT INTO UserCourse(user_id, course_id) VALUES(1, 3);
INSERT INTO UserCourse(user_id, course_id) VALUES(1, 5);
INSERT INTO UserCourse(user_id, course_id) VALUES(1, 7);
INSERT INTO UserCourse(user_id, course_id) VALUES(2, 2);
INSERT INTO UserCourse(user_id, course_id) VALUES(2, 4);
INSERT INTO UserCourse(user_id, course_id) VALUES(2, 6);
INSERT INTO UserCourse(user_id, course_id) VALUES(2, 8);
INSERT INTO UserCourse(user_id, course_id) VALUES(3, 1);
INSERT INTO UserCourse(user_id, course_id) VALUES(3, 2);
INSERT INTO UserCourse(user_id, course_id) VALUES(3, 5);
INSERT INTO UserCourse(user_id, course_id) VALUES(4, 3);
INSERT INTO UserCourse(user_id, course_id) VALUES(4, 5);
INSERT INTO UserCourse(user_id, course_id) VALUES(4, 7);
INSERT INTO UserCourse(user_id, course_id) VALUES(5, 1);
INSERT INTO UserCourse(user_id, course_id) VALUES(5, 4);
INSERT INTO UserCourse(user_id, course_id) VALUES(5, 6);
INSERT INTO UserCourse(user_id, course_id) VALUES(5, 7);
作为可选项,你也可以传递属性给数据库驱动。要这样做,属性的前缀为“driver.”,例如:
这将通过DriverManager.getConnection(url,driverProperties)方法传递值为 UTF8 的 encoding 属性给数据库驱动。
和其他数据源配置类似,可以通过添加前缀“env.”直接把属性传递给初始上下文。比如: