spring boot2.1.1 集成tk mybatis

 

 

此文章内容来源  https://gitee.com/free/Mapper/wikis/1.3-spring-boot?sort_id=208198

环境:

  1. 开发IDE:idea
  2. spring boot:2.1.1 release
  3. tk.mybatis 4
  4. mysql 5
  5. druid 1.0.25
  • 使用spring boot 初始化项目

    pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zjchsoft.bi</groupId>
    <artifactId>tkmb</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>tkmb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.25</version>
        </dependency>

        <!-- 通用 Mapper -->
        <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.0.0</version>
        </dependency>


        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.2.1</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.29</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.0.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>
  • 配置mappers
     
    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/test
        username: root
        password:
        driver-class-name: com.mysql.jdbc.Driver
    mapper:
      mappers:
        - tk.mybatis.mapper.common.Mapper
        - tk.mybatis.mapper.common.Mapper2
      not-empty: true
    # 自定义xml文件时引入,否则可以不引入
    mybatis:
      mapperLocations: classpath:mapper/*.xml

     
  • 配置扫描包路径
package com.zjchsoft.bi.tkmb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@tk.mybatis.spring.annotation.MapperScan(basePackages = "com.zjchsoft.bi.tkmb.mapper")
public class TkmbApplication {

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

}

  •  添加自定义查询
     
    <?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.zjchsoft.bi.tkmb.mapper.UserMapper" >
      <resultMap id="BaseResultMap" type="com.zjchsoft.bi.tkmb.entity.User" >
        <!--
          WARNING - @mbggenerated
          This element is automatically generated by MyBatis Generator, do not modify.
          This element was generated on Wed Dec 19 21:57:41 CST 2018.
        -->
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="user_name" property="userName" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
        <result column="sex" property="sex" jdbcType="VARCHAR" />
      </resultMap>
      <sql id="Base_Column_List" >
            id, user_name, password, sex
        </sql>
      <select id="selectById2" parameterType="int" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from t_user
        where id = #{id}
      </select>
    </mapper>

     

package com.zjchsoft.bi.tkmb.mapper;

import com.zjchsoft.bi.tkmb.entity.User;
import tk.mybatis.mapper.common.Mapper;
//不用添加注解,starter会自动扫描继承Mapper的类文件
public interface UserMapper extends Mapper<User> {
    //自定义查询
    public User selectById2(int id);
}

 

使用控制层进行访问
 

package com.zjchsoft.bi.tkmb.controller;

import com.zjchsoft.bi.tkmb.entity.User;
import com.zjchsoft.bi.tkmb.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class Usercontroller {

    @Autowired
    UserMapper userMapper;

    @GetMapping("/all")
    public List<User> getUser(){
        return userMapper.selectAll();
    }

    @GetMapping("/get/{id}")
    public User getUser(@PathVariable("id") int id){
        return userMapper.selectById2(id);
    }
}
  • 启动项目进行访问



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值