Perfree

Perfree

未设置

未设置

54 文章数
72 评论数

SpringBoot-整合Mybatis-Annotation方式

perfree
2019-01-03 / 0 评论 / 2052 阅读 / 0 点赞

这里利用SpringBoot整合了Mybatis,本实例主要使用annotation注解方式,xml方式请参照[post cid="114" /],本实例Github地址05-mybatis-annotation,废话不多说,接下来开始整合:

POM文件

完整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>com.perfree</groupId>
    <artifactId>mybatis-annotation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mybatis-annotation</name>
    <description>SpringBoot-Mybatis-Annotation-Demo</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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-web</artifactId>
        </dependency>
        <!-- SpringBoot 整合Mybatis依赖包 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- Mysql 驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


</project>

关键依赖

 <!-- SpringBoot 整合Mybatis依赖包 -->
 <dependency>
	 <groupId>org.mybatis.spring.boot</groupId>
	 <artifactId>mybatis-spring-boot-starter</artifactId>
	 <version>1.3.2</version>
 </dependency>
 <!-- Mysql 驱动包 -->
 <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.39</version>
 </dependency>

数据库准备

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(50) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '张三', '21');
INSERT INTO `user` VALUES ('2', '李四', '23');
INSERT INTO `user` VALUES ('3', '王二', '22');

包结构

实体类

package com.perfree.mybatisannotation.pojo;

/**
 * @ClassName User
 * @Description TODO(User实体类)
 * @Author Perfree
 * @Date 2018/10/3 7:13
 * @Version 1.0
 */
public class User {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

Controller

package com.perfree.mybatisannotation.controller;

import com.perfree.mybatisannotation.pojo.User;
import com.perfree.mybatisannotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description TODO(userController)
 * @Author Perfree
 * @Date 2018/10/3 8:41
 * @Version 1.0
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public String getUserById(){
        User user = userService.getUserById(2);
        return user.getName();
    }
}

Service接口

package com.perfree.mybatisannotation.service;

import com.perfree.mybatisannotation.pojo.User;
/**
 * @ClassName UserService
 * @Description TODO(user Service接口)
 * @Author Perfree
 * @Date 2018/10/3 8:42
 * @Version 1.0
 */
public interface UserService {
    User getUserById(int id);
}

Service impl实现类

package com.perfree.mybatisannotation.service.impl;

import com.perfree.mybatisannotation.mapper.UserMapper;
import com.perfree.mybatisannotation.pojo.User;
import com.perfree.mybatisannotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserServiceImpl
 * @Description TODO(user Service 实现)
 * @Author Perfree
 * @Date 2018/10/3 8:43
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(int id) {
        User user = userMapper.getUserById(id);
        return user;
    }
}

Mapper接口

package com.perfree.mybatisannotation.mapper;

import com.perfree.mybatisannotation.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {

    @Select("select id,name,age from user where id=#{id}")
    User getUserById(int id);
}

application.properties

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

主程序

package com.perfree.mybatisannotation;

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

@MapperScan("com.perfree.mybatisannotation.mapper.UserMapper")
@SpringBootApplication
public class MybatisAnnotationApplication {

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

运行

简单的整合完毕,更多mybatis注解可以看org.apache.ibatis.annotations 包提供的,如图:

上一篇 下一篇
评论
来首音乐
最新回复
光阴似箭
今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月