兵兵有你

人品好,气质差.丢了工作就回家...

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

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);

 

 

方式2:

1.搭建好spring boot项目。

2.定义好application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bing
spring.datasource.username=root
spring.datasource.password=
mybatis.mapper-locations=classpath*:/mapper/*.xml //这个很重要

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl//控制台打印SQL日志

3.创建好controller层,servive层(@service),entity层,mapper层(@mapper),serviceImpl类中需要autowire引入mapper层的对象才能调用 

4.然后在resources/mapper目录下写查询的xml文件,其中namespace是指mapper接口层的路径,resultType为返回的实体路径

 mapper里的另一种用法

@Mapper
public interface UserDAO {

    @Select("select * from user where name = #{name}")
    public User find(String name);

    @Select("select * from user where name = #{name} and pwd = #{pwd}")
    /**
      * 对于多个参数来说,每个参数之前都要加上@Param注解,
      * 要不然会找不到对应的参数进而报错
      */
    public User login(@Param("name")String name, @Param("pwd")String pwd);
}

 

 

 

 

 

posted on 2021-03-15 23:49  greatbing  阅读(97)  评论(0编辑  收藏  举报