Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the many-to-many mapping configuration methods of iBATIS

Shulou Source: shulou.com Published: 2022-06-02 07:41:11 09月17日 Update

Editor to share with you what iBATIS's many-to-many mapping configuration methods are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

IBATIS many-to-many mapping configuration 1, build the table. The three tables in the database are:

[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL, [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL CONSTRAINT [PK_student_teacher] PRIMARY KEY CLUSTERED ([studentid], [teacherid]) ON [PRIMARY]) ON [PRIMARY] GO

IBATIS many-to-many mapping configuration 2, prepare data

Insert into student (name,birthday) values ('Zhang San', '1982-01-01') insert into student (name,birthday) values ('Li Si', '1983-02-02') insert into student (name,birthday) values ('Wang Wu', '1984-03-03') insert into student (name,birthday) values ('Zhao Liu', '1985-04-04') insert into teacher (name,subject) values ('Jerry',' language') insert into teacher (name,subject) values ('Tom'') Mathematics) insert into teacher (name,subject) values ('Steven',' English') insert into student_teacher (studentid,teacherid) values (1) insert into student_teacher (studentid,teacherid) values (1) insert into student_teacher (studentid,teacherid) values (2) insert into student_teacher (studentid,teacherid) values (3)

The contents of the iBATIS's many-to-many mapping configuration 3century properties file are as follows:

Driver=com.microsoft.jdbc.sqlserver.SQLServerDriver url=jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;DatabaseName=ibatistest username=sa password=000

IBATIS's many-to-many mapping configuration 4, the general configuration file SqlMapConfig.xml content is as follows:

< sqlMapConfig > < properties resource=" com/lsm/cfg/jdbc.properties "/ > < transactionManager type=" JDBC "> < dataSource type=" SIMPLE "> < property Value= "${driver}" name= "JDBC.Driver" / > < property value= "${url}" name= "JDBC.ConnectionURL" / > < property value= "${username}" name= "JDBC.Username" / > < property value= "${password}" name= "JDBC.Password" / > < / dataSource > < / transactionManager > < sqlMap resource= "com/lsm/domain/Student.xml" / > < sqlMap resource= "com/lsm/ Domain/Teacher.xml "/ > < / sqlMapConfig >

IBATIS's many-to-many mapping configures two Student domain objects, Student and Teacher, as follows:

Teacher.java

Package com.lsm.domain; import java.util.List; public class Teacher {private int id; private String name; private String subject; private List students; / / Note that there is a students of type List, which means that a teacher corresponds to multiple students public List getStudents () {return students;} public void setStudents (List students) {this.students = students. } / / omit other getter and setter} / / Student.java package com.lsm.domain; import java.util.List; public class Student {private int id; private String name; private String birthday; private List teachers; / / there is a teachers of list type, indicating that a student has multiple teachers public List getTeachers () {return teachers;} public void setTeachers (List teachers) {this.teachers = teachers } / / omit other getter and setter}

IBATIS's many-to-many mapping configuration 6 _ sqlmap configuration file

Teacher.xml

! DOCTYPE sqlMap PUBLIC "- / ibatis.apache.org//DTD SQL Map 2.0//EN"http://ibatis.apache.org/dtd/sql-map-2.dtd" > < sqlMap namespace=" teacher "> < typeAlias alias=" Teacher "type=" com.lsm.domain.Teacher "/ > < typeAlias alias=" Student "type=" com.lsm. Domain.Student "/ > < resultMap class=" Teacher "id=" teacherBasicResultMap "> < result property=" id "column=" id "/ > < result property=" name "column=" name "/ > < result property=" subject "column=" subject "/ > < / resultMap >

Tags: Configuration information students teachers content results Zhang San queries files articles Li Si Wang Wu UTF-8 methods multiple mathematics data types Chinese English Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi MySQL MariaDB macOS