打开apache-maven-3.6.0
–>conf
–>settings.xml
文件
主要配置两点:本地仓库;在线镜像地址(如果不修改,默认连接境外地址,下载速度较慢。经测试连接阿里云仓库速度较快)。配置如下。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>E:/program/repositorylocalRepository>
<mirrors>
<mirror>
<id>alimavenid>
<name>aliyun mavenname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<mirrorOf>centralmirrorOf>
mirror>
mirrors>
.......
Window
–>Preferences
–>Maven
–>Installations
如下图settings.xml
。让开发工具STS知道maven的具体配置,主要是我们前面配置的本地仓库,在线镜像地址。这样开发工具知道去哪个镜像下载jar包,然后存储到本地的什么地方。如下图所示Apply and Close
配置生效。打开STS file
–>new
–>spring starter project
直接Next
选择springboot的版本,因为我们是一个web服务所以勾选web–>web.并且勾选SQL–>mybatis。实质上就是在maven的pom.xml
中加入需要的依赖。最后点击finish
。
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.1.RELEASEversion>
<relativePath />
parent>
<groupId>com.examplegroupId>
<artifactId>demo-1artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo-1name>
<description>Demo project for Spring Bootdescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc6artifactId>
<version>11.2.0.3version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.54version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
<addResources>trueaddResources>
configuration>
plugin>
plugins>
build>
project>
application.properties
配置数据库,端口等基本配置。server.port=80
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.241.129:1521:orcl
spring.datasource.username=scott
spring.datasource.password=admin
StartApp.java
(默认为项目名)StartApp.java 为默认启动程序,含main
方法。我将默认的demo.java 修改文件名改后为StartApp.java
package app;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@MapperScan(basePackages="app.mapper") //开启注解扫描,指定扫描文件为app.mapper包底下的所有包含`@mapper`的类。
@ServletComponentScan
public class StartApp {
public static void main(String[] args) {
SpringApplication.run(StartApp.class, args);
}
}
User.java
实体类package app.entity;
import java.io.Serializable;
public class User implements Serializable{
private int id;
private String username;
private String password;
private String danWeiTID;
@Override
public String toString() {
return "User [danWeiTID=" + danWeiTID + ", id=" + id + ", password="
+ password + ", username=" + username + "]";
}
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;
}
public String getDanWeiTID() {
return danWeiTID;
}
public void setDanWeiTID(String danWeiTID) {
this.danWeiTID = danWeiTID;
}
}
UserMapper.java
用户对应的mapper类package app.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import app.entity.User;
@Mapper //添加此注解,便可以被扫描到
public interface UserMapper {
/**
* 返回所有用户列表
* @return
*/
@Select("SELECT * FROM ZBXL_USER")
public List<User> findAllUser();
}
UserController.java
控制转发器package app.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import app.entity.User;
import app.mapper.UserMapper;
import app.util.JsonUtil;
import app.util.SessionUtil;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/getAllUser")
public String getUsers() {
List<User> users = userMapper.findAllUser();
if(users != null && !users.isEmpty()) {
return JsonUtil.toJson(users);
}
return "err";
}
}
JsonUtil.java
package app.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtil {
private static ObjectMapper mapper;
public static String toJson(Object obj){
try {
mapper = new ObjectMapper();
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "json exchange fail";
}
}
打开浏览器输入127.0.0.1/getAllUser
或者localhost/getAllUser
。因为application.properties配置的端口是80,所以可以省略,加上像这样127.0.0.1:80/getAllUser
也无妨。