SpringBoot+Mybatis整合

在最简洁的Spring Boot Demo文章基础上进行SpringBoot和Mybatis整合Demo,仅供学习交流
开发工具及环境:
Eclipse
JDK 1.8
Spring Boot 2.0.2


直接上代码

MainAPP
package pro.demo.SpringBootdemo.MainAPP;

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

@SpringBootApplication
@ComponentScan("pro.demo")
@MapperScan("pro.demo.SpringBootdemo.Mapper")
public class SpringBootdemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootdemoApplication.class, args);
	}
}
UserController
package pro.demo.SpringBootdemo.controller;

import java.util.List;

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;

import pro.demo.SpringBootdemo.model.User;
import pro.demo.SpringBootdemo.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	@RequestMapping("/getalluser")
	@ResponseBody
	public List<User> getAllUser(){
		
		List<User> list = userService.getAllUser();
		
		return list;
	}
}

UserService
package pro.demo.SpringBootdemo.service;

import java.util.List;

import pro.demo.SpringBootdemo.model.User;

public interface UserService {

	List<User> getAllUser();

}
UserServiceImpl
package pro.demo.SpringBootdemo.service.impl;

import java.util.ArrayList;
import java.util.List;

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

import pro.demo.SpringBootdemo.Mapper.UserMapper;
import pro.demo.SpringBootdemo.model.User;
import pro.demo.SpringBootdemo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public List<User> getAllUser() {
		
		return userMapper.getAllUser();
	}

}

UserMapper
package pro.demo.SpringBootdemo.Mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import pro.demo.SpringBootdemo.model.User;

public interface UserMapper {

	@Select("select * from test4")
	List<User> getAllUser();

}
UserModel
package pro.demo.SpringBootdemo.model;

public class User {

	/**
	 * 属性
	 * */
	private Integer id;
	
	private String name;
	
	private Integer age;

	/**
	 * get 和 set 方法
	 * */
	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;
	}
	
	

}

配置文件:application.properties
请修改数据库的链接、用户名和密码
server.port=8081

# mysql
spring.datasource.url=jdbc:mysql://***.***.***.***:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
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>pro.demo</groupId>
	<artifactId>SpringBootDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBootdemo</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>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- web  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<!-- mybatis -->
		<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>1.2.2</version>
		</dependency>
		
		 <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
	</dependencies>

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

第二版 SpringBoot整合Mybatis:
SpringBoot 整合 Mybatis
pom.xml中需要加入
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
mapper可以通过两种方式来进行操作数据库
1、通过注解sql进行数据库操作
@Select("select * from test4")
List<User> getAllUser();
2、通过以前的mapper.xml 写sql进行操作数据库
SpringBootdemoApplication
添加@MapperScan("pro.demo.SpringBootdemo.Mapper")扫描注解




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值