美文网首页
MyBatis+mysql查询和添加数据

MyBatis+mysql查询和添加数据

作者: kingZXY2009 | 来源:发表于2017-03-29 22:28 被阅读21次

    项目结构:

    Menu

    packagecom.mstf.dao;

    importjava.util.Scanner;

    importorg.apache.ibatis.session.SqlSession;

    importcom.mstf.util.MyBatisUtil;

    publicclassMenu {

    privatestaticScanner sc =newScanner(System.in);

    publicstaticvoidmain(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();

    }elseif("2".equals(id)) {

    Table.table2();

    }else{

    System.err.println("非法操作:没有此选项!");

    session.close();

    }

    }

    }

    Table

    63packagecom.mstf.dao;

    importjava.util.List;

    importjava.util.Scanner;

    importorg.apache.ibatis.session.SqlSession;

    importcom.mstf.entity.T_customer;

    importcom.mstf.entity.T_order;

    importcom.mstf.util.MyBatisUtil;

    publicclassTable {

    privatestaticScanner sc =newScanner(System.in);

    staticSqlSession session=MyBatisUtil.getSession();

    /**

    * 根据订单 ID 查询

    * @author wangzheng

    */

    publicstaticvoidtable1(){

    System.err.println("请输入订单ID:");

    String bug=sc.next();

    try{

    List 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

    */

    publicstaticvoidtable2(){

    intcustomer_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 =newT_customer(0,name,age,tel);

    T_order t_order =newT_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

    47packagecom.mstf.entity;

    publicclassT_customer {

    privateintid;

    privateString name;

    privateString age;

    privateString tel;

    publicintgetId() {

    returnid;

    }

    publicvoidsetId(intid) {

    this.id = id;

    }

    publicString getName() {

    returnname;

    }

    publicvoidsetName(String name) {

    this.name = name;

    }

    publicString getAge() {

    returnage;

    }

    publicvoidsetAge(String age) {

    this.age = age;

    }

    publicString getTel() {

    returntel;

    }

    publicvoidsetTel(String tel) {

    this.tel = tel;

    }

    publicT_customer(intid, String name, String age, String tel) {

    super();

    this.id = id;

    this.name = name;

    this.age = age;

    this.tel = tel;

    }

    publicT_customer() {

    super();

    }

    @Override

    publicString toString() {

    return"订单客户:[客户ID:"+ id +", 姓名:"+ name +", 年龄:"+ age +", 电话:"+ tel +"]";

    }

    }

    T_order

    58packagecom.mstf.entity;

    publicclassT_order {

    privateintid;

    privateString order_number;

    privateString order_price;

    privateT_customer t_customer;

    privateintcustomer_id;

    publicintgetCustomer_id() {

    returncustomer_id;

    }

    publicvoidsetCustomer_id(intcustomer_id) {

    this.customer_id = customer_id;

    }

    publicT_customer getT_customer() {

    returnt_customer;

    }

    publicvoidsetT_customer(T_customer t_customer) {

    this.t_customer = t_customer;

    }

    publicintgetId() {

    returnid;

    }

    publicvoidsetId(intid) {

    this.id = id;

    }

    publicString getOrder_number() {

    returnorder_number;

    }

    publicvoidsetOrder_number(String order_number) {

    this.order_number = order_number;

    }

    publicString getOrder_price() {

    returnorder_price;

    }

    publicvoidsetOrder_price(String order_price) {

    this.order_price = order_price;

    }

    publicT_order(intid, String order_number, String order_price,intcustomer_id) {

    super();

    this.id = id;

    this.order_number = order_number;

    this.order_price = order_price;

    this.customer_id = customer_id;

    }

    publicT_order() {

    super();

    }

    @Override

    publicString toString() {

    return"系统订单:[订单ID:"+ id +", 订单号:"+ order_number +", 总价:"+ order_price

    +"];\n"+ t_customer +"\n";

    }

    }

    T_customer.xml

    PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    SELECT * FROM t_customer tc,t_order t WHERE tc.id=t.customer_id and t.customer_id =#{t.customer_id}

    T_order.xml

    PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    insert into t_customer (name, age, tel) values (#{name}, #{age}, #{tel})

    insert into t_order (order_number, order_price, customer_id) VALUES (#{order_number},#{order_price},(SELECT MAX(`id`) FROM `t_customer`))

    MyBatisUtil

    24packagecom.mstf.util;

    importjava.io.Reader;

    importorg.apache.ibatis.io.Resources;

    importorg.apache.ibatis.session.SqlSession;

    importorg.apache.ibatis.session.SqlSessionFactory;

    importorg.apache.ibatis.session.SqlSessionFactoryBuilder;

    publicclassMyBatisUtil {

    // 去读取编写的 mybatis_config.xml 文件加载文件中的映射

    publicstaticSqlSession getSession() {

    SqlSessionFactory ssf=null;

    String url="mybatis_config.xml";

    Reader reader=null;

    try{

    reader=Resources.getResourceAsReader(url);

    }catch(Exception e) {

    e.printStackTrace();

    }

    ssf=newSqlSessionFactoryBuilder().build(reader);

    returnssf.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

    PUBLIC"-//mybatis.org//DTD Config 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-config.dtd">

    欢迎加入技术QQ群:364595326

    相关文章

      网友评论

          本文标题:MyBatis+mysql查询和添加数据

          本文链接:https://www.haomeiwen.com/subject/tvsyottx.html