本篇博客是參照:http://blog.csdn.net/forezp/article/details/70991675完成的姑且算是學(xué)習(xí)筆記吧。
直接切入主題虚吟,SpringBoot的目的就是為了加快開(kāi)發(fā)速度和簡(jiǎn)化開(kāi)發(fā)流程宋雏,省去了xml文件的配置環(huán)節(jié)懈凹。本篇只是描述SpringBoot和mybatis的整合瘾带,至于SpringBoot的其他知識(shí)點(diǎn),請(qǐng)自行百度丧肴。
項(xiàng)目是運(yùn)行在maven環(huán)境下的所以你必須先有
eclipse
maven 3.5 環(huán)境
所有的行為都必須有jar包的支持才行残揉,而maven項(xiàng)目只需要引入相應(yīng)的依賴(lài)到pom.xml文件中即可
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyProject</groupId>
<artifactId>LearnBoot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>LearnBoot Maven Webapp</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom文件引入mybatis-spring-boot-starter的依賴(lài):
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
引入數(shù)據(jù)庫(kù)連接依賴(lài):
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
application.properties配置文件中引入數(shù)據(jù)源:
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
創(chuàng)建數(shù)據(jù)表的建表語(yǔ)句:
-- 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');
具體的實(shí)現(xiàn)
創(chuàng)建實(shí)體類(lèi):
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語(yǔ)句省略請(qǐng)自行實(shí)現(xiàn)
}
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<Account> 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<Account> findAccountList(){
return accountMapper.findAccountList();
}
}
controller層,構(gòu)建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<Account> 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";
}
}
}
實(shí)現(xiàn)結(jié)果
實(shí)現(xiàn)結(jié)果
項(xiàng)目的目錄結(jié)構(gòu)
備注:其中紅線框中的本次改動(dòng)過(guò)的部分
值得注意的是SpringBoot 啟動(dòng)的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目錄下面才可以運(yùn)行芋浮。
運(yùn)行方式是將LearnBoot.java 以java application的方式運(yùn)行抱环。
之后在瀏覽器地址欄輸入相應(yīng)的@RequestMapping 路徑即可。