springboot-mybatis整合

springboot-mybatis整合
注意点:有5个

MybatisApplication

package com.hipad;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.hipad.dao") //注意点1,要包扫描
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }
}

CityController

package com.hipad.controller;
import com.hipad.Service.CityService;
import com.hipad.domain.City;
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;
@RestController //注意点2,相当于@ResponseBody和@Controller
public class CityController {
    @Autowired
    CityService cityService;
    @RequestMapping(value = "city/list",method = RequestMethod.GET)
    public City findCity(@RequestParam(value = "cityName",required = true) String cityName){
        System.out.println("我是findCity------------------------------------------");
        return cityService.findCity(cityName);
    }
}

CityService

package com.hipad.Service;
import com.hipad.domain.City;
date:2018/5/13
 */
public interface CityService {
    City findCity(String cityName);
}

CityServiceImpl

package com.hipad.Service.impl;
import com.hipad.Service.CityService;
import com.hipad.dao.CityDao;
import com.hipad.domain.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CityServiceImpl implements CityService {
    @Autowired
    CityDao cityDao;
    @Override
    public City findCity(String cityName) {
        return cityDao.findCity(cityName);
    }
}

CityDao

package com.hipad.dao;
import com.hipad.domain.City;
public interface CityDao {
    City findCity(String cityName);
}

domain层的City

package com.hipad.domain;
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;
    }
    @Override
    public String toString() {
        return "City{" +
                "id=" + id +
                ", provinceId=" + provinceId +
                ", cityName='" + cityName + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

resources中的application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123

##mybatis配置,首先包扫描,后加载配置文件
mybatis.typeAliasesPackage=com.hipad.domain //注意点3,包扫描domain实体类
mybatis.mapperLocations=classpath:mapper/*.xml

resources中的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.hipad.dao.CityDao">
    <resultMap id="BaseMap" type="com.hipad.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>

    <select id="findCity" resultMap="BaseMap" parameterType="String">
        SELECT * FROM city WHERE city_name=#{cityName} 
    </select> // 注意点4,city_name不是实体类属性
</mapper>

POM

<?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</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mybatis :: 整合 Mybatis Demo</name>

    <!-- 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 依赖 -->
      <!-- 注意点5,依赖,不在spring依赖包,在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>
阅读更多
个人分类: springboot
上一篇CSDN-markdown编辑器
下一篇idea导入项目,加载不了tomcat问题
想对作者说点什么? 我来说一句

springboot-mybatis 简单整合

2018年04月20日 21.59MB 下载

SpringBoot-Mybatis多数据源.

2018年01月20日 43KB 下载

springboot-mybatis整合demo

2015年12月31日 995KB 下载

springboot-mybatis项目练习

2017年06月14日 7.64MB 下载

Springboot-mybatis

2017年07月12日 420KB 下载

没有更多推荐了,返回首页

关闭
关闭