1.使用通用mapper可以更快速的进行数据库的增删查改操作,加入springboot的管理,常规的SQL都可以复用
2.整合
a)引入依赖
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<!--Mybatis依赖-->
<!--Mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--数据库连接依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--springboot整合web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
. 1)注意:这里由于引入的依赖包含好多spring有关的依赖,导致spring的版本可能不兼容.因此还需要增加以下标签,实现对所有有关spring的依赖版本的管理,使版本一一致或兼容
<!--统一管理springboot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
.b)配置文件
server:
port: 8080
#jdbc
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/project1?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
#mybatis
mybatis:
type-aliases-package: com.brant.entity
#通用mapper配置
mapper:
mappers: com.brant.basedao.IBaseDao
identity: MYSQL
.c)配置启动类,这里需要注意MapperScan需要引入tk的包
@SpringBootApplication
@MapperScan({"com.brant.dao"})
public class MyApplication {
public MyApplication() {
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
.d)建立src目录
e)生成数据库和表cities
1)sql
create table cities(
id int primary key auto_increment,
cityid varchar(20) not null,
city varchar(20) not null,
provinceid varchar(20) not null
);
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '110100', '市辖区', '110000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '110200', '县', '110000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '120100', '市辖区', '120000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '120200', '县', '120000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130100', '石家庄市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130200', '唐山市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130300', '秦皇岛市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130400', '邯郸市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130500', '邢台市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130600', '保定市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130700', '张家口市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130800', '承德市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '130900', '沧州市', '130000');
INSERT INTO `cities`(`id`, `cityid`, `city`, `provinceid`) VALUES (null, '131000', '廊坊市', '130000');
.e)建立实体类映射,并实现序列化
@Repository
@Table(name = "cities")
public class Citiy implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "cityid")
private String cityId;
@Column(name = "city")
private String city;
@Column(name = "provinceid")
private String provinceId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvinceId() {
return provinceId;
}
public void setProvinceId(String provinceId) {
this.provinceId = provinceId;
}
@Override
public String toString() {
return "Cities{" +
"id=" + id +
", cityId='" + cityId + '\'' +
", city='" + city + '\'' +
", provinceId='" + provinceId + '\'' +
'}';
}
}
.f)依次编写业务控制层,服务业务层和mapper层
@Controller
@RequestMapping("/city")
public class CityController {
@Autowired
private ICityService iCityService;
@Autowired
private Cities cities;
@RequestMapping(method = RequestMethod.GET,path = "/selectAll")
@ResponseBody
public List<Cities> selectAll(){
return iCityService.selectAll();
}
@RequestMapping(method = RequestMethod.GET,path = "/selectOne/{id}")
@ResponseBody
public Cities selectOne(@PathVariable int id){
cities.setId(id);
return iCityService.selectOne(cities);
}
}
public interface ICityService {
List<Cities> selectAll();
Cities selectOne(Cities cities);
}
@Service
public class CityServiceImpl implements ICityService {
@Autowired
private ICityDao cityDao;
@Override
public List<Cities> selectAll() {
return cityDao.selectAll();
}
@Override
public Cities selectOne(Cities cities) {
return cityDao.selectOne(cities);
}
}
/**
* @author brant
* @date 2023/3/5 01:38
* 这里如果要使用mapper自己封装的方法,不要重写方法.否则会报错 Invalid bound statement (not found)
*/
@Component
public interface ICityDao extends IBaseDao<Cities>{
}
public interface IBaseDao<T> extends Mapper<T>,MySqlMapper<T> {
}
. 1)注意:由于这里使用了mapper的自己的方法,因此在写dao层时不需要重写方法了,否则spring会找不到mapper的方法.这时的dao也要添加@Component注解,交给spring管理
g)使用postman完成接口测试
.h)总结,在使用通用mapper时需要注意以下几点
1)检查application.yml文件配置是否正确
2)检查dao方法(这里排查了1h,很难排查)
3)MapperScan导包tk
4)pom文件要加入springboot版本管理