项目结构:

Menu

package com.mstf.dao;

import java.util.Scanner;

import org.apache.ibatis.session.SqlSession;
import com.mstf.util.MyBatisUtil; public class Menu { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) {
SqlSession session=MyBatisUtil.getSession();
System.out.println("请输入菜单选项:");
System.err.println("1.根据订单ID查询信息");
System.err.println("2.添加客户订单信息");
String id = sc.next();
if ("1".equals(id)) {
Table.table1();
} else if ("2".equals(id)) {
Table.table2();
} else {
System.err.println("非法操作:没有此选项!");
session.close();
}
}
}

  Table

package com.mstf.dao;

import java.util.List;
import java.util.Scanner; import org.apache.ibatis.session.SqlSession; import com.mstf.entity.T_customer;
import com.mstf.entity.T_order;
import com.mstf.util.MyBatisUtil; public class Table { private static Scanner sc = new Scanner(System.in);
static SqlSession session=MyBatisUtil.getSession(); /**
* 根据订单 ID 查询
* @author wangzheng
*/
public static void table1(){
System.err.println("请输入订单ID:");
String bug=sc.next();
try {
List<T_order> list = session.selectList("getT_customerT_order",bug);
for (T_order t_order : list) {
System.out.println(t_order);
}
} finally {
session.commit();
session.close();
System.out.println("查询一条数据成功!如果没有数据显示,则为无此订单ID!");
}
} /**
* 添加一条新数据
* @author wangzheng
*/
public static void table2(){
int customer_id=0;
System.out.println("请输入客户姓名:");
String name=sc.next();
System.out.println("请输入客户年龄:");
String age=sc.next();
System.out.println("请输入客户电话:");
String tel=sc.next();
System.out.println("请输入订单编号:");
String order_number=sc.next();
System.out.println("请输入订单价格:");
String order_price=sc.next();
try {
T_customer t_customer = new T_customer(0,name,age,tel);
T_order t_order = new T_order(0,order_number,order_price,customer_id);
session.insert("insert_T_customer",t_customer);
session.insert("insert_T_order",t_order);
} finally {
session.commit();
session.close();
System.out.println("添加数据成功!");
}
}
}

  T_customer

package com.mstf.entity;

public class T_customer {
private int id;
private String name;
private String age;
private String tel; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public T_customer(int id, String name, String age, String tel) {
super();
this.id = id;
this.name = name;
this.age = age;
this.tel = tel;
}
public T_customer() {
super();
}
@Override
public String toString() {
return "订单客户:[客户ID:" + id + ", 姓名:" + name + ", 年龄:" + age + ", 电话:" + tel + "]";
}
}

  T_order

package com.mstf.entity;

public class T_order {
private int id;
private String order_number;
private String order_price;
private T_customer t_customer;
private int customer_id; public int getCustomer_id() {
return customer_id;
}
public void setCustomer_id(int customer_id) {
this.customer_id = customer_id;
}
public T_customer getT_customer() {
return t_customer;
}
public void setT_customer(T_customer t_customer) {
this.t_customer = t_customer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOrder_number() {
return order_number;
}
public void setOrder_number(String order_number) {
this.order_number = order_number;
}
public String getOrder_price() {
return order_price;
}
public void setOrder_price(String order_price) {
this.order_price = order_price;
} public T_order(int id, String order_number, String order_price, int customer_id) {
super();
this.id = id;
this.order_number = order_number;
this.order_price = order_price;
this.customer_id = customer_id;
}
public T_order() {
super();
}
@Override
public String toString() {
return "系统订单:[订单ID:" + id + ", 订单号:" + order_number + ", 总价:" + order_price
+ "];\n" + t_customer + "\n";
} }

  T_customer.xml

<?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="com.mstf.mapper">
<select id="getT_customerT_order" resultMap="T_customerT_order" parameterType="String">
SELECT * FROM t_customer tc,t_order t WHERE tc.id=t.customer_id and t.customer_id =#{t.customer_id}
</select>
<resultMap type="T_order" id="T_customerT_order">
<id column="id" property="id"/>
<result column="order_number" property="order_number"/>
<result column="order_price" property="order_price"/>
<association property="t_customer" javaType="T_customer">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="tel" property="tel"/>
</association>
</resultMap>
</mapper>

  T_order.xml

<?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="com.mstf.mapper">
<insert id="insert_T_customer">
insert into t_customer (name, age, tel) values (#{name}, #{age}, #{tel})
</insert> <insert id="insert_T_order">
insert into t_order (order_number, order_price, customer_id) VALUES (#{order_number},#{order_price},(SELECT MAX(`id`) FROM `t_customer`))
</insert>
</mapper>

  MyBatisUtil

package com.mstf.util;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil {
// 去读取编写的 mybatis_config.xml 文件加载文件中的映射
public static SqlSession getSession() {
SqlSessionFactory ssf=null;
String url="mybatis_config.xml";
Reader reader=null;
try {
reader=Resources.getResourceAsReader(url);
} catch (Exception e) {
e.printStackTrace();
}
ssf=new SqlSessionFactoryBuilder().build(reader);
return ssf.openSession();
}
}

  db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1/demo
username=root
password=root

  log4j.properties

# 全局的日志配置
log4j.rootLogger = ERROR,stdout
# MyBatis 的日志配置
log4j.logger.org.fkit.mapper.UserMapper=DEBUG
# 控制台输出
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

  mybatis_config.xml

<?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">
<!-- XML 配置文件包含对 MyBatis 系统的核心配置 -->
<configuration>
<!-- 加载数据库配置文件 -->
<properties resource="db.properties"/>
<!-- 指定 MyBatis 所用日志的具体实现 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 给实体类类取别名 -->
<typeAliases>
<typeAlias type="com.mstf.entity.T_customer" alias="T_customer"/>
<typeAlias type="com.mstf.entity.T_order" alias="T_order"/>
</typeAliases>
<!-- 环境配置,即连接数据库 -->
<environments default="mysql">
<environment id="mysql">
<!-- 指定事务管理类型, type="JDBC" 指直接简单使用了 JDBC 的提交和回滚设置 -->
<transactionManager type="JDBC"/>
<!-- dataScurce 指数据源配置, POOLED 是 JDBC 连接对象的数据源连接池的实现 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- mappers 告诉了 MyBatis 去哪里找持久化类的映射文件 -->
<mappers>
<mapper resource="com/mstf/mapper/T_customer.xml"/>
<mapper resource="com/mstf/mapper/T_order.xml"/>
</mappers>
</configuration>

  

MyBatis+mysql查询和添加数据的更多相关文章

  1. mysql查询当天的数据

    mysql查询当天的数据 贴代码: #两个时间都使用to_days()函数 select * from reple where to_days(create_time) = to_days(NOW() ...

  2. mysql查询当天所有数据sql语句

    mysql查询当天的所有信息: select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) a ...

  3. 【转】mysql查询当天所有数据sql语句

    mysql查询当天的所有信息: select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) a ...

  4. php----处理从mysql查询返回的数据

    使用php的mysql,向mysql查询,返回的是一个资源,有4个函数可以进行处理. 1.mysql_fetch_row() 2.mysql_fetch_assoc() 3.mysql_fetch_a ...

  5. (转载)MySQl数据库-批量添加数据的两种方法

    方法一:使用excel表格 方法二:使用insert语句(FileWriter批量写入) 使用excel表格 1.打开数据表,按照表的字段在excel中添加数据.注意:表中字段名必须和excel中的名 ...

  6. MySQL - 查询今天的数据(以及昨天、本月、上个月、今年...) 查询Datetime 时间的数据

    1,查询当天(今天)的数据 1 SELECT * FROM `order` WHERE TO_DAYS(order_time) = TO_DAYS(NOW()) 2,查询昨天的数据 1 SELECT  ...

  7. mysql 查询常见时间段数据

    1.今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 2.昨天 SELECT * FROM 表名 WHERE TO_DAYS( NO ...

  8. java连接elasticsearch:查询、添加数据

    导入jar包 <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport --> <depe ...

  9. mysql 向字段添加数据或者删除数据

    UPDATE table SET cids = CONCAT(cids , ',12') where id=id //向字段添加数据 //因为要用逗号分隔 所以在在前面加了一个逗号 UPDATE ta ...

随机推荐

  1. poj 2612 Mine Sweeper

    Mine Sweeper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6429   Accepted: 2500 Desc ...

  2. Android与server通信的方法之中的一个(json)效率不高安全性不好

    http通信.以xml或者json为载体.相互通信数据. Android对于http的网络通信,提供了标准的java接口--httpURLConnection接口.以及apache的接口--httpc ...

  3. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  4. Python菜鸟晋级12----多线程

    Python 多线程 多线程类似于同一时候执行多个不同程序,多线程执行有例如以下长处: 使用线程能够把占领长时间的程序中的任务放到后台去处理. 用户界面能够更加吸引人.这样比方用户点击了一个butto ...

  5. js解析网络中的json数据

    <?php echo <<<_END <html> <head> </head> <body> <div id=" ...

  6. (五)api网关服务 zuul-路由

    路由是微服务架构中必须的一部分,比如,“/” 可能映射到你的WEB程序上,”/api/users “可能映射到你的用户服务上,“/api/shop”可能映射到你的商品服务商.(注解:我理解这里的这几个 ...

  7. Windows版Redis如何使用?(单机)

    使用Windows版Redis 1.下载Windows版本的Redis 2.在redis目录里创建redis.conf ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  8. H.264标准(一)mp4封装格式详解

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  9. sql server 去掉某字段左边的0

    比如:将以下Code字段 Code 000 001 002 ... 009 010 依次去掉左边的0,000替换为0,001替换为1,010替换为10,以此类推 Code 0 1 2 ... 9 10 ...

  10. Enable .Net 4.5 in IIS on Windows 8.1

    Setting up a new development box for myself I had forgotten all about the necessity to use theaspnet ...