1.3 仿牛客论坛学习笔记--MyBaits入门

MyBatis入门

学习手册

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

http://www.mybatis.org/spring

1.3 仿牛客论坛学习笔记--MyBaits入门_第1张图片

1、导入sql,MyBatis依赖

导包可以去这个网址查询,有使用趋势:

https://mvnrepository.com/search?q=mysql

  
        
            mysql
            mysql-connector-java
            8.0.28
        



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.2

2、配置sql和MyBatis

  • 在application.properties文件中添加配置
  • mapUnder 为了自动实现合并,不区分大小写和下划线 为了实现数据库的字段和实体类相对应 比如 user_name ==UserName
  • classPath就是编译后的target/classes文件夹
# DataSourceProperties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
spring.datasource.username=root
spring.datasource.password=200172
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=30000

# MybatisProperties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.nowcoder.community.entity
mybatis.configuration.useGeneratedKeys=true
mybatis.configuration.mapUnderscoreToCamelCase=true

# logger
logging.level.com.nowcoder.community=debug

3、创建Mapper接口,在里面写方法

package com.js.community.dao;

import com.js.community.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    //根据id查找用户
    User queryById(int id);
    User queryByName(String name);
    User queryByEmail(String email);
    //插入一个用户
    int InsertUser(User user);
    //更新用户信息
    int UpdateStatus(int id,int Status);
    int UpdateHeader(int id,String Header_url);
    int UpdatePassword(int id,String password);
}

4、创建xml文件配置sql 语句

  • namespace写Mapper所在的路径
  • 标签中的id值为对应的方法名字,****标签需要写声明返回类型为对应的实体类,标签中如果传入的参数是实体类需要声明,普通参数可以不声明
  • 在sql中引用参数使用 #{username}
  • sql语句中共同的部分可以提取出来复用 引用的时候写




    
        username, password, salt, email, type, status, activation_code, header_url, create_time
    

    
        id, username, password, salt, email, type, status, activation_code, header_url, create_time
    

    

    

    

    
        insert into user ()
        values(#{username}, #{password}, #{salt}, #{email}, #{type}, #{status}, #{activationCode}, #{headerUrl}, #{createTime})
    

    
        update user set status = #{status} where id = #{id}
    

    
        update user set header_url = #{header_url} where id = #{id}
    

    
        update user set password = #{password} where id = #{id}
    


5、测试

package com.js.community;

import com.js.community.dao.Impl.AlphaImpl;
import com.js.community.dao.UserMapper;
import com.js.community.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

@SpringBootTest
public class MapperTests {
    //先导入Mapper的Bean

    @Autowired(required=false)   //不加required会提示找不到这个Bean
    private UserMapper userMapper;
    @Test
    void selectTests()
    {
        User user = userMapper.queryById(101);
        System.out.println(user);
        
        
    }
}

6、调试

为了方便调试发现错误 可以把dao层的日志级别设置为debug

# logger
logging.level.com.js.community.dao=debug

你可能感兴趣的:(仿牛客论坛学习笔记,java,mysql,spring,mybatis)