Mybatis新手入门使用

1.先引入pom文件

<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
    </dependencies>
    <!--这是方便后面编译时可以带着xml文件-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

2.编写实体类domain.student,定义好字段与getter,setter

package com.bjpowernode.domain;


public class Student {
    private Integer id;
    private String phone;
    private String truename;

    public Integer getId() {
        return id;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

3.定义接口文件dao.studentDao

package com.bjpowernode.dao;

import com.bjpowernode.domain.Student;

import java.util.List;

public interface StudentDao {

    public List<Student> selectStudents();
}

4.定义 dao.studentDao.xml的mapper文件

注意: namespace必须是对应接口文件的全限定名称,resulttype必须是对应的实体类,不然报错

<?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="com.bjpowernode.dao.StudentDao">
    <select id="selectStudents" resultType="com.bjpowernode.domain.Student">
        select id,phone,truename from user
    </select>
</mapper>

5.定义resources下的mybatis配置文件

<?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>
    //加这个可以打印日志
  <settings>
  <setting name="logImpl" value="STDOUT_LOGGING"/>
  </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/daikuan?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
    </mappers>
</configuration>

6.编写功能测试类Myapp

<select id="pageList" resultType="com.jjccb.mall.modules.privilege.entity.ImsActivityPrivilegePrize">
        SELECT
            p.*,
            (
                CASE
                    p.type
                    WHEN 2 THEN
                        c.activity_name
                    WHEN 1 THEN
                        w.white_name
                    END
                ) AS 'name'
        FROM
            ims_activity_privilege_prize p
        LEFT JOIN ims_yidu_coupon c ON p.ref_id = c.id
        AND p.type = 2
        LEFT JOIN ims_yidu_whitelist w ON p.ref_id = w.id
        AND p.type = 1
        <where>
             <if test=" dto.type == 1 ">
                 AND p.type = 1
             </if>
            <if test=" dto.type == 2 ">
                AND p.type = 2
            </if>
        </where>
    </select>

 mybatis-plus批量更新指定条件的数据

            ImsYiduAdmin update = new ImsYiduAdmin();
            update.setAccount("1231231");
            LambdaQueryWrapper<ImsYiduAdmin> where = new LambdaQueryWrapper();
            where.eq(ImsYiduAdmin::getAccount, "123");
            adminService.update(update, where);
原文地址:https://www.cnblogs.com/bing2017/p/14540908.html