SpringBoot整合MyBatis

本篇博客是参照:http://blog.csdn.net/forezp/article/details/70991675完成的姑且算是学习笔记吧。
直接切入主题,SpringBoot的目的就是为了加快开发速度和简化开发流程,省去了xml文件的配置环节。本篇只是描述SpringBoot和mybatis的整合,至于SpringBoot的其他知识点,请自行百度。
项目是运行在maven环境下的所以你必须先有

eclipse
maven  3.5 环境

所有的行为都必须有jar包的支持才行,而maven项目只需要引入相应的依赖到pom.xml文件中即可
pom.xml


    4.0.0

    MyProject
    LearnBoot
    war
    0.0.1-SNAPSHOT

    LearnBoot Maven Webapp
    Demo project for Spring Boot


    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.7.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        


        
            mysql
            mysql-connector-java
            runtime
        
        
            com.alibaba
            druid
            1.0.29
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

pom文件引入mybatis-spring-boot-starter的依赖:

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        

引入数据库连接依赖:


            mysql
            mysql-connector-java
            runtime


            com.alibaba
            druid
            1.0.29

application.properties配置文件中引入数据源:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

创建数据表的建表语句:

-- create table `account`
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');
具体的实现

创建实体类:

package com.zhiyou100.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties
@Component
public class Account {

    private int id;
    private String name;
    private double money;
    
    get和set语句省略请自行实现

}
dao层:
/**
 * @Title: AccountMapper.java 
 * @Prject: LearnBoot
 * @Package: com.zhiyou100.dao 
 * @Description: TODO
 * @author: Liu Hao  
 * @date: 2017年10月10日 下午3:08:52 
 * @version: V1.0   
 */
package com.zhiyou100.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhiyou100.model.Account;

/** 
 * @ClassName: AccountMapper 
 * @Description: TODO
 * @author: Liu Hao
 * @date: 2017年10月10日 下午3:08:52  
 */
@Mapper
public interface AccountMapper {

    @Insert("insert into account(name,money) values (#{name} , #{money})")
    int add(@Param("name") String name,@Param("money") double money);
    
    @Update("update account set name=#{name},money=#{money} where id = #{id}")
    int update(@Param("name") String name,@Param("money") double money ,@Param("id") int id);
    
    @Delete("delete from account where id=#{id}")
    int delete(@Param("id") int id);
    
    @Select("select id, name as name, money as money from account where id =#{id}")
    Account findAccount(@Param("id") int id);
    
    @Select("select id, name as name, money as money from account")
    List findAccountList();
}
service层
/**
 * @Title: AccountService.java 
 * @Prject: LearnBoot
 * @Package: com.zhiyou100.service 
 * @Description: TODO
 * @author: Liu Hao  
 * @date: 2017年10月10日 下午3:43:56 
 * @version: V1.0   
 */
package com.zhiyou100.service;

import java.util.List;

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

import com.zhiyou100.dao.AccountMapper;
import com.zhiyou100.model.Account;

/** 
 * @ClassName: AccountService 
 * @Description: TODO
 * @author: Liu Hao
 * @date: 2017年10月10日 下午3:43:56  
 */
@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;
    
    public int add(String name , double money){
        return accountMapper.add(name, money);
    }
    
    public int update(String name , double money , int id){
        return accountMapper.update(name, money, id);
    }
    
    public int delete (int id){
        return accountMapper.delete(id);
    }
    
    public Account findAccount(int id){
        return accountMapper.findAccount(id);
    }
    
    public List findAccountList(){
        return accountMapper.findAccountList();
    }
}
controller层,构建restful API
/**
 * @Title: AccountController.java 
 * @Prject: LearnBoot
 * @Package: com.zhiyou100.controller 
 * @Description: TODO
 * @author: Liu Hao  
 * @date: 2017年10月10日 下午4:04:25 
 * @version: V1.0   
 */
package com.zhiyou100.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou100.model.Account;
import com.zhiyou100.service.AccountService;

/** 
 * @ClassName: AccountController 
 * @Description: TODO
 * @author: Liu Hao
 * @date: 2017年10月10日 下午4:04:25  
 */
@RestController
@RequestMapping("/account")
@EnableConfigurationProperties({Account.class})
/*@EnableAutoConfiguration*/
public class AccountController {

    
    @Autowired
    AccountService accountService;
    
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List getAccounts() {
        return accountService.findAccountList();
    }
    
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id) {
        return accountService.findAccount(id);
    }
    
    
    @RequestMapping(value = "/{id}/u", method = RequestMethod.GET)
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        int t= accountService.update(name,money,id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }
    }
    
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id")int id) {
        int t= accountService.delete(id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }
    
    
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {
       int t= accountService.add(name,money);
       if(t==1) {
           return "success";
       }else {
           return "fail";
       }
    }
}

实现结果

SpringBoot整合MyBatis_第1张图片
实现结果
SpringBoot整合MyBatis_第2张图片
项目的目录结构
备注:其中红线框中的本次改动过的部分

值得注意的是SpringBoot 启动的main方法文件:

package com.zhiyou100;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

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

该文件必须放置在com.zhiyou100目录下面才可以运行。
运行方式是将LearnBoot.java 以java application的方式运行。
之后在浏览器地址栏输入相应的@RequestMapping 路径即可。

你可能感兴趣的:(SpringBoot整合MyBatis)