首页 技术 正文
技术 2022年11月11日
0 收藏 617 点赞 2,498 浏览 1670 个字

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

1.增加持久化层

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

SpringBoot 集成 Mybatis(三)

2.Mapper相关

实体类

public class Emp {private Integer empId;
private String empName;
private Integer empAge;

SpringBoot 集成 Mybatis(三)

数据库表

CREATE TABLE `table_emp` (
`emp_id`  int NOT NULL AUTO_INCREMENT ,
`emp_name`  varchar(100) NULL ,
`emp_age`  int NULL ,
PRIMARY KEY (`emp_id`)
)

SpringBoot 集成 Mybatis(三)

Mapper配置文件

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.springboot.mappers.EmpMapper">
<select id="selectAll" resultType="com.atguigu.springboot.bean.Emp">
select emp_id empId, emp_name empName, emp_age empAge
from table_emp
</select>
</mapper>

SpringBoot 集成 Mybatis(三)

Mapper接口

public interface EmpMapper {List<Emp> selectAll();}

SpringBoot 集成 Mybatis(三)

Service接口

@Transactional
public interface EmpService {List<Emp> getAll();}

SpringBoot 集成 Mybatis(三)

Service 接口实现

@Service
public class EmpServiceImpl implements EmpService {@Autowired
private EmpMapper empMapper;@Override
public List<Emp> getAll() {
return empMapper.selectAll();
}}

SpringBoot 集成 Mybatis(三)

Handler调用

@Autowired
private EmpService empService;@ResponseBody
@RequestMapping("/getAll")
public List<Emp> getAll() {
return empService.getAll();
}

SpringBoot 集成 Mybatis(三)

3.增加application.yml配置

spring:
datasource:
name: mydb
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/sb_db
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath*:/mybatis/*Mapper.xml

SpringBoot 集成 Mybatis(三)

4.在主启动类上使用注解扫描Mapper

@MapperScan("com.webcode.springboot.mappers")

SpringBoot 集成 Mybatis(三)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919