mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Merge pull request #6 from xchunzhao/master
Add springboot for mybatis annotation
This commit is contained in:
26
.gitignore
vendored
26
.gitignore
vendored
@@ -10,3 +10,29 @@
|
|||||||
|
|
||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
hs_err_pid*
|
hs_err_pid*
|
||||||
|
|
||||||
|
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
nbproject/private/
|
||||||
|
build/
|
||||||
|
nbbuild/
|
||||||
|
dist/
|
||||||
|
nbdist/
|
||||||
|
.nb-gradle/
|
||||||
24
springboot-mybatis-annotation/.gitignore
vendored
Normal file
24
springboot-mybatis-annotation/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
nbproject/private/
|
||||||
|
build/
|
||||||
|
nbbuild/
|
||||||
|
dist/
|
||||||
|
nbdist/
|
||||||
|
.nb-gradle/
|
||||||
BIN
springboot-mybatis-annotation/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
BIN
springboot-mybatis-annotation/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
Binary file not shown.
1
springboot-mybatis-annotation/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
1
springboot-mybatis-annotation/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip
|
||||||
64
springboot-mybatis-annotation/pom.xml
Normal file
64
springboot-mybatis-annotation/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>springboot</groupId>
|
||||||
|
<artifactId>springboot-mybatis-annotation</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>springboot-mybatis-annotation</name>
|
||||||
|
<description>Springboot-mybatis :: 整合Mybatis Annotation Demo</description>
|
||||||
|
|
||||||
|
<!-- Spring Boot 启动父依赖 -->
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.1.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
|
||||||
|
<mysql-connector>5.1.39</mysql-connector>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- Spring Boot Web 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot Test 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot Mybatis 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>${mybatis-spring-boot}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MySQL 连接驱动依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>${mysql-connector}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Junit -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.spring.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
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.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 xchunzhao on 02/05/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,27 @@
|
|||||||
|
package org.spring.springboot.dao;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.*;
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市 DAO 接口类
|
||||||
|
*
|
||||||
|
* Created by xchunzhao on 02/05/2017.
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface CityDao {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据城市名称,查询城市信息
|
||||||
|
*
|
||||||
|
* @param cityName 城市名
|
||||||
|
*/
|
||||||
|
@Select("SELECT * FROM city")
|
||||||
|
@Results({
|
||||||
|
@Result(property = "id", column = "id"),
|
||||||
|
@Result(property = "provinceId", column = "province_id"),
|
||||||
|
@Result(property = "cityName", column = "city_name"),
|
||||||
|
@Result(property = "description", column = "description"),
|
||||||
|
})
|
||||||
|
City findByName(@Param("cityName") String cityName);
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package org.spring.springboot.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市实体类
|
||||||
|
*
|
||||||
|
* Created by xchunzhao on 02/05/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 xchunzhao on 02/05/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 xchunzhao on 02/05/2017.
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class CityServiceImpl implements CityService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CityDao cityDao;
|
||||||
|
|
||||||
|
public City findCityByName(String cityName) {
|
||||||
|
return cityDao.findByName(cityName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
## 数据源配置
|
||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
|
||||||
|
spring.datasource.username=root
|
||||||
|
spring.datasource.password=123456
|
||||||
|
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||||
|
|
||||||
Reference in New Issue
Block a user