Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。
springboot的概念:
從最根本上來講,Spring Boot就是一些庫的集合,它能夠被任意項目的構建系統所使用。簡便起見,該框架也提供了命令行界面,它可以用來運行和測試Boot應用。框架的發布版本,包括集成的CLI(命令行界面),可以在Spring倉庫中手動下載和安裝。
idea導入java項目后如何配置并運行、搭建springboot:
可以在官網https://start.spring.io/生成spring boot的模板。如下圖
idea配置springboot?
解壓之后idea導入這個項目就ok;
接入mybatis:
MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。
搭建一個springboot項目。在項目對象模型pom.xml中插入mybatis的配置
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency>
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.30</version></dependency>
創建數據庫以及user表:
CREATE TABLE `users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`username` varchar(255) NOT NULL,`age` int(10) NOT NULL,`phone` bigint NOT NULL,`email` varchar(255) NOT NULL,PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
分別創建三個包,分別是dao/pojo/service, 目錄如下
yml和properties優先哪個、
添加User:
package com.athm.pojo;/*** Created by toutou on 2018/9/15.*/
public class User {private int id;private String username;private Integer age;private Integer phone;private String email;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getPhone() {return phone;}public void setPhone(Integer phone) {this.phone = phone;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}
添加UserMapper:
package com.athm.dao;import com.athm.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;/*** Created by toutou on 2018/9/15.*/
@Mapper
public interface UserMapper {@Select("SELECT id,username,age,phone,email FROM USERS WHERE AGE=#{age}")List<User> getUser(int age);
}
添加UserService:
package com.athm.service;import com.athm.pojo.User;import java.util.List;/*** Created by toutou on 2018/9/15.*/
public interface UserService {List<User> getUser(int age);
}
Spring的配置文件?添加UserServiceImpl:
package com.athm.service;import com.athm.dao.UserMapper;
import com.athm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** Created by toutou on 2018/9/15.*/
@Service
public class UserServiceImpl implements UserService{@AutowiredUserMapper userMapper;@Overridepublic List<User> getUser(int age){return userMapper.getUser(age);}
}
controller添加API方法:
package com.athm.controller;import com.athm.pojo.User;
import com.athm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** Created by toutou on 2018/9/15.*/
@RestController
public class IndexController {@AutowiredUserService userService;@GetMapping("/show")public List<User> getUser(int age){return userService.getUser(age);}@RequestMapping("/index")public Map<String, String> Index(){Map map = new HashMap<String, String>();map.put("北京","北方城市");map.put("深圳","南方城市");return map;}
}
修改租車ZucheApplication:
package com.athm.zuche;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
@ComponentScan(basePackages = {"com.athm.controller","com.athm.service"})
@MapperScan(basePackages = {"com.athm.dao"})
public class ZucheApplication {public static void main(String[] args) {SpringApplication.run(ZucheApplication.class, args);}
}
添加數據庫連接相關配置,application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/zuche
spring.datasource.username=toutou
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
如何用intellij idea寫java?application:
package com.htt.taotao;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@ComponentScan(basepackages={"com.taotao.cotroller","com.taotao.service"})
@MapperScan(basepackages={"com.taotao.dao"})
public class TaotaoApplication {public static void main(String[] args) {SpringApplication.run(TaotaoApplication.class, args);}
}
?
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态