springboot之整合mybatis-annotation(注解方式)

本例将采用maven管理,代码托管在github上,地址:https://github.com/wolf909867753/springboot

1。创建maven-module,mybatis-annotation,并在pom.xml中添加springboot依赖

<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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>mybatis-annotation</artifactId>
    <packaging>war</packaging>
    <name>mybatis-annotation Demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
        <mysql-spring-boot>5.1.32</mysql-spring-boot>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-spring-boot}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>mybatis-annotation</finalName>
    </build>
</project>

2.工程结构



3.创建工程mybaits,添加配置文件resources\application.properties工程

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


    目录如下:
    com\springboot\domain\City.java
    

public class City implements Serializable{


    /**
     * 城市编号
     */
    private Long id;

    /**
     * 省份编号
     */
    private Long provinceId;

    /**
     * 城市名称
     */
    private String cityName;

    /**
     * 描述
     */
    private String description;
        getter/setter.....

}   


    com\springboot\dao\CityDao.java
    

/**
 * 城市 DAO 接口类
 * Created by wanglu-jf on 17/6/27.
 * @Mapper 标志接口为 MyBatis Mapper 接口
 * @Select 是 Select 操作语句
 * @Results 标志结果集,以及与库表字段的映射关系
 */
@Mapper
public interface CityDao {
    /**
     * 查询城市信息
     */
    @Select(" SELECT * FROM city WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "provinceId", column = "province_id"),
            @Result(property = "cityName", column = "city_name"),
            @Result(property = "description", column = "description")
    })
    public City queryCityById(@Param("id") int id);
}

   
    com\springboot\service\ICityService.java
    
public interface ICityService {

    /**
     * 查询城市信息
     */
    public City queryById(int id);
}

    
    com\springboot\service\impl\CityServiceImpl.java
    
@Service
public class CityServiceImpl implements ICityService {

    @Autowired
    private CityDao cityDao;
    /**
     * 查询城市信息
     */
    @Override
    public City queryById(int id) {
        return cityDao.queryCityById(id);
    }
}

    
    com\springboot\controller\CityController.java
    
    
@RestController
@RequestMapping("city/")
public class CityController {

    @Autowired
    private ICityService cityService;

    @RequestMapping(value = "query/{id}",method = RequestMethod.GET)
    public City queryById(@PathVariable("id") int id){
        return this.cityService.queryById(id);
    }
}


    com\springboot\Application.java
    
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

    
4.运行Application.java的main方法或者发布到tomcat,
    访问http://127.0.0.1:8080/city/query/%E6%BD%8D%E5%9D%8A%E5%B8%82,
    输出
        {"id":1,"provinceId":1,"cityName":"潍坊市","description":"我的家在山东省潍坊市。"}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值