mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 14:13:52 +08:00
Springboot 整合 Mybatis 的完整 Web 案例
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package org.spring.springboot;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.spring.springboot.dao.CityDao;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot 应用启动类
|
||||
*
|
||||
* Created by bysocket on 16/4/26.
|
||||
*/
|
||||
// Spring Boot 应用的标识
|
||||
@SpringBootApplication
|
||||
@MapperScan("org.spring.springboot.dao")
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 程序启动入口
|
||||
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
|
||||
SpringApplication.run(Application.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.spring.springboot.controller;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.spring.springboot.dao;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.spring.springboot.domain.City;
|
||||
|
||||
/**
|
||||
* 城市 DAO 接口类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public interface CityDao {
|
||||
|
||||
/**
|
||||
* 根据城市名称,查询城市信息
|
||||
*
|
||||
* @param cityName 城市名
|
||||
*/
|
||||
City findByName(@Param("cityName") String cityName);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.spring.springboot.domain;
|
||||
|
||||
/**
|
||||
* 城市实体类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public class City {
|
||||
|
||||
/**
|
||||
* 城市编号
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 省份编号
|
||||
*/
|
||||
private Long provinceId;
|
||||
|
||||
/**
|
||||
* 城市名称
|
||||
*/
|
||||
private String cityName;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getProvinceId() {
|
||||
return provinceId;
|
||||
}
|
||||
|
||||
public void setProvinceId(Long provinceId) {
|
||||
this.provinceId = provinceId;
|
||||
}
|
||||
|
||||
public String getCityName() {
|
||||
return cityName;
|
||||
}
|
||||
|
||||
public void setCityName(String cityName) {
|
||||
this.cityName = cityName;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.spring.springboot.service;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑接口类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public interface CityService {
|
||||
|
||||
/**
|
||||
* 根据城市名称,查询城市信息
|
||||
* @param cityName
|
||||
*/
|
||||
City findCityByName(String cityName);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.spring.springboot.service.impl;
|
||||
|
||||
import org.spring.springboot.dao.CityDao;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑实现类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
@Service
|
||||
public class CityServiceImpl implements CityService {
|
||||
|
||||
@Autowired
|
||||
private CityDao cityDao;
|
||||
|
||||
public City findCityByName(String cityName) {
|
||||
return cityDao.findByName(cityName);
|
||||
}
|
||||
|
||||
}
|
||||
24
springboot-mybatis/src/main/resources/mapper/CityMapper.xml
Normal file
24
springboot-mybatis/src/main/resources/mapper/CityMapper.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?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>
|
||||
12
springboot-mybatis/src/main/resources/mybatis-config.xml
Normal file
12
springboot-mybatis/src/main/resources/mybatis-config.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
<typeAliases>
|
||||
<package name="org.spring.springboot.domain"/>
|
||||
</typeAliases>
|
||||
<mappers>
|
||||
<mapper resource="mapper/CityMapper.xml"/>
|
||||
</mappers>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user