Mybatis

目录

一、配置依赖文件

二、配置log4j.properties文件

三、配置SqlMapConfig.xml文件

四、配置Mapper.xml文件

五、增删改查

 六、动态代理

七、输入映射

八、输出映射

九、动态SQL

十、多对一

十一、一对多

十二、多对多

十三、自查讯


一、配置依赖文件



    4.0.0

    com.iotek
    fristMybatis
    1.0-SNAPSHOT

    jar

    

        
        
        
            org.mybatis
            mybatis
            3.5.7
        

        
        
        
            junit
            junit
            4.12
            test
        

        
        
        
            mysql
            mysql-connector-java
            8.0.30
        

        
        
        
            log4j
            log4j
            1.2.17
        
    

    
        8
        8
    

二、配置log4j.properties文件

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

三、配置SqlMapConfig.xml文件




    
    
        
            
            
            
            
                
                
                
                
            
        
    
    
        
    

四、配置Mapper.xml文件





    
    
package com.iotek.pojo;

/**
 * @Author:
 * @CreateTime: 2023-02-05  13:25
 */
public class Goods {
    private int goods_id;
    private String goods_name;
    private double goods_price;

    public Goods() {
        super();
    }

    public Goods(int goods_id, String goods_name, double goods_price) {
        this.goods_id = goods_id;
        this.goods_name = goods_name;
        this.goods_price = goods_price;
    }

    public int getGoods_id() {
        return goods_id;
    }

    public void setGoods_id(int goods_id) {
        this.goods_id = goods_id;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public double getGoods_price() {
        return goods_price;
    }

    public void setGoods_price(double goods_price) {
        this.goods_price = goods_price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "goods_id=" + goods_id +
                ", goods_name='" + goods_name + '\'' +
                ", goods_price=" + goods_price +
                '}';
    }
}
package com.iotek.mybatis.test;

import com.iotek.pojo.Goods;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author:
 * @CreateTime: 2023-02-05  13:38
 */
public class MybatisTest {

    /**
     * @description:查询一条数据
     * @param
     * @return void
     * @time: 2023/2/5 13:53
     **/
    @Test
    public void test1() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //查询一条数据 com.iotek.selectGoodsByGoodsID命名空间
        Goods goods= session.selectOne("com.iotek.selectGoodsByGoodsID");
        System.out.println(goods);
    }
}

五、增删改查






    
    
    
    
    
    
        INSERT INTO t_goods(goods_name,goods_price) VALUES(#{goods_name},#{goods_price})
    

    
    
        DELETE FROM t_goods WHERE goods_id=#{id}
    
    
    
        UPDATE t_goods SET goods_name=#{goods_name},goods_price=#{goods_price} WHERE goods_id=#{goods_id}
    

    
    
    
package com.iotek.mybatis.test;

import com.iotek.pojo.Goods;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-02-05  13:38
 */
public class MybatisTest {
    /**
     * @description:查询一条数据
     * @param
     * @return void
     * @time: 2023/2/5 13:53
     **/
    @Test
    public void test1() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //查询一条数据 com.iotek.selectGoodsByGoodsID命名空间
        Goods goods= session.selectOne("com.iotek.selectGoodsByGoodsID",10);
        System.out.println(goods);
    }
    /**
     * @description:插入一条数据
     * @param
     * @return void
     * @time: 2023/2/5 15:09
     **/
    @Test
    public void test2() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //查询一条数据 com.iotek.selectGoodsByGoodsID命名空间
        Goods goods = new Goods(0,"宏碁/ACER",29577.28);
        //返回值影响行数
        int insert = session.insert("com.iotek.insertOneGoods", goods);
        //事务提交 内从中提交到磁盘
        session.commit();
        System.out.println("插入了"+insert+"条数据");
    }
    /**
     * @description:删除了一条数据
     * @param
     * @return void
     * @time: 2023/2/5 20:16
     **/
    @Test
    public void test3() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //返回值影响行数
        int delete = session.delete("com.iotek.deleteOneGoods", 21);
        //事务提交 内从中提交到磁盘
        session.commit();
        System.out.println("删除了"+delete+"条数据");
    }
    /**
     * @description:修改了一条数据
     * @param
     * @return void
     * @time: 2023/2/5 20:32
     **/
    @Test
    public void test4() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //返回值影响行数
        Goods goods = new Goods(22,"微星/MSI",39577.28);
        int delete = session.update("com.iotek.updateOneGoods", goods);
        //事务提交 内从中提交到磁盘
        session.commit();
        System.out.println("修改了"+delete+"条数据");
    }

    /**
     * @description:查询集合
     * @param
     * @return void
     * @time: 2023/2/5 20:44
     **/
    @Test
    public void test5() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //返回集合
        List goodsLis = session.selectList("com.iotek.selectGoodsLikeGoodsName","华");
        //事务提交 内从中提交到磁盘
        System.out.println(goodsLis);
    }
}

 六、动态代理






    
    
        INSERT INTO t_user(userName,userAge) VALUES(#{userName},#{userAge})
    

    

    
    
package com.iotek.mybatis.DaoTest;

import com.iotek.Dao.UserMapper;
import com.iotek.pojo.User;
import com.iotek.pojo.UserAgeCondition;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-02-05  23:06
 */
public class UserDaoTest {
    private SqlSessionFactory sqlSessionFactory;
    //Before在运行Test之前执行
    @Before
    public void getSessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }
    /**
     * @description:添加一条数据
     * @param
     * @return void
     * @time: 2023/2/6 12:00
     **/
    @Test
    public void addUser(){
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);//代理对象
        User user = new User(0,"lucy",18);
        userMapper.addUser(user);
        session.commit();
    }
    /**
     * @description:姓名模糊查询
     * @param
     * @return void
     * @time: 2023/2/6 14:38
     **/
    @Test
    public void queryUserByUserName(){
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);//代理对象
        List user = userMapper.queryUserByUserName("思");
        System.out.println(user);
    }
    /**
     * @description:查询年龄20到50岁之间的
     * @param
     * @return void
     * @time: 2023/2/6 14:15
     **/
    @Test
    public void queryUserByUserAgeCondition(){
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);//代理对象
        UserAgeCondition uc = new UserAgeCondition();
        uc.setMaxAge(50);
        uc.setMinAge(20);
        User user = new User();
        user.setUserAgeCondition(uc);
        List users = userMapper.queryUserByUserAgeCondition(user);
        System.out.println(users);
    }
}

七、输入映射

package com.iotek.Dao;

import com.iotek.pojo.Goods;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2023-02-06  12:41
 */
public interface GoodsMapper {
    //添加一条数据
    public void addGoods(Goods goods);
    //删除一条数据
    public void deleteGoodsByGoodsId(int goodsID);
    //修改一条数据
    public void updateGoodsByGoodsId(Goods goods);
    //根据ID查询
    public Goods queryGoodsByGoodsID(int goodsId);
    //根据名称查询
    public List queryGoodsByGoodsName(String goodsName);
    //根据ID和名称查询  传入多个参数默认放入Map键值对中 map key value --> key=param1 value=goodsID;key=param2 value=goodsName 参数按顺序
    public Goods queryGoodsByGoodsIDAndGoodsName(int goodsID,String goodsName);
    //根据ID和名称查询 @Param注解方式
    public Goods queryGoodsByGoodsIDAndGoodsName2(@Param("goodsID") int goodsID, @Param("goodsName") String goodsName);
    //根据ID和名称查询 一个用param一个用@Param注解方式
    public Goods queryGoodsByGoodsIDAndGoodsName3(@Param("goodsID") int goodsID, String goodsName);
    //根据ID和名称查询 传入Map
    public Goods queryGoodsByMap(Map map);
    //根据ID和名称查询 传入List集合
    public Goods queryGoodsByIDAndList(int goodsID,List goods);
}




    
        INSERT INTO t_goods(goods_name,goods_price) VALUES (#{goods_name},#{goods_price});
    

    
        DELETE FROM t_goods WHERE goods_id = #{goods_id}
    

    
        UPDATE t_goods SET goods_name=#{goods_name},goods_price=#{goods_price} WHERE goods_id = #{goods_id}
    

    

    

    
    

    

    

    
    

    
    
package com.iotek.mybatis.DaoTest;

import com.iotek.Dao.GoodsMapper;
import com.iotek.pojo.Goods;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author:
 * @CreateTime: 2023-02-06  12:49
 */
public class GoodsDaoTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getSqlSessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }
    /**
     * @description:添加一条记录
     * @param
     * @return void
     * @time: 2023/2/6 16:06
     **/
    @Test
    public void addGoods(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = new Goods(0,"戴尔/DELL",45633.2);
        goodsMapper.addGoods(goods);
        session.commit();
    }
    /**
     * @description:根据ID删除
     * @param
     * @return void
     * @time: 2023/2/6 16:06
     **/
    @Test
    public void deleteGoodsByGoodsId(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        goodsMapper.deleteGoodsByGoodsId(22);
        session.commit();
    }
    /**
     * @description:根据ID修改
     * @param
     * @return void
     * @time: 2023/2/6 16:07
     **/
    @Test
    public void updateGoodsByGoodsId(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = new Goods(24,"华为/HUAWEI",85416.37);
        goodsMapper.updateGoodsByGoodsId(goods);
        session.commit();
    }
    /**
     * @description:根据ID查询
     * @param
     * @return void
     * @time: 2023/2/6 16:07
     **/
    @Test
    public void queryGoodsByGoodsID(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsID(10);
        System.out.println(goods);
    }
    /**
     * @description:根据名称查询
     * @param
     * @return void
     * @time: 2023/2/6 16:07
     **/
    @Test
    public void queryGoodsByGoodsName(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        List goodsList = goodsMapper.queryGoodsByGoodsName("华");
        System.out.println(goodsList);
    }
    /**
     * @description:根据ID和名称查询
     * @param
     * @return void
     * @time: 2023/2/6 16:08
     **/
    @Test
    public void queryGoodsByGoodsIDAndGoodsName(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsIDAndGoodsName(10,"微星/MSI");
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByGoodsIDAndGoodsName2(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsIDAndGoodsName2(10,"微星/MSI");
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByGoodsIDAndGoodsName3(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsIDAndGoodsName3(10,"微星/MSI");
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByMap(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Map map = new HashMap<>();
        map.put("goodsID",10);
        map.put("goodsName","微星/MSI");
        Goods goods = goodsMapper.queryGoodsByMap(map);
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByIDAndList(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = new Goods();
        goods.setGoods_name("微星/MSI");
        List goodsList = new ArrayList<>();
        goodsList.add(goods);
        Goods gd = goodsMapper.queryGoodsByIDAndList(10,goodsList);
        System.out.println(gd);
    }
}

八、输出映射





    

    

    
    

    
    
    
        
        

    
    
    

九、动态SQL




    
    
    

    
    

    
    

    
    

    
    

    

    
    
        SELECT * FROM t_goods
    
package com.iotek.DaoTest;

import com.iotek.Dao.AnimalDao;
import com.iotek.Dao.GoodsDao;
import com.iotek.pojo.Animal;
import com.iotek.pojo.Goods;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-02-13  14:01
 */
public class DynSqlTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void queryGoodsByIf(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods goods = new Goods();
        goods.setGoods_name("华为");//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%"
        //goods.setGoods_name("");//SELECT * FROM t_goods
        goods.setGoods_price(80000);//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%" AND goods_price>?
        List list = goodsDao.queryGoodsByIf(goods);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByWhere(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods goods = new Goods();
        goods.setGoods_name("华为");//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%"
        //goods.setGoods_name("");//SELECT * FROM t_goods
        goods.setGoods_price(80000);//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%" AND goods_price>?
        List list = goodsDao.queryGoodsByWhere(goods);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByChoose(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods goods = new Goods();
        goods.setGoods_name("华为");//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%"
        //goods.setGoods_name("");//SELECT * FROM t_goods
        goods.setGoods_price(80000);//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%" AND goods_price>?
        List list = goodsDao.queryGoodsByChoose(goods);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByForeach1(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        int ids[] = {1,20,4};
        List list = goodsDao.queryGoodsByForeach1(ids);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByForeach2(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        List list = new ArrayList<>();
        list.add(1);
        list.add(20);
        list.add(14);
        List goodsList = goodsDao.queryGoodsByForeach2(list);
        System.out.println(goodsList);
    }
    @Test
    public void queryGoodsByForeach3(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods g1= new Goods();
        g1.setGoods_id(1);
        Goods g2= new Goods();
        g2.setGoods_id(20);
        Goods g3= new Goods();
        g3.setGoods_id(14);
        List goods = new ArrayList<>();
        goods.add(g1);
        goods.add(g2);
        goods.add(g3);
        List goodsList = goodsDao.queryGoodsByForeach3(goods);
        System.out.println(goodsList);
    }
}

十、多对一

package com.iotek.pojo;

/**
 * @Author:
 * @CreateTime: 2023-02-13  15:49
 */
public class Order {
    private Integer id;
    private double orderNumer;
    private double totalamount;
    private String orderTime;
    private GoodsTwo goodsTwo;

    public Order() {
        super();
    }

    public Order(Integer id, double orderNumer, double totalamount, String orderTime, GoodsTwo goodsTwo) {
        this.id = id;
        this.orderNumer = orderNumer;
        this.totalamount = totalamount;
        this.orderTime = orderTime;
        this.goodsTwo = goodsTwo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public double getOrderNumer() {
        return orderNumer;
    }

    public void setOrderNumer(double orderNumer) {
        this.orderNumer = orderNumer;
    }

    public double getTotalamount() {
        return totalamount;
    }

    public void setTotalamount(double totalamount) {
        this.totalamount = totalamount;
    }

    public String getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }

    public GoodsTwo getGoodsTwo() {
        return goodsTwo;
    }

    public void setGoodsTwo(GoodsTwo goodsTwo) {
        this.goodsTwo = goodsTwo;
    }

    @Override
    public String toString() {
        return "order{" +
                "id=" + id +
                ", orderNumer=" + orderNumer +
                ", totalamount=" + totalamount +
                ", orderTime='" + orderTime + '\'' +
                ", goodsTwo=" + goodsTwo +
                '}';
    }
}
package com.iotek.pojo;

import java.util.Set;

/**
 * @Author:一的一段维护端
 * @CreateTime: 2023-02-13  15:47
 */
public class GoodsTwo {
    private Integer gid;
    private String goodsname;
    private double goodsprice;
    private Set orders;//关联关系

    public GoodsTwo() {
        super();
    }

    public GoodsTwo(Integer gid, String goodsname, double goodsprice, Set orders) {
        this.gid = gid;
        this.goodsname = goodsname;
        this.goodsprice = goodsprice;
        this.orders = orders;
    }

    public Integer getGid() {
        return gid;
    }

    public void setGid(Integer gid) {
        this.gid = gid;
    }

    public String getGoodsname() {
        return goodsname;
    }

    public void setGoodsname(String goodsname) {
        this.goodsname = goodsname;
    }

    public double getGoodsprice() {
        return goodsprice;
    }

    public void setGoodsprice(double goodsprice) {
        this.goodsprice = goodsprice;
    }

    public Set getOrders() {
        return orders;
    }

    public void setOrders(Set orders) {
        this.orders = orders;
    }

    @Override
    public String toString() {
        return "GoodsTwo{" +
                "gid=" + gid +
                ", goodsname='" + goodsname + '\'' +
                ", goodsprice=" + goodsprice +
                ", orders=" + orders +
                '}';
    }
}



    
    
    
        
        
        
        
        
            
            
            
        
    
    

    
    
    
        
        
        
        
        

        
    
    
package com.iotek.DaoTest;

import com.iotek.Dao.GoodsTwoDao;
import com.iotek.Dao.OrderDao;
import com.iotek.pojo.Goods;
import com.iotek.pojo.GoodsTwo;
import com.iotek.pojo.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-02-13  16:22
 */
public class OneToMany {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void ManyToOne1(){
        SqlSession session = sqlSessionFactory.openSession();
        OrderDao orderDao = session.getMapper(OrderDao.class);
        Order order = orderDao.getOrderByIDManyToOne1(1);
        System.out.println(order);
    }
    @Test
    public void ManyToOne2(){
        SqlSession session = sqlSessionFactory.openSession();
        OrderDao orderDao = session.getMapper(OrderDao.class);
        Order order = orderDao.getOrderByIDManyToOne2(4);
        System.out.println(order);
    }
}

十一、一对多




    
    
    
        
        
        
        
            
            
            
            
        
    
    

    
    
    
        
        
        
        

        
    
    
package com.iotek.DaoTest;

import com.iotek.Dao.GoodsTwoDao;
import com.iotek.pojo.GoodsTwo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author:
 * @CreateTime: 2023-02-13  16:22
 */
public class ManyToOne {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void OneToMany1(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsTwoDao goodsTwoDao = session.getMapper(GoodsTwoDao.class);
        GoodsTwo goodsByID = goodsTwoDao.getGoodsByIDOneToMany1(1);
        System.out.println(goodsByID);
    }
    @Test
    public void OneToMany2(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsTwoDao goodsTwoDao = session.getMapper(GoodsTwoDao.class);
        GoodsTwo goodsByID = goodsTwoDao.getGoodsByIDOneToMany2(2);
        System.out.println(goodsByID);
    }
}

十二、多对多

package com.iotek.pojo;

import java.util.Set;

/**
 * @Author:
 * @CreateTime: 2023-02-13  23:32
 */
public class Student {
    private Integer sid;
    private String sname;
    private Set courses;

    public Student() {
        super();
    }

    public Student(Integer sid, String sname, Set courses) {
        this.sid = sid;
        this.sname = sname;
        this.courses = courses;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Set getCourses() {
        return courses;
    }

    public void setCourses(Set courses) {
        this.courses = courses;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", courses=" + courses +
                '}';
    }
}
package com.iotek.pojo;

/**
 * @Author:
 * @CreateTime: 2023-02-13  23:28
 */
public class Course {
    private Integer cid;
    private String cname;

    public Course() {
        super();
    }

    public Course(Integer cid, String cname) {
        this.cid = cid;
        this.cname = cname;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }
}



    
    
        
        
        
            
            
        
    
    
package com.iotek.DaoTest;

import com.iotek.Dao.OrderDao;
import com.iotek.Dao.StudentDao;
import com.iotek.pojo.Order;
import com.iotek.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author:
 * @CreateTime: 2023-02-13  16:22
 */
public class MangToMany {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void ManytoMany1(){
        SqlSession session = sqlSessionFactory.openSession();
        StudentDao studentDao = session.getMapper(StudentDao.class);
        Student student = studentDao.getStudentByIdManyToMany1(1);
        System.out.println(student);
    }
}

十三、自查讯

package com.iotek.pojo;

import java.util.Set;

/**
 * @Author:
 * @CreateTime: 2023-02-13  23:59
 */
public class Newlable {
    private int id;
    private String name;
    private Set newlables;//栏目下所有的子栏目

    public Newlable() {
        super();
    }

    public Newlable(int id, String name, Set newlables) {
        this.id = id;
        this.name = name;
        this.newlables = newlables;
    }

    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 Set getNewlables() {
        return newlables;
    }

    public void setNewlables(Set newlables) {
        this.newlables = newlables;
    }

    @Override
    public String toString() {
        return "Newlable{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", newlables=" + newlables +
                '}';
    }
}
package com.iotek.Dao;

import com.iotek.pojo.Newlable;

import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-02-14  00:02
 */
public interface NewlableDao {
    public List getChildrenByPid1(int pid);
    public List getChildrenByPid2(int pid);
}



    
    
        
        
        
    
    


    
    
        
        
        
    
    

package com.iotek.DaoTest;

import com.iotek.Dao.NewlableDao;
import com.iotek.Dao.OrderDao;
import com.iotek.pojo.Newlable;
import com.iotek.pojo.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-02-14  00:17
 */
public class SelectOwn {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void ManyToOne1(){
        SqlSession session = sqlSessionFactory.openSession();
        NewlableDao newlableDao = session.getMapper(NewlableDao.class);
        List newlableList = newlableDao.getChildrenByPid1(2);
        System.out.println(newlableList);
    }
    @Test
    public void ManyToOne2(){
        SqlSession session = sqlSessionFactory.openSession();
        NewlableDao newlableDao = session.getMapper(NewlableDao.class);
        List newlableList = newlableDao.getChildrenByPid2(2);
        System.out.println(newlableList);
    }
}

你可能感兴趣的:(框架,mybatis,java,mysql)