Myeclipse下搭建Spring Boot+Mybatis

1.进入命令行,切换到目标文件夹下,注意:此步不用创建项目的文件夹。

2. 在命令行输入:mvn  archetype:generate  -DgroupId=公司名或公司域名  -DartifactId=项目名称   -DarchetypeArtifactId=maven-archetype-webapp   -DinteractivMode=false      -DarchetypeCatalog=internal,出现下图说明创建成功,文件夹内会出现项目文件夹

3.在springboot(项目名称,对应-DartifactId)\src\main下创建“java”文件夹。

4.运行Myeclipse,点击菜单栏File-》Import-》,选择“Existing Maven Projects”



点击“Finish”

5.对项目名称点右键-》Properties-》Java Build Path,选择Libraries选项卡:

a.Add Library -》JRE System Library,添加系统的JRE版本,这里用的是Spring 4.X,需要使用1.6以上的JRE版本。

b.Add Library -》MyEclipse Server Library,选择系统中安装的Tomcat8

6.双击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>zwhl</groupId>
  <artifactId>springboot</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springboot Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.9.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.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.mybatis.spring.boot</groupId>
	    <artifactId>mybatis-spring-boot-starter</artifactId>
	    <version>1.3.1</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</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-starter-jdbc</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-tomcat</artifactId>
       <scope>provided</scope>
   </dependency>
    <exclusions>
	<exclusion>
		<artifactId>log4j-over-slf4j</artifactId>
		<groupId>org.slf4j</groupId>
		</exclusion>
		<exclusion>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
	<!-- mysql连接 -->    
     <dependency>    
         <groupId>mysql</groupId>    
         <artifactId>mysql-connector-java</artifactId>    
         <version>5.1.42</version>    
     </dependency> 
	<!-- mybatis -->
  </dependencies>
  <build>
    <finalName>springboot</finalName>
  </build>
</project>

注意:如果采用默认方式对着Application点击右键Run as Java Application启动(该方式访问无需加项目名,直接http://127.0.0.1:8080/home),则不需加exclusions节点中的内容。

7.在src/main下创建包“com.Application",并在该包下创建Application.java:

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.model.User;
import com.service.UserService;

@RestController  
public class CenterController {
	
	@RequestMapping("/home")  
    String home() {  
        return "Hello World!this ChilrenStudy";  
    }  
	
	@Autowired
    private UserService userService;
    
    @RequestMapping("/list1")
    @ResponseBody
    public List<User> list1(){
    	System.out.print("ok");
    	List<User> userList=userService.queryList();
    	return userList;
    }
}

8.在src/main/resources下创建“application.properties”:

mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.model

#mysql
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/数据库名称
spring.datasource.username=root
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

9.在src/main下创建包“com.model",并在该包下创建User.java:

package com.model;

public class User {

	private Integer id;
    private String name;
    
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

10.在src/main下创建包“com.dao",并在该包下创建UserMapper.java:

package com.dao;

import java.util.List;

import com.model.User;

public interface UserMapper {
	List<User> queryList();  
}

11.在src/main下创建包“com.service",并在该包下创建UserService.java:

package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.UserMapper;
import com.model.User;

@Service
public class UserService {

	@Autowired
    private UserMapper userMapper;

	public List<User> queryList() {
        return userMapper.queryList();
    }
}

12.在src/main下创建包“com.controller",并在该包下创建CenterController.java:

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.model.User;
import com.service.UserService;

@RestController  
public class CenterController {
	
	@RequestMapping("/home")  
    String home() {  
        return "Hello World!this CenterController";  
    }  
	
	@Autowired
    private UserService userService;
    
    @RequestMapping("/list1")
    @ResponseBody
    public List<User> list1(){
    	System.out.print("ok");
    	List<User> userList=userService.queryList();
    	return userList;
    }
}

13.在src/main/resources下创建文件夹“mybatis”,并在该文件夹下创建UserMapper.xml:、

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.UserMapper">
  <select id="queryList" resultType="com.model.User">  
        SELECT * FROM Users 
    </select>  
</mapper>
14. 对项目名称点右键,选择Maven4Myeclipse-》Update Project,下载Jar包。

15.将该项目发布至Tomcat,启动后按照http://127.0.0.1:8080/项目名称/访问即可。本例中写了

http://127.0.0.1:8080/springboot/list1和http://127.0.0.1:8080/springboot/home,访问正常显示即表明搭建成功!








猜你喜欢

转载自blog.csdn.net/woai671/article/details/79093414