Mybatis-基础操作-查询
- 解决办法
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=1234
# 指定mybatis输出日志的位置,输出控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#开启mybati的驼峰命名自动映射开关
mybatis.configuration.map-underscore-to-camel-case=true
package com.csdn.mapper;
import com.csdn.pojo.Emp;
import org.apache.ibatis.annotations.*;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
//根据ID删除数据
@Delete("delete from emp where id =#{id}")
/*public void delete(Integer id);*/
public int delete(Integer id);
//新增员工
@Options(keyProperty = "id",useGeneratedKeys = true)//会自动将生成的主键值,赋值给emp对象的id属性
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time,update_time) " +
"value (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
public void insert(Emp emp);
//更新员工
@Update("update emp\n" +
"set username=#{username},\n" +
" name=#{name},\n" +
" gender=#{gender},\n" +
" image=#{image},\n" +
" job=#{job},\n" +
" entrydate=#{entrydate},\n" +
" dept_id=#{deptId},\n" +
" update_time=#{updateTime}\n" +
"where id = #{id};\n")
public void update(Emp emp);
/* //根据id查询员工
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);*/
//方案一:给字段起别名,让别名与实体类属性一致
//根据id查询员工
/* @Select("select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id=#{id}")
public Emp getById(Integer id);*/
//方案二:通过@Results,@Result注解手动映射封装
//根据id查询员工
/* @Results({
@Result(column = "dept_id",property = "deptId"),
@Result(column = "create_time",property = "createTime"),
@Result(column = "update_time",property = "updateTime")
})
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);*/
//方案三:开启mybatis的驼峰命名自动映射开关
//新增员工
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);
}