Spring-BootSpring Boot 菜鸟教程网站收藏

Spring Boot 学习笔记(3):MyBatis

2018-10-08  本文已影响22人  HOWD

上一篇是通过Jdbc访问数据库,本篇来写通过MyBatis数据访问。

首先在pom.xml中加入MyBatis依赖。

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.1.1</version>
</dependency>

可以不添加spring-boot-starter-jdbc。因为,mybatis-spring-boot-starter依赖中存在spring-boot-starter-jdbc。

mysql的依赖还是要在的,不然怎么连接数据库呢。

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.35</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.14</version>
</dependency>

修改resource下的application.properties

## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml

结构图


image

在MySQL中创建City表,并在src/domain/下创建City实体类

public class City {

    private Long id;

    private Long provinceId;

    private String cityName;
 
    private String description;
    // 省略getter和setter

}

在resource中创建mapper文件夹,在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="org.spring.springboot.dao.CityDao">
    <resultMap id="BaseResultMap" type="org.spring.springboot.domain.City">
        <result column="id" property="id" />
        <result column="province_id" property="provinceId" />
        <result column="city_name" property="cityName" />
        <result column="description" property="description" />
    </resultMap>

    <parameterMap id="City" type="org.spring.springboot.domain.City"/>
    
    <sql id="Base_Column_List">
        id, province_id, city_name, description
    </sql>
    
    <select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List" />
        from city
        where city_name = #{cityName}
    </select>
</mapper>

Controller层

@RestController
public class CityRestController {

    @Autowired
    private CityService cityService;

    @RequestMapping(value = "/api/city", method = RequestMethod.GET)
    public City findOneCity(@RequestParam(value = "cityName", required = true) String cityName) {
        return cityService.findCityByName(cityName);
    }
}

Service层

/**
 * 城市业务逻辑接口类
 */
public interface CityService {

    /**
     * 根据城市名称,查询城市信息
     * @param cityName
     */
    City findCityByName(String cityName);
}

/**
 * 城市业务逻辑实现类
 */
@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    @Override
    public City findCityByName(String cityName) {
        return cityDao.findByName(cityName);
    }

}

Dao层

/**
 * 城市 DAO 接口类
 */
@Component
public interface CityDao {

    /**
     * 根据城市名称,查询城市信息
     *
     * @param cityName 城市名
     */
    City findByName(@Param("cityName") String cityName);
}

全部搞定后,运行项目,并在postman中测试接口即可。

上一篇下一篇

猜你喜欢

热点阅读