SpringBoot入门——整合Mybatis

SpringBoot入门——整合Mybatis

        springboot整合mybatis,只需要简单的配置就能整合在一起,这里写了个查询和插入小例子来演示如何调用mybatis。

1、配置pom.xml

先配置pom.xml,来引用spring和mybatis相关的jar包

<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.springboot</groupId>
	<artifactId>spring-boot2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<!-- 管理jar包的版本号 -->
		<mybatis-spring.version>1.3.0</mybatis-spring.version>
		<mybatis-autoconfigure.version>1.3.0</mybatis-autoconfigure.version>
	</properties>

	<dependencies>
		<!-- web支持 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- Springboot devtools热部署 依赖包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>

		<!-- mybatis-spring依赖,包含mybatis和mybatis-spring等 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-spring.version}</version>
		</dependency>

		<!-- mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin </artifactId>
				<configuration>
					<!-- 如果没有该项配置,devtools不会起作用,即应用不会restart -->
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

2、创建mapper文件存放文件夹mapper

该文件夹用来放mybatis的mapper文件

3、创建mapper类及配置

(1)、创建mapper的接口类

有两种方式写Sql语句,一种是基于xml文件,另一种是基于注解,想详细了解请自行百度

包名:com.springboot.dao

类名:AdminDAO 

package com.springboot.dao;

import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface AdminDAO {

	// 基于Mapper.xml写sql语句
	public List<Map<String, Object>> selectAll();

	// 基于注解方式写sql语句
	@Insert("insert into account(name,money) values(#{name},#{money})")
	int addAdmin(@Param("name") String name, @Param("money") String money);

}

(2)、创建mapper的配置类

创建此配置类的作用是在以后分布式搭建的时候,可以扫描每块mapper的包的位置

并且在加上此配置类后mapper类不加@Mapper注解也可以,只需此配置类中设定好mapper的包的位置

包名:com.springboot.dao;

类名:MapperConfig

package com.springboot.dao;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
/**
 * 
 * 扫描本包的mapper
 *
 */
@Configuration
@MapperScan("com.springboot.dao")
public class MapperConfig {

}

4、创建实体类

包名:com.springboot.entity;

类名:Admin

package com.springboot.entity;

public class Admin {
	private Integer id;
	private String name;
	private String money;
	
	public Admin() {}

	public Admin(Integer id, String name, String money) {
		super();
		this.id = id;
		this.name = name;
		this.money = money;
	}

	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 String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}
	
}

5、配置application.properties

只需要简单的几个配置就能行,如果需要配置其他参数,请自行查找相关资料

# 数据库连接
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 连接池配置
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

# mybatis配置
# mybatis映射文件位置
mybatis.mapper-locations=classpath:/mapper/*Mapper.xml 
# 实体类entity包位置
mybatis.type-aliases-package=com.springboot.entity

6、编写Mapper文件

编写mapper.xml,放入刚才创建的mapper文件夹下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace必须指向Dao接口 -->
<mapper namespace="com.springboot.dao.AdminDAO">

    <!-- 根据ID查询数据 -->
    <select id="selectAll" resultType="map">
   		select
   		      *
   		from
   			account
    </select>

</mapper>

7、编写service

写对应的service业务处理层

(1)、service接口类:

包名:com.springboot.service

类名:AdminService

package com.springboot.service;

import java.util.List;
import java.util.Map;
//service接口类
public interface AdminService {
	
	public List<Map<String,Object>> findAll();
	
	public int addAdmin(String name,String money);
}

(2)、service实现类

包名:com.springboot.service.impl

类名:AdminServiceImpl

package com.springboot.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.dao.AdminDAO;
import com.springboot.service.AdminService;

@Service
//service接口的实现类
public class AdminServiceImpl implements AdminService{
	
	@Autowired
	private AdminDAO adminDAO;
	
    // 查询方法
	public List<Map<String, Object>> findAll() {
		return adminDAO.selectAll();
	}
    
    // 插入方法
	public int addAdmin(String name, String money) {
		return adminDAO.addAdmin(name, money);
	}

}

8、编写controller类

包名:com.springboot.controller

类名:AdminController

package com.springboot.controller;

import java.util.List;
import java.util.Map;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springboot.service.AdminService;

@Controller
public class AdminController {
	@Autowired
	private AdminService adminService;

    // 查询controller
	@RequestMapping("/admin")
	@ResponseBody
	public List<Map<String, Object>> test(){
		return adminService.findAll();
	}
	
    // 插入controller
	@RequestMapping("/addAdmin")
	@ResponseBody
	public int addAdmin(@RequestParam("name") String name,@RequestParam("money") String money){
		return adminService.addAdmin(name, money);
	}
	
}

9、编写Main类

用来启动springboot的main类

包名:com.springboot

类名:ApplicationMain

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties
public class ApplicationMain {

	public static void main(String[] args) {
		SpringApplication.run(ApplicationMain.class, args);
		System.out.println("---------------OK----------------");
	}

}

10、测试

测试1:

输入地址:http://localhost:8080/admin

如果显示一序列json信息则查询成功

测试2:

输入地址:http://localhost:8080/addAdmin?name=test&money=666

如果显示1,则代表插入一条记录成功

再输入http://localhost:8080/admin

来查询刚才插入的内容

转载于:https://my.oschina.net/u/3523885/blog/1499646

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值