자바 프로그래머 좋은 튜토리얼 소개 MyBatis로 플러스를 공유 할 수

좋은 프로그래머가 자바 튜토리얼 공유 MyBatis로 플러스 소개 : 1.MyBatis 플러스 소개

  MyBatis로 플러스는 직원 개발의 MyBatis 향상 도구, 개발을 단순화 효율성과 건강을 증가시키기 만의 MyBatis를 기준으로 변경되지 강화하는 것입니다.

  

  MyBatis로 플러스 핵심 기능 : 일반적인 CRUD 코드 생성기 및 생성자의 조건에 대한 지원.

  

  일반 CRUD는 : 좋은 매퍼 인터페이스의 정의는 상속 할 필요가 BaseMapper <T> 인터페이스 구성 파일을 작성하지 않고 일반적인 CRUD 기능, 방법 및 인터페이스를 얻을 수 있습니다

  

  EntityWrapper하여 복잡한 SQL <T> (엔티티 래퍼 클래스), SQL 문을 접합하기 위해 사용될 수 있으며, 그룹화 쿼리를 정렬 지원 조건 빌더

2. 의존도를 추가

  <의존성>

                    <groupId>com.baomidou</groupId>

                    <artifactId>mybatis-plus</artifactId>

                    <version>2.3</version>

            </dependency>

3. 구성

<! - MP는 MybatisSqlSessionFactoryBean을 제공합니다 ->

    <bean id="sqlSessionFactoryBean"

            class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">

            <!-- 数据源 -->

            <property name="dataSource" ref="dataSource"/>

            <!-- 别名处理 -->

            <property name="typeAliasesPackage" value="com.qf.entity"/>

            <!-- 插件注册 -->

            <property name="plugins">

                    <list>

<! - 매김 플러그인을 등록 ->

                            <bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" />

                    </list>

            </property>

    </bean>

4.Dao 层

공용 인터페이스 IUserDao는 BaseMapper <사용자> {확장

}

엔티티 클래스

@데이터

@tablename (값 = "t_user")

공용 클래스 사용자 {

    @TableId(value="id",type=IdType.AUTO)

    private Integer id;

    @TableField(value="username")

    private String name;

    private Integer age;

    private String password;

    @TableField(exist=false)

    private Integer xxx;

}

6. 일반적인 주석

@TableField는 (= 거짓 존재는) : 속성이 데이터베이스 테이블 필드가 아니라 사용해야 함을 나타냅니다.

@TableField는 (진정한 = 존재) : 속성 데이터베이스 테이블 필드 나타냅니다.

@tablename : 데이터베이스 테이블

@TableId 표 기본 키 식별자

@TableField : 필드는 테이블을 식별

7. 시험 방법

  @테스트

    public void testMybatisPlus(){

            System.out.println("selectById:"+userDao.selectById(4)); // 根据Id查询

            System.out.println("selectList:"+userDao.selectList(null)); // 查询全部

            com.baomidou.mybatisplus.plugins.Page<User> page = new com.baomidou.mybatisplus.plugins.Page<>();

            List<User> list = userDao.selectPage(page, null); // 分页查询

            page.setRecords(list); // 把结果封装到分页对象中

            System.out.println(page.getCurrent());

            System.out.println(page.getPages());

            System.out.println(page.getSize());

            System.out.println(page.getTotal());

            System.out.println(page.getRecords());

            EntityWrapper<User> entityWrapper = new EntityWrapper<>();

            entityWrapper.eq("id", 4);

            entityWrapper.or().like("username", "3");

            List<User> selectList = userDao.selectList(entityWrapper); // 条件查询

            System.out.println("wrapper:"+selectList);

    }

추천

출처blog.51cto.com/14573321/2446419