spring boot项目搭建示例①

所用技术:JDK1.7、Mysql、spring boot、Logback、Mybatis

开发工具:Myeclipse、Maven

1.数据库创建:

CREATE TABLE `userinfo` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

2.创建Maven工程

步骤:

①、Ctrl+N-->选择Maven Project

spring boot项目搭建示例①_第1张图片

②、勾选Create a simple project (skip archetype selection)-->点击Next

spring boot项目搭建示例①_第2张图片

③、输入Group Id、Artifact Id,选择packaging为war,点击Finish

spring boot项目搭建示例①_第3张图片

项目结构如下:

spring boot项目搭建示例①_第4张图片

3、导入spring boot所需jar包

pom.xml:


  4.0.0
  com.hu
  SpringBootPro
  0.0.1-SNAPSHOT
  war
  
  
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.9.RELEASE
		pom
	
	
		org.springframework.boot
		spring-boot-starter-web
		1.5.9.RELEASE
	
	
        org.springframework.boot
        spring-boot-starter-test
        1.5.9.RELEASE
        test
    
  
  

4.增加application.properties属性文件

在src/main/resources下新建文件

application.properties:

server.port=8010
server.contextPath=/springboot

5、增加启动类

MainServer.java:

package com.hu.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainServer {
	public static void main(String[] args) {
		SpringApplication.run(MainServer.class, args);
	}
}

项目结构如下:

spring boot项目搭建示例①_第5张图片

6、启动测试

现在可以检查一下是否搭建成功了,在MainServer中右键-->run as/debug as -->java application

spring boot项目搭建示例①_第6张图片

控制台出现如下便是成功了:

spring boot项目搭建示例①_第7张图片

7.编写第一个测试接口

①、将MainServer.java申明当前为Controller

②、添加测试接口地址

③、启动main方法,前端访问

代码如下:

package com.hu.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication
public class MainServer {
	public static void main(String[] args) {
		SpringApplication.run(MainServer.class, args);
	}
	
	@RequestMapping(value = "/hello")
	public @ResponseBody String hello(){
		return "Hello Boot";
	}
}

前端访问接口:

spring boot项目搭建示例①_第8张图片

8、整合日志框架

①、导入日志jar包pom.xml下增加:


		org.springframework.boot
		spring-boot-starter-logging
		1.5.9.RELEASE
	

②、增加logback.xml配置文件,放到resource目录下



    
    
    
    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    
    
    
        
            
     ${LOG_HOME}/TestSpringBoot_info.log.%d{yyyy-MM-dd}.log
            
            30
        
        
            info
            ACCEPT
            DENY
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
        
        
            10MB
        
    

    
        
            
 ${LOG_HOME}/TestSpringBoot_error.log.%d{yyyy-MM-dd}.log
            
            30
        
        
            error
            ACCEPT
            DENY
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
        
        
            10MB
        
    

    
    
        
        
        
    

代码引用:

spring boot项目搭建示例①_第9张图片

9、整合Mybatis

①、导入Mybatis和Mysql的jar包


		org.mybatis.spring.boot
		mybatis-spring-boot-starter
		1.3.1
	
	
		mysql
		mysql-connector-java
		5.1.30
	

②、在application.properties文件中配置数据库连接信息

server.port=8010
server.contextPath=/springboot
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

③、新建Controller、entity、service、dao包

spring boot项目搭建示例①_第10张图片

④、添加接口、实现类、实体、sql语句

User.java:

package com.hu.entity;

public class User{
	private int id;
	private String username;
	private String password;
	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 String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", username=" + username + ", password="
				+ password + "]";
	}
	
	
}

UserDao.java:

package com.hu.dao;

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 com.hu.entity.User;
@Mapper
public interface UserDao {
	 @Select("select * from userinfo where username = #{username}")
	 User findByName(@Param("username") String username);
	 @Insert("insert into userinfo (username, password) values(#{username}, #{password})")
	 int insert(@Param("username") String name, @Param("password") String password);
}

UserService.java:

package com.hu.service;

import org.springframework.stereotype.Service;

import com.hu.entity.User;

@Service("userService")
public interface UserService {
	//查询
	 public User findByname(String username);
	 //增加
	 public int insertUser(String username,String password);
}

UserServiceImpl.java:

package com.hu.service.impl;

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

import com.hu.dao.UserDao;
import com.hu.entity.User;
import com.hu.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	UserDao userDao;
	
	@Override
	public User findByname(String username) {
		// TODO Auto-generated method stub
		User userinfo = userDao.findByName(username);
		return userinfo;
	}

	@Override
	public int insertUser(String username, String password) {
		// TODO Auto-generated method stub
		return userDao.insert(username, password);
	}

}

UserController.java:

package com.hu.Controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hu.entity.User;
import com.hu.service.UserService;
@Controller
public class UserController {
	private final Logger log = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/add",method = RequestMethod.GET)
	public @ResponseBody String addUser() {
		userService.insertUser("xiaoming","111111");
        return "插入成功";
    }
	@RequestMapping(value="/find",method=RequestMethod.GET)
	public @ResponseBody User queryUserByName(String username){
		User userinfo = userService.findByname(username);
		return userinfo;
	}
}

MainServer.java:

package com.hu.server;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages={"com.hu"})
@MapperScan({"com.hu.dao"})
public class MainServer {
	private final Logger log = LoggerFactory.getLogger(MainServer.class);
	public static void main(String[] args) {
		SpringApplication.run(MainServer.class, args);
	}
	
}

10.测试接口数据插入及查询

①、启动主函数

②、测试接口

spring boot项目搭建示例①_第11张图片

spring boot项目搭建示例①_第12张图片


到这里基本上一个spring boot框架项目就搭建好了,只不过这个还不支持mybatis映射及jsp页面,下一篇我们讲解。

喜欢就请关注!!后续还有spring boot 打包及发布博文--------

spring boot打成jar包发布示例:《spring boot 项目打成jar包发布示例(一)》


项目源码:https://download.csdn.net/download/huxiangen/10530109



你可能感兴趣的:(spring,boot,spring,boot项目搭建,打包,部署)