Hello SpringBoot And Mybatis(SpringBoot入门搭建)

125 阅读2分钟

很久以前整合过一次SpringBoot和Mybatis,不过那个时候xml配置Mybatis仍是主流,今天心血来潮,通过注解来整合下,网上找资料发现论坛真的是特别乱,各种抄袭,有些甚至抄袭都不完整,哎,只能发个感慨,然后现在成功了也发个博客记录下吧。

下面开始搭建Spring boot+Mybatis

1.新建项目

填好目录和项目名

暂时勾选web就好了,next

finish

一个基础的骨架就出来,完善下目录结构

 

在pom.xml文件中加入相关依赖

<?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.will</groupId>
	<artifactId>first-boot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>first-boot</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.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>
		<mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version>
		<mysql-connector.version>5.1.39</mysql-connector.version>
		<java.version>1.8</java.version>
	</properties>

	<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>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-spring-boot.version}</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector.version}</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

建好数据库,分别是建一个user表,加id,name,age三个属性

配置下核心文件application.properties

spring.datasource.url

spring.datasource.username

spring.datasource.password

mybatis.typeAliasesPackage

改成自己的就行了,其余属性可以自行去脑补

server.port=8080
#视图层控制 用mvc方式访问templates下的HTML
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf这样配置就可以直接访问static下的HTML(和mvc访问方式二选一)
spring.thymeleaf.prefix = classpath:/static/
spring.thymeleaf.suffix = .html

spring.datasource.url=jdbc:mysql://localhost:3306/mymessage?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.typeAliasesPackage=com.will.firstboot.entity

在包entity下创建一个实体类user(entity——》user)

package com.will.firstboot.entity;

public class User {
    private Integer id;

    private String name;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

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

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

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

mapper——》UserMapper

package com.will.firstboot.mapper;
import com.will.firstboot.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

    @Repository
    public interface UserMapper {

        @Select("SELECT * FROM user WHERE id = #{id}")
        User selectUser(int id);

    }

service——》UserService

package com.will.firstboot.service;

import com.will.firstboot.entity.User;
import com.will.firstboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User UserService(int id){
        return userMapper.selectUser(id);
    }
}

controller——》UserController

package com.will.firstboot.controller;

import com.will.firstboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/showUser/{id}")
    public String selectUser(@PathVariable int id){
        return userService.UserService(id).toString();
    }
}

 

直接去自动生成的启动类启动就好了,我这里叫做FirstBootApplication。。。。。。。

 

=============分割=======================

你是不是会发现项目启动不起来,会报错Field userMapper in com.will.firstboot.service.UserService required a bean of type 'com.will.firstboot.mapper.UserMapper' that could not be found.

哈哈哈哈,我就是这样报错的,报错的意思是找不到mapper,加个@MapperScan("com.will.firstboot.mapper")注解就好了,反正我看到很多教程各种少注解,启动类如下

package com.will.firstboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@MapperScan("com.will.firstboot.mapper")
public class FirstBootApplication {
	public static void main(String[] args) {

		SpringApplication.run(FirstBootApplication.class, args);


	}
}

 

好了,我们启动访问下http://localhost:8080/showUser/1    ok了

 

 

有问题欢迎指正,感谢各位。

-------------------------------------------------------------------------2019-04-12分割线--------------------------------------------------------------

假如有出现mysql相关的警告,可以将mysql jar包版本改为 5.1.35