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 ...
随机推荐
- Codeforces 13C Sequence dp
题目链接:http://codeforces.com/problemset/problem/13/C 题意: 给定n长的序列 每次操作能够给每一个数++或-- 问最少须要几步操作使得序列变为非递减序列 ...
- 2015多校联合训练赛hdu 5301 Buildings 2015 Multi-University Training Contest 2 简单题
Buildings Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- Vuejs2.0学习之二(Render函数,createElement,vm.$slots,函数化组件,模板编译,JSX)
时隔一周多,因为一些别的事情绊住了,下面接着写.中间这段时间也有看官方文档,发现正如他所说90%的基础内容都一样,所以这里直接跳到我比较关注的东东上,要是想看看哪些不一样,可以参考这个http://v ...
- hdu_4707
算是水题一道吧,我也没有建树,看别人又用vector,又用bfs,dfs的,对vector不熟,所以就模拟了一下 #include<iostream> #include<string ...
- vue平行组件传值 params
欢迎加入前端交流群交流知识&&获取视频资料:749539640 需要传值的组件组件: <template> <div> <router-link :to= ...
- [luogu P1776] 宝物筛选 解题报告(单调队列优化DP)
题目链接: https://www.luogu.org/problemnew/show/P1776 题目: 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF ...
- hbase的优化(全)
高可用 在HBase中Hmaster负责监控RegionServer的生命周期,均衡RegionServer的负载,如果Hmaster挂掉了,那么整个HBase集群将陷入不健康的状态,并且此时的工作状 ...
- (转载)10个实用的但偏执的Java编程技术
10个实用的但偏执的Java编程技术 在沉浸于编码一段时间以后(比如说我已经投入近20年左右的时间在程序上了),你会渐渐对这些东西习以为常.因为,你知道的…… 作者:小峰来源:码农网|2015-09- ...
- php 生成 guid
function guid( $opt = true ){ // Set to true/false as your default way to do this. if( function_exis ...
- 常见Json字符串反序列化处理方式总结
常用来处理Json字符串序列化 反序列化组件:Newtonsoft.Json (https://www.newtonsoft.com/json) 参考资料https://www.cnblogs.com ...