spring boot 整合mybatis

代码下载地址

  • maven配置
<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.ljb</groupId>
  <artifactId>springboot-mybatis</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> 
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</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</artifactId>
            <version>1.1.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 创建表
    spring boot 整合mybatis_第1张图片
    spring boot 整合mybatis_第2张图片
  • 工程结构
    spring boot 整合mybatis_第3张图片
  • application.yml配置
server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  typeAliasesPackage: com.ljb.springboot.entity
  mapperLocations: classpath:mapper/*.xml
  • DAO层
public interface ExampleDao {

  public Student findById(int id);
  public List<Student> findByIds(List<Integer> ids);
  public List<RelationResult> relationQuery();
  
}

  • mapper映射文件
<?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.ljb.springboot.dao.ExampleDao">
	<resultMap id="BaseResultMap"
		type="com.ljb.springboot.entity.Student">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="name" property="name" jdbcType="VARCHAR" />
		<result column="teacher_id" property="teacher_id"
			jdbcType="INTEGER" />
	</resultMap>
	
	<resultMap id="RelationResultMap"
		type="com.ljb.springboot.entity.RelationResult">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="sid" property="sid" jdbcType="INTEGER" />
	</resultMap>
	<!-- parameterType 传入参数类型 resultType 返回结果类型 resultMap 用于引用通过 resultMap 标签定义的映射类型,这也是mybatis组件高级复杂映射的关键 -->
	<select id="findById" resultMap="BaseResultMap"
		parameterType="java.lang.Integer">
		select * from t_student where id =
		#{id,jdbcType=INTEGER}
	</select>

	<select id="findByIds" parameterType="java.lang.Integer"
		resultMap="BaseResultMap">
		select * from t_student
		<if test="list!=null">
			where id in
			<foreach item="item" collection="list" index="index"
				open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
	</select>
	
	<select id="relationQuery" resultMap="RelationResultMap">
		select t1.id as id,t2.sid as sid from t_student t1 left join  t_teacher t2 on t1.id=t2.sid
	</select>
</mapper>

Controller层

@RestController
@RequestMapping(path="/example")
public class ExampleController {

  @Autowired 
  ExampleDao dao;
  @RequestMapping(path="/querybyid",method=RequestMethod.GET)
  public Student queryStudentById(int id) {
    return dao.findById(id);
  }
  
  @RequestMapping(path="/querybyids",method=RequestMethod.POST)
  public List<Student> queryStudentByIds(@RequestBody List<Integer> ids) {
    return dao.findByIds(ids);
  }
  
  @RequestMapping(path="/querybyrelation",method=RequestMethod.GET)
  public List<RelationResult> relationQuery() {
    return dao.relationQuery();
  }
  
  
}

  • 启动类, 使用 @MapperScan(“com.ljb.springboot.dao”) 扫描dao层
@SpringBootApplication
@MapperScan("com.ljb.springboot.dao")
public class ExampleApplication {

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

两个实体类

public class RelationResult {
  private int id;
  private int sid;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public int getSid() {
    return sid;
  }
  public void setSid(int sid) {
    this.sid = sid;
  }
}
package com.ljb.springboot.entity;

public class Student {
  private int id;
  private String name;
  private int teacher_id;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getTeacher_id() {
    return teacher_id;
  }
  public void setTeacher_id(int teacher_id) {
    this.teacher_id = teacher_id;
  }
}

你可能感兴趣的:(spring,boot)