第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Mybatis如何實(shí)現(xiàn)對(duì)象的更新?

Mybatis如何實(shí)現(xiàn)對(duì)象的更新?

現(xiàn)在有這樣兩張表: CREATE TABLE STUDENTS ( stud_id int(11) NOT NULL AUTO_INCREMENT, name varchar(50) NOT NULL, email varchar(50) NOT NULL, phone_number varchar(15) NULL, dob date DEFAULT NULL, address_id int(11) NOT NULL, PRIMARY KEY (stud_id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; /*Sample Data for the students table */ insert into students(stud_id,name,email,dob,address_id) values (1,'Student1','student1@gmail.com','1983-06-25',1); insert into students(stud_id,name,email,dob) values (2,'Student2','student2@gmail.com','1983-06-25',2); CREATE TABLE ADDRESS ( address_id int(11) NOT NULL AUTO_INCREMENT, city varchar(50) NOT NULL, state varchar(50) NOT NULL, PRIMARY KEY (address_id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; insert into address(city,state) values('北京','北京'); insert into address(city,state) values('上海','上海'); 對(duì)于關(guān)聯(lián)查詢(xún),是比較簡(jiǎn)單的: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- type 就是java類(lèi)型 resultMap就是java類(lèi)屬性名與表字段名的映射--> <mapper namespace="com.mybatis.mappers.StudentMapper"> <resultMap type="com.mybatis.mappers.Student" id="StudentResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <result property="dob" column="dob" /> <result property="address.addressId" column="address_id"/> <result property="address.city" column="city"/> <result property="address.state" column="state"/> </resultMap> <select id="selectStudents" resultMap="StudentResult"> select students.*,address.* from students left outer join address on students.address_id=address.address_id </select> </mapper> 這樣,就能實(shí)現(xiàn)面向?qū)ο笫降牟樵?xún)。 但是,如何實(shí)現(xiàn)面向?qū)ο笫降母履??比?外在的API接口是如下的: @Test public void testStudent() throws IOException{ InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session= factory.openSession(); session.getConnection(); /* * XML文件與Mapper類(lèi)的名字保持一致,就能自動(dòng)關(guān)聯(lián)映射。 * */ StudentMapper mapper = session.getMapper(StudentMapper.class); List<Student> students= mapper.selectStudents(); for (Student student : students) { //更新了Address: student.getAddress().setCity("西安"); //直接實(shí)現(xiàn)保存:更改的實(shí)際上是Address這個(gè)表: mapper.save(student); } }
查看完整描述

1 回答

?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

想了一下,這種需求好像在關(guān)系型數(shù)據(jù)庫(kù)中間比較奇葩。。。在nosql當(dāng)中倒是比較常見(jiàn)。

查看完整回答
反對(duì) 回復(fù) 2019-03-01
  • 1 回答
  • 0 關(guān)注
  • 2299 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)