springboot 整合mybatis

一、新建一個maven工程

image.png


二、在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.bt.com.cn</groupId>

  <artifactId>bt-springboot</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>bt-springboot</name>

  <description>bt-springboot</description>


<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.5.RELEASE</version>

</parent>


<!-- Add typical dependencies for a web application -->

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

</dependencies>


<!-- Package as an executable jar -->

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>



三、在src/main/resoures新建application.properties 文件,加入如下配置

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=asdf123

spring.datasource.driver-class-name=com.mysql.jdbc.Driver


四、數據庫中新建一張表

image.png


五、新建一個UserMapper文件

image.png


package com.batian.mapper;


import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.Param;


public interface UserMapper {

@Insert("insert into user values(#{id},#{username},#{age})")

public int insert(@Param("id") String id,@Param("username") String username,@Param("age") int age);

}



六、新建UserService及實現類UserServiceImpl

image.png


package com.batian.service;


public interface UserService {

public int insert(String id,String username,int age);

}





package com.batian.service.impl;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import com.batian.mapper.UserMapper;

import com.batian.service.UserService;


@Service

@Transactional

public class UserServiceImpl implements UserService{

@Autowired

private UserMapper userMapper;

public int insert(String id,String username,int age){

return userMapper.insert(id, username, age);

}

}



七、新建一個UserController

image.png


package com.batian.controller;


import org.apache.catalina.User;

import org.springframework.beans.factory.annotation.Autowired;

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

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


import com.batian.mapper.UserMapper;

import com.batian.service.UserService;


@RestController

public class UserController {

@Autowired

private UserService userService;

@RequestMapping("/insert")

public String insert(){

int result = userService.insert("1", "test", 18);

String msg = "插入成功";

if(result<=0){

msg = "插入失敗";

}

return msg;

}

}



八、編寫啓動類

image.png


package com.batian;


import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

@MapperScan("com.batian.mapper")

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}



注:@MapperScan這個註解必須加上


九、啓動類後,在瀏覽器中瀏覽

image.png


數據庫中查看

image.png



此時整合說明我們已經成功整合mybatis


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章