使用Spring boot 快速创建项目

本文介绍了如何使用Spring Boot进行快速项目创建,强调了其整合框架以提高开发效率的特点。通过STS 4.0,安装Eclipse和STS后,详细步骤包括创建项目、构建项目结构、设置application.yml配置文件、利用mybatis-generator逆向生成数据库映射及PO类,最后展示了注解驱动的控制器编写。值得注意的是,Spring Boot项目无需额外配置Tomcat即可直接运行。

Spring boot 是整合了框架,以快速开发为目的,大大节省了开发效率。

以下是创建项目。利用STS 4.0软件使用时必须安装eclipse 和 sts 4.0 。方可打开sts4.0

创建好的项目然后建立项目结构

建立application.yml文件配置

server:
  port: 8888


spring:
  datasource:
    url: jdbc:sqlserver://10.10.10.2:1433;databaseName=scmis000
    username: sc
    password: redone
    dbcp2:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      initial-size: 5
      min-idle: 10
      max-wait-millis: 10000 
  

系统生成的spring boot 主类

package com.xukai;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;


//mapperscan扫描。这样的话就不用一个个配置了。
@MapperScan("com.xukai.mapper")
@SpringBootApplication
public class XmHelperApplication {

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

}

用mybatis-generator-core-1.3.7.jar 来逆向生成数据库mapper和po类

直接用注解的形式写代码。方便快捷。

package com.xukai.mapper;

import com.xukai.po.Personnel;
import java.util.List;
import org.apache.ibatis.annotations.Select;

public interface PersonnelMapper {
	 
	@Select("SELECT * FROM personnel WHERE PersonnelId = #{id}")
    Personnel getPersonnelById(String id);

	@Select("select * from personnel")
    List<Personnel> selectAll();

    @Select("select Password from OperPw where PersonId = #{id}")
    String getPWDByID(String id); 
    
    
}

这是控制器

package com.xukai.controller;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xukai.mapper.PersonnelMapper;
import com.xukai.po.Personnel;

//这个注解是不返回页面,而是返回json数据
@RestController
public class LoginController {
	
	@Autowired
	private PersonnelMapper personnelMapper;


	
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String helloWorld(@RequestBody Map<String, String> maps) throws JsonProcessingException, RemoteException {
		
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, String> map = new HashMap<>();
		String resultString;
		String username = maps.get("username");
		String password = maps.get("password");
		
		
		//查询是否已经关联
		
		
		
		Personnel personnel = personnelMapper.getPersonnelById(username);
		if (personnel == null) {
			map.put("success", "0");
			map.put("reason", "无此账号!");
			resultString = objectMapper.writeValueAsString(map);
		} else {
			String encryptPWD = personnelMapper.getPWDByID(personnel.getPersonnelid());
			personnel.setEncryptPWD(encryptPWD);
			personnel.setPassword(pwd); 
	
			//判断密码是否正确
			if (password.equals(pwd)) {
				resultString = objectMapper.writeValueAsString(personnel);
			} else {
				map.put("success", "0");
				map.put("reason", "密码不正确!");
				resultString = objectMapper.writeValueAsString(map);
			}
				
		}
		
		
		return resultString; 
	}
}

OK。项目配置完了。快吧? 而且不用tomcat就可以直接运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值