牛客网项目--MyBatis

1. 安装软件:

MySQL

官网下载–>在根目录下,新建文件夹my.ini–>
在这里插入图片描述
在这里插入图片描述

  • 对mysql进行初始化
    • 配置环境变量,将mysql的bin目录配置到path中;
    • 对mysql进行初始化,密码设定等,
      在这里插入图片描述
      启动mysql

net start mysql
登录mysql
mysql -uroot -p
建库导入数据表
source ‘sql文件的文件路径’

mysql-workbenth/navicat

装载客户端,可视化界面;本机上使用navicat
从官网下载,正常安装即可;
建立连接即可
在这里插入图片描述
在这里插入图片描述

MyBatis

核心组件

  • SqlSessionFactory:用于创建SqlSession的工厂类。
  • SqlSession: MyBatis的核心组件,用于向数据库执行SQL。
    -主配置文件:XML配置文件,可以对MyBatis的底层行为做出详细的配置。
  • Mapper接口:就是DAO接口(会有实现的机制,不用写实现类),在MyBatis中习惯性的称之为Mapper。
  • Mapper映射器:用于编写SQL,并将SQL和实体类映射的组件,采用XML、注解均可实现。·
  • 示例:使用MyBatis对用户表进行CRUD操作。

http://www.mybatis.org/mybatis-3
http://www.mybatis.org/spring

前三个组件被springboot整合,不需要自己实现

示例

前置操作
对用户表user,用mybatis进行增删改的操作
在项目中导入包
在这里插入图片描述

配置文件:

在这里插入图片描述
实体类:

在这里插入图片描述
在这里插入图片描述

代码部分:
创建user类

package com.nowcoder.community.entity;

import java.util.Date;

public class User {
    private int id;
    private String username;
    private String salt;
    private String email;
    private int type;
    private int status;
    private String activationCode;
    private String headerUrl;
    private Date createTime;

    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 getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getActivationCode() {
        return activationCode;
    }

    public void setActivationCode(String activationCode) {
        this.activationCode = activationCode;
    }

    public String getHeaderUrl() {
        return headerUrl;
    }

    public void setHeaderUrl(String headerUrl) {
        this.headerUrl = headerUrl;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", salt='" + salt + '\'' +
                ", email='" + email + '\'' +
                ", type=" + type +
                ", status=" + status +
                ", activationCode='" + activationCode + '\'' +
                ", headerUrl='" + headerUrl + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}

创建Usermapper的抽象类,访问数据库,在Dao层中写。不用写实现类,

package com.nowcoder.community.dao;

import com.nowcoder.community.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
//添加注解,装配bean
@Mapper
public interface UserMapper {
    User selectById(int id);

    User selectByName(String name);

    User selectByEmail(String email);

    int insertUser(User user);

    int updateStatus(int id,int status);

    int updateHeader(int id,String headerUrl);

    int updatePassword(int id,String password);
}

提供配置文件
mapper文件夹下创建*.xml文件
在这里插入图片描述
在这里插入图片描述

注意写全限定名(完整的包名)

在这里插入图片描述
在这里插入图片描述

<?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.nowcoder.community.dao.UserMapper">
    <sql id="selectFields">
        id, username,password,salt,email,type,status,activation_code,header_url, create_time
    </sql>
    <select id="selectById" resultType="User">
        select id, username,password,salt,email,type,status,activation_code,header_url, create_time
        from user
        where id=#{id}
    </select>
    <select id="selectByame" resultType="User">
        select id, username,password,salt,email,type,status,activation_code,header_url, create_time
        from user
        where name=#{username}
    </select>
    <select id="selectByame" resultType="User">
        select id, username,password,salt,email,type,status,activation_code,header_url, create_time
        from user
        where email=#{email}
    </select>

</mapper>

在这里插入图片描述
完整增改查的操作,并在测试类里面验证

<?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.nowcoder.community.dao.UserMapper">
    <sql id="insertFields">
        id, username,password,salt,email,type,status,activation_code,header_url, create_time
    </sql>

    <sql id="selectFields">
        id, username,password,salt,email,type,status,activation_code,header_url, create_time
    </sql>
    <select id="selectById" resultType="User">
        select <include refid="selectFields"></include>
        from user
        where id=#{id}
    </select>
    <select id="selectByame" resultType="User">
        select <include refid="selectFields"></include>
        from user
        where name=#{username}
    </select>
    <select id="selectByame" resultType="User">
        select <include refid="selectFields"></include>
        from user
        where email=#{email}
    </select>

    <insert id="insertUser" parameterType="User" keyProperty="id">
        insert into user (<include refid="insertFields"></include>)
        values(#{username},#{password},#{salt},#{email},#{type},#{status},#{activationCode},#{headerUrl},#{createTime})
    </insert>
    <update id="updateStatus">
        update user set status=#{status} where id=#{id}
    </update>
    <update id="updateHeader">
        update user set header_url=#{headerUrl} where id=#{id}
    </update>
    <update id="updatePassword">
        update user set password=#{password} where id=#{id}
    </update>
</mapper>
package com.nowcoder.community;

import com.nowcoder.community.dao.UserMapper;
import com.nowcoder.community.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MapperTests {

    @Autowired//注入bean
    private UserMapper userMapper;

    @Test
    public void testSelectUser(){
        User user=userMapper.selectById(101);
        System.out.println(user);

        user=userMapper.selectByName("liubei");
        System.out.println(user);

        user=userMapper.selectByEmail("nowcoder101@sina.com");
        System.out.println(user);

    }
    @Test
    public void testInsertUser(){
        User user=new User();
        user.setUsername("test");
        user.setPassword("123456");
        user.setSalt("abc");
        user.setEmail("test@qq.com");
        user.setHeaderUrl("http://www.nowcoder.com/101.png");
        user.setCreateTime(new Date());

        int rows=userMapper.insertUser(user);
        System.out.println(user);
        System.out.println(user.getId());
    }
    @Test
    public void updateUser(){
        int rows=userMapper.updateStatus(150,1);
        System.out.println(rows);

        rows=userMapper.updateHeader(150,"http://www.nowcoder.com/101.png");
        System.out.println(rows);

        rows=userMapper.updatePassword(150,"123456789");
        System.out.println(rows);


    }
}

调试技巧
设置日志级别

logger

#logging.level.com.nowcoder.community=debug
#logging.file=d:/work/data/nowcoder/community.log

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要创建一个Spring Boot项目并整合MyBatis-Plus,你可以按照以下步骤进行操作: 1. 首先,在你的项目的pom.xml文件中添加MyBatis-Plus和MySQL驱动的依赖。你可以引用和中提供的示例代码来配置pom.xml文件。这将确保你的项目具有使用MyBatis-Plus和MySQL的必要依赖。 2. 接下来,创建一个用于定义数据库连接和其他配置的配置文件。你可以在Spring Boot的配置文件(application.properties或application.yaml)中添加以下配置信息: - 数据库连接配置:包括数据库的URL、用户名和密码等信息。 - MyBatis-Plus配置:你可以配置MyBatis-Plus的一些属性,比如自动填充、逻辑删除等。 你可以根据你的实际需求进行配置。 3. 然后,创建数据库对应的实体类。你可以使用Java类来数据库,并在类上使用注解来映射数据库字段和。 4. 接下来,创建Mapper接口和Mapper.xml文件。Mapper接口用于定义数据库操作的方法,而Mapper.xml文件用于编写具体的SQL语句。你可以使用MyBatis-Plus的自动注入功能来简化这一过程。通过继承MyBatis-Plus提供的BaseMapper接口,你可以自动获得常见的CRUD操作方法。 5. 最后,编写业务逻辑代码并注入Mapper。在你的Service类中,你可以注入Mapper接口,并使用它来调用数据库操作方法。你可以根据你的实际需求编写其他业务逻辑代码。 运行你的Spring Boot项目后,你应该能够看到控制台输出一系列信息,明Spring Boot项目成功整合了MyBatis-Plus。你可以参考中提供的示例代码来验证整合结果。 总结起来,创建Spring Boot项目并整合MyBatis-Plus的步骤包括:配置pom.xml文件、创建配置文件、定义实体类、创建Mapper接口和Mapper.xml文件、编写业务逻辑代码。你可以根据所提供的参考内容来详细了解每个步骤的具体实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值