SpringBoot+mybatis搭建

首先:项目结构图

 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>SpringBoot</groupId>
  <artifactId>SpringBoot</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>


  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.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.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>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>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>

  </dependencies>

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

</project>

 application.properties配置文件:





 # MYSQ 相关配置
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 spring.datasource.url=jdbc:mysql://localhost:3306/bb?useUnicode=true&amp;characterEncoding=UTF-8
 spring.datasource.username=root
 spring.datasource.password=root
 spring.datasource.max-active=20
 spring.datasource.max-idle=8
 spring.datasource.min-idle=8
 spring.datasource.initial-size=10

 # Spring JPA 相关配置
 spring.jpa.show-sql=true
 spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true
 spring.jpa.hibernate.ddl-auto=update
 spring.jpa.hibernate.generate-ddl=true
 spring.jpa.hibernate.open-in-view=true

mybatis.mapperLocations=classpath:mapper/*Mapper.xml
mybatis.typeAliasesPackage=com.my.bean

 springboot启动类:

package com.my;

import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * Created by Zhang on 2018/8/4.
 * springboot启动类
 */
@SpringBootApplication
@MapperScan("com.my.mapper") // mybatis扫描路径,针对的是接口Mapper类
public class Application {

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

 需要注意的地方:

serviceimpl实现类中 一定要有@Service注解

package com.my.serviceimpl;

import com.my.bean.User;
import com.my.mapper.UserMapper;
import com.my.service.UserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Zhang on 2018/8/4.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> login() {
        System.err.print("1234");
        userMapper.insert();
        return userMapper.selectAll();
    }
}

 控制层:

package com.my.controller;

import com.my.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Zhang on 2018/8/4.
 *
 */
@Controller
@RequestMapping("/server/user")
public class UserController {
@Autowired
UserService userService;
    @RequestMapping("/login")
    @ResponseBody
    public  String login(){
        return  userService.login().toString();
    }


}

猜你喜欢

转载自blog.csdn.net/qq_40100214/article/details/81412288