本篇博客是参照:http://blog.csdn.net/forezp/article/details/70991675完成的姑且算是学习笔记吧。
直接切入主题,SpringBoot的目的就是为了加快开发速度和简化开发流程,省去了xml文件的配置环节。本篇只是描述SpringBoot和mybatis的整合,至于SpringBoot的其他知识点,请自行百度。
项目是运行在maven环境下的所以你必须先有
eclipse
maven 3.5 环境
所有的行为都必须有jar包的支持才行,而maven项目只需要引入相应的依赖到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的依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</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>
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<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层,构建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";
}
}
}
实现结果
实现结果 项目的目录结构备注:其中红线框中的本次改动过的部分
值得注意的是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 路径即可。
网友评论