Springboot和Mybatis整合

1.建立Maven工程

2.建立appliaction.properties

  加入mybatis-config.xml配置及掃描mapper.xml文件夾配置

3.把mybatis工程中寫好的mappe接口,mapper的xml文件,mybatis-config.xml以及實體類拷貝過來

4.Pom文件修改

    1)log4j的依賴只留Log4j其餘兩個刪除

    2)加入sprongBoot以及mybatis-Springboot依賴包

 3)mybatis-java驅動確認

    2)要加上紅框的内容

      不然啓動時會出現Unsatisfied dependency expressed through bean property 'sqlSessionFactory'錯誤



5.編寫啓動類

---------------------------------------------------------------

package mybatis.simple;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan(basePackages = {"mybatis.simple.mapperInterface"})

public class MybatisSpringbootApp {

public static void main(String[] args) {

SpringApplication.run(MybatisSpringbootApp.class, args);

}

}

---------------------------------------------------------------

6.編寫Controler

----------------------------------------------------------------

package mybatis.simple.controller;

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

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

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

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

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

import mybatis.simple.mapper.UserMapper;

import mybatis.simple.model.SysUser;

import java.util.List;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

@RestController

@RequestMapping("/")

public class UserController {

    @Resource

    private UserMapper userMapper;

    @GetMapping("getOne")

    public SysUser selectByUserId(@RequestParam(value = "id") Long id){

        return userMapper.selectById(id);

    }

    @GetMapping("getAll")

    public List selectByUserId(){

        return userMapper.selectAll();

    }

@RequestMapping(value = "/deleteById")

@ResponseBody

public List getBookDetails(HttpServletRequest request) {

String userId = request.getParameter("id");

SysUser sysUser = new SysUser();

sysUser.setId(Long.valueOf(userId));

userMapper.deleteById(sysUser);

return userMapper.selectAll();

}

}

------------------------------------------------------------

Pom.xml文件内容

--------------------------------------------------------------------------------------------------

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">

4.0.0

com.accenture

mybatisSpringboot

0.0.1-SNAPSHOT

myBatis和springboot整合

myBatis和springboot整合

UTF-8

org.springframework.boot

spring-boot-starter-parent

2.0.5.RELEASE

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-devtools

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.2.0

junit

junit

4.12

test

mysql

mysql-connector-java

8.0.11

log4j

log4j

1.2.17

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.8

1.8

src/main/resources

**/*.properties

**/*.yml

**/*.xml

false

-----------------------------------------------------------------------------------------------------------

mybatis-config.xml配置内容

----------------------------------------------------------------------------------------------------------------

    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-config.dtd">



   

   

       

   


   

   

       

   


   

   

   

   

-------------------------------------------------------------------------------------

application.properties配置内容

------------------------------------------------------------------------------

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true

spring.datasource.username=root

spring.datasource.password=123

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

mybatis.config-location=classpath:mybatis-config.xml

mybatis.mapperLocations=classpath:mybatis/simple/mapper/*.xml

#mybatis.typeAliasesPackage=mybatis.simple.model

---------------------------------------------------------------------------------------

你可能感兴趣的:(Springboot和Mybatis整合)