springboot整合mybatis(一)

如何用idea intellij 新建一个springboot 项目:

转自:https://blog.csdn.net/flyer_tang/article/details/80282863

 

先新建一个maven项目,目录如下:

Application.java    spring项目运行的入口

package com.wxm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
//@MapperScan("com.wxm.dao") 
public class Application {
    
        public static void main(String[] args)throws Exception {
            SpringApplication.run(Application.class, args);
        }
        
}
@SpringBootApplication是下面三个的组合
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
有了该注解(@SpringBootApplication)会扫描Application.java包及其子包下的java文件
TestController.java
package com.wxm.controller;
import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wxm.domain.Tablea;
import com.wxm.service.ITestService;

@RestController
public class TestController{
    @Resource 
    private ITestService testService;
    
        @RequestMapping("/vv")
        String home(){
            return "hello world";
        }
        
        @RequestMapping("/test")
        Tablea ttt(){
            return testService.selectByPrimaryKey(1l);
        }
}
ITableaDao.java
package com.wxm.dao;

import org.apache.ibatis.annotations.Mapper;

import com.wxm.domain.Tablea;
@Mapper
//这里和 Application.java 中的
@MapperScan("com.wxm.dao") 任意写一个就可以
public interface ITableaDao { 
  Tablea selectByPrimaryKey(Long id);
}
ITestService.java

package com.wxm.service;

import com.wxm.domain.Tablea;

public interface ITestService {
    Tablea selectByPrimaryKey(Long id);
}
TestServiceImpl.java
package com.wxm.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wxm.dao.ITableaDao;
import com.wxm.domain.Tablea;
import com.wxm.service.ITestService;

@Service
public class TestServiceImpl implements ITestService{
    @Resource
    private ITableaDao tableDao;
    @Override
    public Tablea selectByPrimaryKey(Long id) {
        System.out.println("srevice");
        return tableDao.selectByPrimaryKey(id);
    }

}
TableaMapper.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.wxm.dao.ITableaDao">
  <resultMap id="BaseResultMap" type="com.wxm.domain.Tablea">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="num" jdbcType="VARCHAR" property="num" />
    <result column="date" jdbcType="DATE" property="date" />
    <result column="price" jdbcType="REAL" property="price" />
    <result column="flightnum" jdbcType="VARCHAR" property="flightnum" />
    <result column="leg" jdbcType="VARCHAR" property="leg" />
  </resultMap>
  <sql id="Base_Column_List">
    id, num, date, price, flightnum, leg
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from tablea
    where id = #{id,jdbcType=BIGINT}
  </select>
</mapper>
Tablea.java
package com.wxm.domain;

import java.util.Date;

public class Tablea {
    private Long id;

    private String num;

    private Date date;

    private Float price;

    private String flightnum;

    private String leg;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num == null ? null : num.trim();
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getFlightnum() {
        return flightnum;
    }

    public void setFlightnum(String flightnum) {
        this.flightnum = flightnum == null ? null : flightnum.trim();
    }

    public String getLeg() {
        return leg;
    }

    public void setLeg(String leg) {
        this.leg = leg == null ? null : leg.trim();
    }
}

application.properties

#服务器端口号配置
server.port=18080

#mybatis配置
mybatis.type-aliases-package = com.wxm.domain  
#classpath 指的就是编译路径 src/main/resources mybatis.mapper-locations = classpath:mapper/*.xml #mysql配置 spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver

pom.xml

<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>com.wxm</groupId>
  <artifactId>MyDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/>
      </parent>
    <dependencies>  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
     </dependencies>
</project>

   运行 在main函数中 点击右键 run as java application 

结果:
项目地址:https://github.com/WxmDe/springBoot.git

 

posted @ 2018-01-15 17:54  小梅子呀  阅读(199)  评论(0)    收藏  举报