Mybatis的注解开发

MyBatis的常用注解

@Insert:实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与@Result 一起使用,封装多个结果集

@One:实现一对一结果集封装

@Many:实现一对多结果集封装


简单使用

步骤一:创建实体类

package domain;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

public class UserBir {

    int id;
    String username;
    String password;
    BigInteger birthday;


    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public BigInteger getBirthday() {
        return birthday;
    }

    public void setBirthday(BigInteger birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "UserBir{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}


步骤二:编写映射接口(这一步是重点,将映射文件中的sql语句通过注解形式放在了这里)

package mapper;

import domain.UserBir;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {

    @Select("select * from UserBir")
    List<UserBir> findAll();


}


步骤三:在核心文件中扫描添加了注解的接口(有两种方式:可以扫描类或者扫描类所在的包)

<?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">


<!--数据源环境-->
<configuration>


    <!--    指定默认的环境名称-->
    <environments default="development">
        <!--        指定当前环境名称-->
        <environment id="development">
            <!--            事务管理器-->
            <transactionManager type="JDBC"></transactionManager>
            <!--            指定当前数据类型是连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/one"/>
                <property name="username" value="root"/>
                <property name="password" value="admin123"/>
            </dataSource>
        </environment>
    </environments>

    <!--    扫描接口-->
    <mappers>
        <!--扫描使用注解的类-->
        <mapper class="mapper.UserMapper"></mapper>
        <!--扫描使用注解的类所在的包-->
        <!--        <package name="mapper"/>-->
    </mappers>


</configuration>

测试

package test;

import domain.UserBir;
import 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 org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

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

public class MyTest {


    @Test
    public void test1() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<UserBir> list = userMapper.findAll();
        System.out.println(list);

        sqlSession.close();

    }


}

注解实现复杂映射开发


一对一查询

public interface UserMapper {

    @Select("select * from UserBir where id=${id}")
    List<UserBir> findAll();
}
public interface OrderMapper {

    @Select("select * from OrderInfo")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "ordertime", property = "ordertime"),
            @Result(column = "total", property = "total"),
            @Result(column = "uid", property = "uid"),
            @Result(
                    javaType = UserBir.class,//要封装的实体类型
                    property = "userBir",//要封装的属性名称
                    column = "uid",//根据这个字段去查询对应表中的数据
                    one = @One(select = "mapper.UserMapper.findAll")
            )
    })
    List<OrderInfo> findAll();
}

一对多查询

public interface OrderMapper {

    //    根据用户查询出对应的订单
    @Select("select * from OrderInfo where uid=#{uid}")
    List<OrderInfo> findByUSerId();


}

public interface UserMapper {
    //    查询所有用户与其对应的所有订单
    @Select("select * from UserBir")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "birthday", column = "birthday"),
            @Result(
                    javaType = List.class,
                    property = "orderInfoList",
                    column = "id",
                    many = @Many(select = "mapper.OrderMapper.findByUSerId")
            )

    })
     List<UserBir> findUserAndOrder();


}

posted @ 2021-02-24 21:56  西红柿里没有番茄  阅读(59)  评论(0编辑  收藏  举报