搜索 K
Appearance
博客正在加载中...
Appearance
Mybatis 非常流行,我们来整合一下。
为了方便,我们新建一个表:
CREATE TABLE city
(
id int(11) PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(30),
state VARCHAR(30),
country VARCHAR(30)
)
INSERT INTO city (id, name, state, country) VALUES (1,'稻妻', '原', '二次元')package com.peterjxl.learnspringbootwebadmin.bean;
import lombok.Data;
@Data
public class City {
private Long id;
private String name;
private String state;
private String country;
}新增 mapper 接口:
package com.peterjxl.learnspringbootwebadmin.mapper;
import com.peterjxl.learnspringbootwebadmin.bean.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CityMapper {
@Select("select * from city where id = #{id}")
City getById(Long id);
} 新增 service 层:
package com.peterjxl.learnspringbootwebadmin.service;
import com.peterjxl.learnspringbootwebadmin.bean.City;
import com.peterjxl.learnspringbootwebadmin.mapper.CityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CityService {
@Autowired
CityMapper cityMapper;
public City getCityById(Long id) {
return cityMapper.getById(id);
}
}新增一个表现层方法:
@Controller
public class IndexController {
@Autowired
CityService cityService;
@ResponseBody
@GetMapping("/city")
public City getCityById(@RequestParam("id") Long id) {
return cityService.getCityById(id));
}
//............
} 重启项目,然后访问:
除了使用注解、配置文件,还可以两者一起结合起来使用(例如有些 SQL 比较复杂,写在配置文件比较方便) 例如,我们要 insert 一个 city,先新增代理方法:
@Service
public class CityService {
@Autowired
CityMapper cityMapper;
public City getCityById(Long id) {
return cityMapper.getById(id);
}
public City saveCity(City city) {
return cityMapper.insert(city);
}
} 然后新增 resources/mybatis/mapper/CityMapper.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.peterjxl.learnspringbootwebadmin.mapper.CityMapper">
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into city (name, state, country) values (#{name}, #{state}, #{country})
</insert>
</mapper>如果我们想要获取 insert 后,自动增长的 id 值,可以使用 useGeneratedKeys 和 keyProperty 属性。 在 IndexController 新增 controller 方法:
@ResponseBody
@PostMapping("/city")
public City saveCity(City city) {
return cityService.saveCity(city);
}我们可以通过 postman 发送请求。先在浏览器登录,然后通过控制台获取 SessionID
新增表单数据,再发送请求:
数据库也能看到数据:

上述 insert 的功能,使用注解也可完成:
public interface CityMapper {
@Insert("insert into city(`name`,`state`,`country`) values(#{name},#{state},#{country})")
@Options(useGeneratedKeys = true,keyProperty = "id")
public void insert(City city);
}如果每个 Mapper 类上,都使用@Mapper 注解,那么等数据库表多了之后,就有很多的注解,为此我们可以使用一个注解,表明所有 Mapper 接口所在的地址:
package com.peterjxl.learnspringbootwebadmin;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@MapperScan("com.peterjxl.learnspringbootwebadmin.mapper")
@ServletComponentScan
@SpringBootApplication
public class LearnSpringBootWebAdminApplication {
public static void main(String[] args) {
SpringApplication.run(LearnSpringBootWebAdminApplication.class, args);
}
} 这样其他 Mapper 接口,就不用 @Mapper 注解了。当然,还是建议使用 @Mapper 注解的方式