学习手册
http://www.mybatis.org/mybatis-3
http://www.mybatis.org/spring
导包可以去这个网址查询,有使用趋势:
https://mvnrepository.com/search?q=mysql
mysql
mysql-connector-java
8.0.28
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
# 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
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);
}
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}
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);
}
}
为了方便调试发现错误 可以把dao层的日志级别设置为debug
# logger
logging.level.com.js.community.dao=debug