1.引入依赖
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.2.8
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
com.github.pagehelper
pagehelper
5.3.0
2.写application.yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
#Mapper.xml所在的位置
mapper-locations: classpath*:mapper
private Integer deptno;
private String dname;
private String loc;
}
4.写mapper
@Mapper
public interface DeptMapper {
int deleteByPrimaryKey(Integer deptno);
int insert(Dept record);
int insertSelective(Dept record);
Dept selectByPrimaryKey(Integer deptno);
int updateByPrimaryKeySelective(Dept record);
int updateByPrimaryKey(Dept record);
int batchInsert(@Param("list") List list);
}
5.写mapper.xml
deptno, dname, loc
select
from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
delete from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
insert into tb_dept (deptno, dname, loc
)
values (#{deptno,jdbcType=TINYINT}, #{dname,jdbcType=VARCHAR}, #{loc,jdbcType=VARCHAR}
)
insert into tb_dept
deptno,
dname,
loc,
#{deptno,jdbcType=TINYINT},
#{dname,jdbcType=VARCHAR},
#{loc,jdbcType=VARCHAR},
update tb_dept
dname = #{dname,jdbcType=VARCHAR},
loc = #{loc,jdbcType=VARCHAR},
where deptno = #{deptno,jdbcType=TINYINT}
update tb_dept
set dname = #{dname,jdbcType=VARCHAR},
loc = #{loc,jdbcType=VARCHAR}
where deptno = #{deptno,jdbcType=TINYINT}
insert into tb_dept
(deptno, dname, loc)
values
(#{item.deptno,jdbcType=TINYINT}, #{item.dname,jdbcType=VARCHAR}, #{item.loc,jdbcType=VARCHAR}
)
6.测试
@SpringBootTest
class DeptMapperTest {
@Resource
DeptMapper deptMapper;
@Test
void selectByPrimaryKey() {
Dept dept = deptMapper.selectByPrimaryKey(10);
System.out.println(dept);
}
}