其他分享
首页 > 其他分享> > MyBatis(五天掌握之第二天)

MyBatis(五天掌握之第二天)

作者:互联网

1.核心配置文件详解

1.1 properties

1.2 settings

1.3 mappers

2 API详解

2.1 输入:vo条件

package com.czxy.ssm.vo;

/**
 * @author LiReign
 */
public class UserVo {
    private String beginTime;
    private String endTime;
    public String getBeginTime() {
        return beginTime;
    }
    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }
    public String getEndTime() {
        return endTime;
    }
    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
}
    /**
     * 条件查询
     * @param userVo
     * @return
     */
    @Select("SELECT * FROM `user` WHERE birthday >= #{beginTime} AND birthday <= #{endTime}")
    public List<User> condition(UserVo userVo);
package com.czxy.ssm;

import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
import com.czxy.ssm.utils.MyBatisUtils;
import com.czxy.ssm.vo.UserVo;

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

/**
* @author LiReign
*/
public class Test06_Condition {
   public static void main(String[] args) throws IOException {

       //1 通过会话获得功能接口
       UserMapper userMapper = MyBatisUtils.getMapper(UserMapper.class);
       //2 更新
       UserVo userVo = new UserVo();
       userVo.setBeginTime("2010");
       userVo.setEndTime("2020");
       List<User> list = userMapper.condition(userVo);
       // 打印结果
       list.forEach(System.out::println);
       //3 提交并关闭
       MyBatisUtils.commitAndclose();
   }
}

2.2 输出:结果集映射

    @Select("select * from user")
    @Results({
            @Result(column = "user_name",property = "userName"),
            @Result(column = "password", property = "password")
    })
    public List<User> selectAll();

3. 多表操作:一对多

3.1 JavaBean

3.1.1 编写JavaBean:User

public class User {
    private String uid;
    private String userName;
    private String password;
    private String name;
    private String email;
    private Date birthday;
    private String sex;
    private Integer state;
    private String code;

3.1.2 编写JavaBean:Order

public class Order {
    private String oid; 		// 订单编号
    private Date ordertime; 	// 下单时间
    private Double total; 		// 订单总金额
    private Integer state; 		// 订单状态 0 未支付 1 已支付 2已发货 3已收货
    private String address;		// 收货人地址
    private String name;		// 收货人姓名
    private String telephone;	// 收货人电话
    private String uid;

3.1.3 编写对应关系

public class User {
    // 一对多关系:一个用户拥有多个订单
    private List<Order> orderList = new ArrayList<>()

3.2 编写各自Mapper

3.2.1 编写UserMapper

package com.czxy.ssm.mapper;

import com.czxy.ssm.domain.User;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author LiReign
 */
public interface UserMapper {

    /**
     * 查询所有
     * @return
     */
    @Select("select * from user")
    @Results(id="userResultMap", value={
            @Result(column = "user_name",property = "userName"),
            @Result(column = "password", property = "password")
    })
    public List<User> selectAll();
}

3.2.2 编写OrderMapper

package com.czxy.ssm.mapper;

import com.czxy.ssm.domain.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author LiReign
 */
public interface OrderMapper {

    /**
     * 查询指定用户的所有订单
     * @param uid
     * @return
     */
    @Select("SELECT * FROM orders WHERE uid = #{uid}")
    public List<Order> selectAllByUid(@Param("uid") String uid);
}

3.3 测试类

3.3.1 user测试

package com.czxy.ssm;

import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author LiReign
 */
public class Test01_SelectAll {
    public static void main(String[] args) throws IOException {
        //1 加载配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2 获得工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3 通过工程获得会话(连接)
        SqlSession sqlSession = factory.openSession();
        //4 获得功能接口
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //5 进行查询
        List<User> list = userMapper.selectAll();
        //6 处理数据
        for(User user: list) {
            System.out.println(user);
        }
        //7 释放资源
        sqlSession.close();
    }
}

3.3.2 order测试

package com.czxy.ssm;

import com.czxy.ssm.domain.Order;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.OrderMapper;
import com.czxy.ssm.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author LiReign
 */
public class Test02_SelectAllOrderByUId {
    public static void main(String[] args) throws IOException {
        //1 加载配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2 获得工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3 通过工程获得会话(连接)
        SqlSession sqlSession = factory.openSession();
        //4 获得功能接口
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        //5 进行查询
        List<Order> list = orderMapper.selectAllByUid("u001");
        //6 处理数据
        list.forEach(System.out::println);
        //7 释放资源
        sqlSession.close();
    }
}

3.4 映射关系

在这里插入图片描述

package com.czxy.ssm.mapper;

import com.czxy.ssm.domain.User;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author LiReign
 */
public interface UserMapper {

    /**
     * 查询所有
     * @return
     */
    @Select("select * from user")
    @Results(id="userResultMap", value={
            @Result(property = "uid", column = "uid"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "password", column = "password"),
            @Result(property = "orderList",column = "uid", many=@Many(select = "com.czxy.ssm.mapper.OrderMapper.selectAllByUid"))
    })
    public List<User> selectAll();
}

4. 多表操作:多对一

4.1 分析

4.2 OrderMapper 详情

public interface OrderMapper {

    /**
     * 查询详情
     * @param oid
     * @return
     */
    @Select("select * from orders where oid = #{oid}")
    public Order selectById(@Param("oid") String oid);
}

4.3 测试类 order详情

package com.czxy.ssm;

import com.czxy.ssm.domain.Order;
import com.czxy.ssm.mapper.OrderMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author LiReign
 */
public class Test03_SelectOrderByOId {
    public static void main(String[] args) throws IOException {
        //1 加载配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2 获得工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3 通过工程获得会话(连接)
        SqlSession sqlSession = factory.openSession();
        //4 获得功能接口
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        //5 进行查询
        Order order = orderMapper.selectById("x001");
        //6 处理数据
        System.out.println(order);
        //7 释放资源
        sqlSession.close();
    }
}

4.4 修改Order

public class Order {​    // 多对一:多个订单属于一个用户    private User user;

4.5 UserMapper 详情

public interface UserMapper {

    /**
     * 通过uid查询详情
     * @param uid
     * @return
     */
    @Select("select * from user where uid = #{uid}")
    public User selectById(@Param("uid") String uid);
}

4.6 修改OrderMapper,建立关联

package com.czxy.ssm.mapper;

import com.czxy.ssm.domain.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author LiReign
 */
public interface OrderMapper {

    /**
     * 查询详情
     * @param oid
     * @return
     */
    @Select("select * from orders where oid = #{oid}")
    @Results({
            @Result(property = "user", column = "uid" , one = @One(select="com.czxy.ssm.mapper.UserMapper.selectById"))
    })
    public Order selectById(@Param("oid") String oid);
}

标签:String,ssm,第二天,五天,MyBatis,import,com,public,czxy
来源: https://blog.csdn.net/LiReign/article/details/120610601