SpringBoot入门——整合Mybatis
2017-08-02 00:00
831 查看
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
来查询刚才插入的内容
相关文章推荐
- Spring boot入门,整合mybatis开发案例
- Spring boot入门五 spring mvc spring boot mybatis配置整合
- SpringBoot入门-快速整合Mybatis
- SpringBoot入门之整合mybatis
- Spring Boot整合mybatis全注解入门教程
- springboot整合Mybatis(一)——入门
- (七)springboot入门之整合Mybatis
- Spring boot入门五 spring mvc spring boot mybatis配置整合
- springboot+mybatis整合(入门)
- Spring boot整合mybatis
- spring-boot整合mybatis(使用Fiddler抓包工具进行测试)
- SpringBoot学习:整合MyBatis,使用Druid连接池
- Spring boot Mybatis 整合(完整版)
- SpringBoot学习-(六)SpringBoot与Mybatis整合
- SpringBoot整合Mybatis
- SpringBoot整合Mybatis提示SAXParseException: 前言中不允许有内容
- SpringBoot整合Mybatis
- spring-boot结合mysql整合mybatis
- (十一)Spring Boot整合Mybatis使用druid实现多数据源自动切换
- maven项目 spring-boot 整合 mybatis 实现查询功能demo