MyBatis+mysql查询和添加数据
项目结构:
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查询和添加数据的更多相关文章
- mysql查询当天的数据
mysql查询当天的数据 贴代码: #两个时间都使用to_days()函数 select * from reple where to_days(create_time) = to_days(NOW() ...
- mysql查询当天所有数据sql语句
mysql查询当天的所有信息: select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) a ...
- 【转】mysql查询当天所有数据sql语句
mysql查询当天的所有信息: select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) a ...
- php----处理从mysql查询返回的数据
使用php的mysql,向mysql查询,返回的是一个资源,有4个函数可以进行处理. 1.mysql_fetch_row() 2.mysql_fetch_assoc() 3.mysql_fetch_a ...
- (转载)MySQl数据库-批量添加数据的两种方法
方法一:使用excel表格 方法二:使用insert语句(FileWriter批量写入) 使用excel表格 1.打开数据表,按照表的字段在excel中添加数据.注意:表中字段名必须和excel中的名 ...
- MySQL - 查询今天的数据(以及昨天、本月、上个月、今年...) 查询Datetime 时间的数据
1,查询当天(今天)的数据 1 SELECT * FROM `order` WHERE TO_DAYS(order_time) = TO_DAYS(NOW()) 2,查询昨天的数据 1 SELECT ...
- mysql 查询常见时间段数据
1.今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 2.昨天 SELECT * FROM 表名 WHERE TO_DAYS( NO ...
- java连接elasticsearch:查询、添加数据
导入jar包 <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport --> <depe ...
- mysql 向字段添加数据或者删除数据
UPDATE table SET cids = CONCAT(cids , ',12') where id=id //向字段添加数据 //因为要用逗号分隔 所以在在前面加了一个逗号 UPDATE ta ...
随机推荐
- Mongodb简单介绍
1. 简单介绍 Mongodb是一种强大.灵活,可扩展的数据存储方式.属于nosql.非关系型数据库的一种. mongodb是面向文档的数据库. 尽管是非关系型数据库.可是它保留了很多关系型数据库的特 ...
- Android学习之——自己搭建Http框架(2)——框架扩展
· 本文主要解说的是Json指定转化成对象返回.下载进度更新,随时取消Request请求 一.Json指定转化成对象返回 上篇文章主要讲基础的框架搭建起来了,这次须要做一些些的扩展,这里Json转化用 ...
- 当我们谈论Erlang Maps时,我们谈论什么 Part 2
声明:本文讨论的Erlang Maps是基于17.0-rc2,时间2014-3-4.兴许Maps可能会出现语法或函数API上的有所调整,特此说明. 前情提要: [Erlang 0116] 当我们谈论E ...
- VC6.0 设置动态链接库工程生成dll以及lib文件的位置
在"Projet"->"Settings..."的"Link"选项卡中 "Output file name"中设置 ...
- Ubuntu14.04下Mongodb(在线安装方式|apt-get)安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 本博文介绍了MongoDB,并详细指引读者在Ubuntu下MongoDB的安装和使用.本教程在Ubuntu14.04下测试通过. 一.MongoDB介绍 MongoDB 是一个是 ...
- HD-ACM算法专攻系列(7)——Text Reverse
问题描述: 源码: /**/ #include"iostream" #include"string" using namespace std; void Pri ...
- 头像文件上传 方法一:from表单 方法二:ajax
方法一:from表单 html 设置form表单,内包含头像预览div,内包含上传文件input 设置iframe用来调用函数传参路径 <!--表单提交成功后不跳转处理页面,而是将处理数据返回给 ...
- cg基础
1. row-major or column-major 2.orthogonal matrices 3.why cameras always point along the world coordi ...
- QT笔记 -- (3) 为QLabel添加鼠标响应方法1
参考 http://qt-project.org/wiki/Make-a-QLabel-Clickable 1.首先重载QLabel的mousePressEvent,这样点击QLabel时就能发出cl ...
- 使用js获取url中的get参数并转成json格式
写在前面的 没啥说的 上代码 思路就是先获取到?后面的参数区,然后 利用字符串转数组方法获取到各个参数 var json = {}; var url = 'https://www.baidu.com/ ...