《Java從入門到放棄》框架入門篇:SpringBoot+mybatis搭建三層架構項目

本篇主要講解基於SpringBoot+mybatis實現三層架構項目。

三層架構主要包括:表示層,業務層,數據訪問層,還有實體類層。

早期的SSM框架中,mybatis的使用比較麻煩,需要配置自己的XML文件,DAO層通過接口映射XML配置等。雖然開發方提供了generator可以自動生成實體類和DAO文件,但沒有在根本上改變配置上的複雜度。

但隨着springboot的出現,springboot+mybatis可以完全註解不用配置文件,也可以輕鬆上手寫代碼了。這種方式等下篇文章再做介紹,本篇主要是把SSM項目改版成Springboot+mybatis項目。

OK,在前一篇文章的基礎上繼續。

第一步、修改pom.xml文件,添加mysql數據庫和mybatis相關。

<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <groupId>com.pxy</groupId>
    <artifactId>BlogManager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <!-- 設置字符集 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 設置Java版本 -->
        <java.version>1.7</java.version>
    </properties>
    <dependencies>
        <!-- 對Web開發的支持,包含tomcat等Web開發特性 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 使用MySql數據庫 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 引入mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- DevTools in Spring Boot 項目熱部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 如果直接Main啓動spring,以下plugin必須添加。-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

第二步、在src/main/java中添加以下三層相關包。

com.pxy.controller:控制器

com.pxy.dao:數據訪問層

com.pxy.entity:實體類

com.pxy.service:業務層

com.pxy.test:啓動APP

第三步、使用generator生成實體類和DAO層,並複製到項目中。

第四步、在src/main/resources下添加application.properties或application.yml文件對APP進行配置,spring.datasource配置好後,SqlSesisonFactory、Mapper等都會自動注入。

application.properties寫法:

spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blogdb?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

mybatis.type-aliases-package=com.pxy.entity
mybatis.mapperLocations=classpath:com.pxy.dao/*.xml

application.yml寫法(注意縮進):

spring:   
    devtools:
        restart:
            enabled: true   
            additional-paths: src/main/java  
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/blogdb?characterEncoding=UTF-8
        username: root
        password: root

mybatis:
    type-aliases-package: com.pxy.entity
    mapperLocations: classpath:com.pxy.dao/*.xml

推薦使用application.yml的寫法。

第五步、編寫Service層代碼、Controller代碼和啓動APP類代碼。

Service代碼:

@Service

@Service
public class AuthorService {
	@Autowired
	private AuthorMapper authorMapper;
	
	public Author findById(int id){
		return authorMapper.selectByPrimaryKey(id);
	}
}

Controller代碼:

//spring4裏的註解,@RestController是@ResponseBody和@Controller的縮寫
@RestController
public class AuthorController {
	@Autowired
	private AuthorService authorService;
	
	@RequestMapping("/hello")
	public String hello(){
		Author author = authorService.findById(1);
		return author.getUsername();
	}
}

App代碼:

@SpringBootApplication(scanBasePackages={"com.pxy.*"})    //表示掃描com.pxy下所有包
@MapperScan("com.pxy.dao")	//表示對mapper掃描
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(App.class, args);
	}
}

第六步、啓動項目,並訪問localhost:8080/hello。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章