• mybatis


    mybaits是什么东西昵?

    在学习Java中一般与springMVC、struts2+spring+mybatis联合开发

    以下是mybaits的一个查询方式

    接口

    package cn.jcet.mapper;
    
    import java.util.List;
    
    import cn.jcet.domian.Orders;
    import cn.jcet.domian.User;
    
    public interface OrdersMapper {
    
        //查询商品订单
        public List<Orders> findByAll();
        
        //一对一查询
        public List<Orders> selectOrders();
    
        //多对多关系查询
        public List<User> selectUser();
    
    }

    mybatis的xml配置sql文件

    <?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="cn.jcet.mapper.OrdersMapper">
    	
    	<!-- 手动封装实体对应id -->
    	<resultMap type="Orders" id="orders">
    		<result column="user_id" property="userId"/>
    	</resultMap>
    	<!-- 
    	查询全部商品订单 -->
    	<select id="findByAll" resultMap="orders" >
    		select id,user_id,number,createtime,note from orders;
    	</select>
    	
    	<!-- //一对一查询
    	public List<Orders> selectOrders();
    	手动封装实体
    	 -->
    	 <resultMap type="Orders" id="order">
    		<result column="id" property="id"/>
    		<result column="user_id" property="userId"/>
    		<result column="number" property="number"/> 
    		<!-- 一对一关系映射  -->
    	 	<association property="user" javaType="User">
    			<id column="user_id" property="id"/>
    			<result column="username" property="username"/>
    		</association> 
    	</resultMap>
    	 <select id="selectOrders" resultMap="order">
    	 SELECT 
    	 o.id,
    	 o.user_id,
    	 o.number,
    	 o.createtime,
    	 u.username
    	 FROM orders o 
    	 LEFT JOIN USER u ON 
    	 o.user_id=u.id
    	</select>
    	
    	<!-- 多对多关系映射 
    	public List<User> selectUser();
    	-->
    	<resultMap type="User" id="user">
    		<id column="id" property="id"/>
    		<result column="username" property="username"/>
    		<!-- 多对多 存储集合 -->
    		<collection property="orders" ofType="Orders">
    			<id column="user_id" property="id"/>
    			<result column="number" property="number"/>
    		</collection>
    	</resultMap>
    	<select id="selectUser" resultMap="user">
    		 SELECT 
    			 o.id,
    			 o.user_id,
    			 o.number,
    			 o.createtime,
    			 u.username
    			 FROM  USER u
    			 LEFT JOIN  orders o
    			 ON o.user_id=u.id
    	</select>
    	
    	
    	
    		
    </mapper>
    

      

    测试mybatis查询语句

    package cn.jcet.mybaits;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    
    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 cn.jcet.domian.Orders;
    import cn.jcet.domian.User;
    import cn.jcet.mapper.OrdersMapper;
    
    public class OrdersMapperTest {
    
        
        
        @Test
        public void demo() throws IOException {
            String resources = "sqlMapConfig.xml";
            //创建工厂
            InputStream in = Resources.getResourceAsStream(resources);
            
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
            
            SqlSession openSession = sessionFactory.openSession();
            
            OrdersMapper mapper = openSession.getMapper(OrdersMapper.class);
            
            List<Orders> list = mapper.findByAll();
            for (Orders orders : list) {
                System.out.println(orders);
            }
        }
        
        
        
        @Test
        //一对一测试
        public void demo2() throws IOException {
            String resources = "sqlMapConfig.xml";
            //创建工厂
            InputStream in = Resources.getResourceAsStream(resources);
            
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
            
            SqlSession openSession = sessionFactory.openSession();
            
            OrdersMapper mapper = openSession.getMapper(OrdersMapper.class);
            
            List<Orders> list = mapper.selectOrders();
            
            for (Orders orders : list) {
                System.out.println(orders);
            }
        }
        
        @Test
        //多对多测试
        public void demo3() throws IOException {
            String resources = "sqlMapConfig.xml";
            //创建工厂
            InputStream in = Resources.getResourceAsStream(resources);
            
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
            
            SqlSession openSession = sessionFactory.openSession();
            
            OrdersMapper mapper = openSession.getMapper(OrdersMapper.class);
            
            List<User> selectUser = mapper.selectUser();
            for (User user : selectUser) {
                System.out.println(user);
            }
        }
    }
  • 相关阅读:
    HTTP/2的优先级
    JavaScript 日期权威指南
    岂曰无衣与子同袍
    Android项目中实现native调用
    关键渲染路径
    @ModelAttribute使用详解
    @SessionAttribute使用详解
    @ControllerAdvice 拦截异常并统一处理
    js获取文件MD5值
    Mybatis分页插件PageHelper的配置和使用方法
  • 原文地址:https://www.cnblogs.com/yuyang123/p/9674554.html
Copyright © 2020-2023  润新知