Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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 >

Share To

Development

© 2024 shulou.com SLNews company. All rights reserved.

12
Report