#注意:要配置开启多条语句操作,否则会报错( org.apache.ibatis.exceptions.PersistenceException)
lf-driver=com.mysql.jdbc.Driver
lf-url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
lf-user=LF
lf-password=LF
<?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>
<properties resource="mybatis/config/db.properties"></properties>
<!-- 配置环境 -->
<environments default="development">
<environment id="development">
<!-- 配置事务管理器的类型 -->
<transactionManager type="JDBC"/>
<!-- 配置数据源相关的属性 -->
<dataSource type="UNPOOLED">
<property name="driver" value="${lf-driver}"/>
<property name="url" value="${lf-url}"/>
<property name="username" value="${lf-user}"/>
<property name="password" value="${lf-password}"/>
</dataSource>
</environment>
</environments>
<!--END 配置环境 --> <!-- 配置映射路径 -->
<mappers>
<mapper resource="mybatis/config/mapper/StudentMapper.xml"/>
</mappers>
<!-- END 配置映射路径 --> </configuration>
<?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"> <mapper namespace="cn.zr.mybatismore.entity.StudentMapper">
<!-- 添加数据 -->
<insert id="addData" parameterType="cn.zr.mybatismore.entity.Student"> INSERT INTO STUDENT (NAME,AGE) VALUES (#{name},#{age}); INSERT INTO COURSE (NAME,SCORE) VALUES
<foreach collection="courses" item="val" separator=",">
(#{val.name},#{val.score})
</foreach> </insert>
</mapper>
package cn.zr.mybatismore.entity;

public class Course {

    private String name;
private Integer score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public Course() {
}
public Course(String name, Integer score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Course [name=" + name + ", score=" + score + "]";
} }
package cn.zr.mybatismore.entity;

import java.util.List;

public class Student {

    private String name;
private Integer age;
private List<Course> courses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
public Student() {
}
public Student(String name, Integer age, List<Course> courses) {
this.name = name;
this.age = age;
this.courses = courses;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", courses="
+ courses + "]";
} }
package cn.zr.mybatismore.entity;

public interface StudentMapper {

    /**
* 添加数据
* @param student 被添加的对象
* @return 操作数据库的数据量
*/
int addData(Student student); }
package cn.zr.mybatismore.utils;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionManager; import cn.zr.mybatismore.entity.Course;
import cn.zr.mybatismore.entity.Student;
import cn.zr.mybatismore.entity.StudentMapper; public class TestMore { public static void main(String[] args) {
TestMore testMore = new TestMore();
testMore.addData();
} private static SqlSessionManager sessionManager;
private static StudentMapper studentMapper;
static{
String resource = "mybatis/config/mybatis-config.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sessionManager = SqlSessionManager.newInstance(reader);
studentMapper = sessionManager.getMapper(StudentMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
} public void addData(){ Course course1 = new Course("math", 80);
Course course2 = new Course("chinese", 90);
Course course3 = new Course("english", 85);
List<Course> courses =new ArrayList<Course>();
courses.add(course1);
courses.add(course2);
courses.add(course3);
Student student = new Student();
student.setName("ganlu");
student.setAge(23);
student.setCourses(courses);
System.out.println(student);
int count = studentMapper.addData(student);
if (count > 0) {
System.out.println("操作成功"+count+"条数据");
}else {
System.out.println("操作失败");
}
} }

mybatis 框架 的应用之三(操作两张没有关联的表,存在主键和外键关系)的更多相关文章

  1. Sql语句中两个比较迷糊的概念:“连接查询” 与 “外键约束”

    Sql语句中两个比较迷糊的概念:“连接查询” 与 “外键约束 Sql 中的连接查询:就是为了避免笛卡尔积,因为涉及到多表查询的化,不使用连接查询,会先将多个互相乘,求出笛卡尔积,然后在在里面查询符合的 ...

  2. (10)MySQL触发器(同时操作两张表)

    什么是触发器 触发器是与表有关的数据库对象,在满足定义条件时触发,并执行触发器中定义的语句集合.触发器的这种特性可以协助应用在数据库端确保数据的完整性. 举个例子,比如你现在有两个表[用户表]和[日志 ...

  3. SQL 两张结构一样的表合并查询 .

    select * from table1 union all select * from table2 union all 是所有的都显示出来: select * from table1 union ...

  4. 【转】 mysql使用federated引擎实现远程访问数据库(跨网络同时操作两个数据库中的表)

    原文转自:http://www.2cto.com/database/201412/358397.html 问题: 这里假设我需要在IP1上的database1上访问IP2的database数据库内的t ...

  5. 【mybatis】mybatis查询 结果 用map接收,无实体接收 + 关联子表 一并返回主子表的结果

    如果后台程序没有实体对应mysql的数据表. 而mybatis想要查询mysql这个数据表的数据,返回给应用程序. 应用程序该如何接收? =============================== ...

  6. 两种获取MySql数据库中所有表的主键和外键约束信息的Sql语句

    最近在写Rafy底层的一些东西,在数据库方面把MySql数据库集成到里面去,里面有一个需求,需要获取非系统数据库,也就是我们自己建立的数据库中所有表的主键和外键元数据列表. 第一种方法:是网上的方法, ...

  7. mysql之表与表关联和表操作

    一 表于表之间的关联 foregin key:设置外键表于表之间建立关联. 多对一关联: 创建步骤,应该先创建好被关联的那一张表,然后再去创建关联的那一张表. 关联表的多条对应着被关联的那张表的一条记 ...

  8. myBatis框架之入门(四)

    Mybatis多表管理查询 多表关联关系分析: 多表关联:至少两个表关联.分析多表关系的经验技巧:从一条记录出发,不要从表整体去分析,比如分析A表和B表关系,A表中的一条记录对应B表中的几条记录,如果 ...

  9. ssm(spring、springmvc、mybatis)框架整合

    第一次接触这3大框架,打算一个一个慢慢学,参照网上资料搭建了一个ssm项目,作为新手吃亏在jar包的导入上,比如jdbc DataSource配置的时候由于导入的jar包不兼容或者缺包导致项目无法正常 ...

随机推荐

  1. Linux系统中的wc

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  2. Python笔记-3

    一.文件的操作 1.文件的读.写.新增 读文件的获取句柄的语法:f=open('path/file') 句柄的理解: <_io.TextIOWrapper name='/root/myfile/ ...

  3. 处理get中文乱码

    package com.servlet;              import java.io.IOException;       import java.io.PrintWriter;      ...

  4. gulp 合格插件评判标准

    官方插件列表:    https://gulpjs.com/plugins/   合格插件的判断标准 1. 不修改内容 如果一个插件一个文件都修改(无论是文案内容,文件路径),那么它就不是一个gulp ...

  5. Python函数 help()

    **help() 功能: help() 函数用于查看函数或模块用途的详细说明.object -- 对象:返回对象帮助信息. 语法: help([object]) 实例: >>>hel ...

  6. 学习FPGA有必要写SDRAM控制器吗?

    在学习FPGA的过程中,注意是在学习过程中,联系FPGA的使用技巧,强烈建议尝试设计一个SDRAM控制器,不要使用IP核. 学习SDRAM控制器设计,能让你掌握很多知识. 更好的使用状态机去精准控制时 ...

  7. TCP,你懂的

    TCP(Transmission Control Protocol 传输控制协议)是一种面向连接(连接导向)的.可靠的. 基于IP的传输层协议.TCP在IP报文的协议号是6.TCP是一个超级麻烦的协议 ...

  8. 蓝桥杯 算法训练 ALGO-93 反置数

    算法训练 反置数   时间限制:1.0s   内存限制:512.0MB 问题描述 一个整数的“反置数”指的是把该整数的每一位数字的顺序颠倒过来所得到的另一个整数.如果一个整数的末尾是以0结尾,那么在它 ...

  9. FTP mget without prompt

    # ftp 192.168.100.2Connected to 192.168.100.2.220 Microsoft FTP ServiceName (192.168.100.2:root): ja ...

  10. 求分数1+1/2+1/3+.....+1/n的值

    总结:自己理解错了的有以下几点: 1.s初始化的值是0.但数据类型最好定位double双精度类型 2.for循环里面的i<n.不要忘了等号,因为i作为分母,不能为0,所以从1开始, 3.在mai ...