使用Spring boot 快速创建项目
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就可以直接运行。
还没有评论,来说两句吧...