开发工具:IntelliJ IDEA 2019.1 x64
1.File-->New-->Project...-->spring Initializr
下一步(需要在联网的情况下):
下一步,选择依赖的组件,可选可不选,后续可以在pom.xml中手动添加依赖:
下一步:
点击finish:,目录不存在,点OK创建。
提示:如果是第一次创建Springboot项目,会比较慢,因为需要下载Spring的相关依赖,后续创建会快速完成。
项目结构:
pom.xml文件中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--oracle11g-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<!--log4j日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--spring boot热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
<!--jQuery-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
</dependency>
pom.xml文件中的build配置:
<build> <finalName>java_springboot</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>tomcat8x</containerId> <home>D:\STUDY\tomcat\apache-tomcat-8.5.35\apache-tomcat-8.5.35</home> </container> <configuration> <type>existing</type> <home>D:\STUDY\tomcat\apache-tomcat-8.5.35\apache-tomcat-8.5.35</home> </configuration> </configuration> <executions> <execution> <id>cargo-run</id> <phase>install</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
resources下,删除application.properties文件,新建application.yml文件,内容:
debug: true
#服务器信息
server:
tomcat:
max-threads: 1000
min-spare-threads: 30
port: 8081
#spring配置
spring:
devtools:
restart:
enabled: true #设置开启热部署
freemarker:
cache: false #页面不加载缓存,修改即时生效
datasource:
url: jdbc:oracle:thin:@//127.0.0.1:1521/orcl
username: erp
password: erp
driver-class-name: oracle.jdbc.OracleDriver
#mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml #mapper映射文件路径
config-location: classpath:mybatis-config.xml #mybatis主配置文件所在路径
# type-aliases-package: com.yjl.java_springboot.entity #设置类型别名
# configuration:
# auto-mapping-behavior: full #自动映射结果集
# map-underscore-to-camel-case: true #下划线转驼峰
# log-impl: org.apache.ibatis.logging.log4j.Log4jImpl #指定所用日志的具体实现
# cache-enabled: true #在本项目中开启二级缓存
在项目中配置多套环境的配置方法。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml
笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
resources下,创建mybatis-config.xml文件(application.yml中指定好的),配置内容:
<configuration>
<settings>
<!--指定所用日志的具体实现-->
<setting name="logImpl" value="LOG4J"/>
<!--自动映射结果集-->
<setting name="autoMappingBehavior" value="FULL"/>
<!--下划线转驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases><!-- 设置类型别名 -->
<package name="com.yjl.java_springboot.entity"/>
</typeAliases>
</configuration>
日志使用的log4j,log4j.properties文件也在resources下。
resources下,创建templates和mapper文件夹。mapper中存放mapper.xml文件,templates中存放html文件。
spring boot为啥不推荐jsp,参考:https://spring.io/blog/2012/10/30/spring-mvc-from-jsp-and-tiles-to-thymeleaf
创建实体类:
idea连接数据库:https://blog.csdn.net/qq_38550031/article/details/86639496
选择文件夹,将数据库中的表生成到相应的文件夹中,只是需要修改下实体类的包名。
以Sysuser为例:
创建包controller、entity、dao、service,添加java类:
userDAO.java==================================================================
package com.example.demo_springboot.dao;
import com.example.demo_springboot.entity.Sysuser;
import org.apache.ibatis.annotations.Param;
public interface userDAO {
Sysuser login(@Param("usercode") String usercode, @Param("password") String password);
Sysuser getInfo(@Param("pkUser") String pk_user);
}
userService.java==================================================================
package com.example.demo_springboot.service;
import com.example.demo_springboot.entity.Sysuser;
public interface userService {
Sysuser login(String usercode, String password) throws Exception;
Sysuser getInfo(String pk_user) throws Exception;
}
userServiceImpl.java==================================================================
package com.example.demo_springboot.service.impl;
import com.example.demo_springboot.dao.userDAO;
import com.example.demo_springboot.entity.Sysuser;
import com.example.demo_springboot.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
@Component
public class userServiceImpl implements userService {
@Autowired
userDAO udao;
@Override
public Sysuser login(String usercode, String password) throws Exception {
return udao.login(usercode, password);
}
@Override
public Sysuser getInfo(String pk_user) throws Exception {
return udao.getInfo(pk_user);
}
}
userController.java==================================================================
package com.example.demo_springboot.controller;
import com.alibaba.fastjson.JSON;
import com.example.demo_springboot.entity.Sysuser;
import com.example.demo_springboot.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller
public class userController {
@Autowired
userService uService;
@RequestMapping("/hello")
public String hello() {
return "login";
}
@RequestMapping("/login")
public String login(String usercode, String password, Model m, HttpSession session) throws Exception {
Sysuser u = uService.login(usercode, password);
if (u != null) {
session.setAttribute("user", u);
}
return "index";
}
@RequestMapping("/getuserInfo/{pk_user}")
@ResponseBody
public String getuserInfo(@PathVariable String pk_user) throws Exception {
return JSON.toJSONString(uService.getInfo(pk_user));
}
}
=================================================================================
templates下创建html文件:
login.html=========================================================================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form id="form" method="post" action="/login">
用户:<input type="text" name="usercode"/>
密码:<input type="password" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
index.html=========================================================================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<button onclick="getUserInfo()">getUserInfo</button>
<div id="userInfo"></div>
</body>
<!--从maven库中导入jQuery-->
<script type="text/javascript" src="/webjars/jquery/3.1.1/jquery.min.js"></script>
<script>
//使用Ajax获取user信息
function getUserInfo() {
var PKuser='[[${session.user.pkUser}]]';
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var data = JSON.parse(xmlHttp.responseText);
document.getElementById('userInfo').innerText = data.identitycode;//显示identitycode属性
}
}
xmlHttp.open("POST", '/getuserInfo/' + PKuser, true);
xmlHttp.send();
}
</script>
</html>
最后,DemoSpringbootApplication(启动类)中添加注解:
@MapperScan("com.example.demo_springboot.dao")//扫描dao层
运行DemoSpringbootApplication。
浏览器输入:http://localhost:8081/hello(端口号是application.yml中的server.port变量,默认8080)
根据数据库中的用户编码和密码登录:
点击按钮,通过Ajax获取Sysuser对象的identitycode属性。