使用idea搭建springboot+mybatis一個簡單的工程

廢話少說直接使用idea搭建一個springboot+mybatis的工程html

1.搭建工程按照圖片一步一步next就能夠了,中間須要選着的地方會截圖java

在最後完成簡單工程的搭建以後先完善下工程的目錄結構:mysql

 

 在pom文件中添加依賴:web

<?xml version="1.0" encoding="UTF-8"?>
<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>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.springboot.demo</groupId>
   <artifactId>springboot-demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springboot-demo</name>
   <packaging>war</packaging>
   <description>Demo project for Spring Boot</description>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <mybatis-spring-boot.version>2.0.0</mybatis-spring-boot.version>
      <mysql-connector.version>5.1.24</mysql-connector.version>
      <java.version>1.8</java.version>
      <fastjson.version>1.2.56</fastjson.version>
   </properties>

   <dependencies>
      <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-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>${mybatis-spring-boot.version}</version>
      </dependency>
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>${mysql-connector.version}</version>
      </dependency>
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>${fastjson.version}</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

 


玩springboot的都知道最重要的一個配置文件就是application.properties
server.port=18080
#數據庫連接配置信息
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/spring-boot?useUnicode=true&characterEncoding=utf8
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.username = root
spring.datasource.password = tangbing

##視圖層控制 用mvc方式訪問tempaltes下面的html文件
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**

##thymeleaf這樣配置就能夠直接訪問static下的HTML(和mvc訪問方式二選一)
#spring.thymeleaf.prefix = classpath:/static/, classpath:/tempaltes/
#spring.thymeleaf.suffix = .html

spring.thymeleaf.mode = HTML5
spring.thymeleaf.encoding = UTF-8
spring.thymeleaf.servlet.content-type = text/html

#緩存 (開發是關閉,。。。。。)
spring.thymeleaf.cache = false

#mybatis配置
mybatis.configuration.use-generated-keys= true
# 不配置找不到mapper對用的xml
mybatis.mapper-locations=classpath:com/springboot/demo/MET-INFO/mybatis-mapper/*.xml

接下來就作下測試spring

/**
 * 文件名: testController.java
 * 版    權: Copyright © 2013 - 2019 CFNE, Inc. All Rights Reserved
 * 描    述: &lt;描述&gt;
 * 修改人: BING.TANG
 * 修改時間: 2019/3/7
 * 跟蹤單號: &lt;跟蹤單號&gt;
 * 修改單號: &lt;修改單號&gt;
 * 修改內容: &lt;修改內容&gt;
 */
package com.springboot.demo.contorller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.demo.api.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 功能描述 :
 * @author BING.TANG
 * @version [版本號,  2019/3/7]
 * @see [相關類/方法]
 * @since [產品/模板版本號]
 */
@Controller
public class testController
{
    @Resource(name = IUserService.BEAN_ID)
    private IUserService userService;

    @RequestMapping("/index")
    public String index()
    {
        return "index";
    }

    @RequestMapping("/queryUserById/{id}")
    @ResponseBody
    public JSONObject queryUserById(@PathVariable String id){
        return this.userService.queryUserById(id);
    }

}

這裏說下想訪問templates下面的靜態資源文件的話不能用@RestController註解,由於RestController = Controller + ResponseBody,這樣就會直接返回數據。sql

service的實現類:數據庫

/**
 * 文件名: UserServiceImpl.java
 * 版    權: Copyright © 2013 - 2019 CFNE, Inc. All Rights Reserved
 * 描    述: &lt;描述&gt;
 * 修改人: BING.TANG
 * 修改時間: 2019/3/7
 * 跟蹤單號: &lt;跟蹤單號&gt;
 * 修改單號: &lt;修改單號&gt;
 * 修改內容: &lt;修改內容&gt;
 */
package com.springboot.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.springboot.demo.api.domain.User;
import com.springboot.demo.api.service.IUserService;
import com.springboot.demo.service.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 功能描述 :
 * @author BING.TANG
 * @version [版本號,  2019/3/7]
 * @see [相關類/方法]
 * @since [產品/模板版本號]
 */
@Service(IUserService.BEAN_ID)
public class UserServiceImpl implements IUserService
{
    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject queryUserById(String id)
    {
        try
        {
            User user = this.userMapper.selectUser(id);
            return JSONObject.parseObject(JSONObject.toJSON(user).toString());
        } catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}

mapper文件apache

/**
 * 文件名: UserMapper.java
 * 版    權: Copyright © 2013 - 2019 CFNE, Inc. All Rights Reserved
 * 描    述: &lt;描述&gt;
 * 修改人: BING.TANG
 * 修改時間: 2019/3/7
 * 跟蹤單號: &lt;跟蹤單號&gt;
 * 修改單號: &lt;修改單號&gt;
 * 修改內容: &lt;修改內容&gt;
 */
package com.springboot.demo.service.mapper;

import com.springboot.demo.api.domain.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

/**
 * 功能描述 :
 * @author BING.TANG
 * @version [版本號,  2019/3/7]
 * @see [相關類/方法]
 * @since [產品/模板版本號]
 */
@Repository
public interface UserMapper
{

    User selectUser(@Param("id") String id);
}

代碼寫到這裏也能夠作下簡單的測試了:訪問controller的indexjson

 

最終返回的是templates下面的index頁面。而訪問/queryUserById/{id}返回的就是個json數據

 

到這裏第一個簡單的springboot+mybatis工程就算搭建成功了
相關文章
相關標籤/搜索