最近读了spring-boot开发手册,spring-boot相比于spring-mvc封装了很多常用的依赖,并且内置了tomcat容器,启动生成的jar包即可启动项目,也是目前流行的微服务常用框架。本文主要用到了spring-boot,以及mybatis,数据库用到了mysql。
源码下载
1.首先创建一个表:
CREATE TABLE `t_user` (
`USER_ID` int(11) NOT NULL AUTO_INCREMENT,
`USER_NAME` char(30) NOT NULL,
`USER_PASSWORD` char(10) NOT NULL,
`USER_EMAIL` char(30) NOT NULL,
PRIMARY KEY (`USER_ID`),
KEY `IDX_NAME` (`USER_NAME`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
插入一些数据:
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (1, '林炳文', '1234567@', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (2, 'evan', '123', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (3, 'kaka', 'cadg', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (4, 'simle', 'cscs', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (5, 'arthur', 'csas', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (6, '小德', 'yuh78', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (7, '小小', 'cvff', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (8, '林林之家', 'gvv', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (9, '林炳文Evankaka', 'dfsc', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (10, 'apple', 'uih6', '[email protected]');
我习惯于先创建好maven项目,构建目录再导入到编译器中,这样的好处就是搭建好一个脚手架模板,后面改改参数就可以用到各个工程里面。
4.0.0
com.boot
testSpringBoot
0.0.1-SNAPSHOT
testSpringBoot
jar
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
org.springframework.boot
spring-boot-starter-thymeleaf
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-maven-plugin
创建的文件目录如图:
工程目录
(1) 启动器
在com.boot(即最外层目录文件)下写一个如下main方法:
package com.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
(2)controller
在com.boot.web下创建一个类如下:
package com.boot.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MainController {
@RequestMapping(value = "/")
String home() {
return "Hello World!";
}
}
(3)启动项目
找到com.boot下的Application以java Application方式启动,然后打开浏览器输入localhost:8080就会出现Hello World!
启动页面
(1)User.java
对应数据库中表的字段,放在src/main/java下的包com.boot.domain
package com.boot.domain;
public class User {
private Integer userId;
private String userName;
private String userPassword;
private String userEmail;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userPassword=" + userPassword + ", userEmail=" + userEmail
+ "]";
}
}
(2)UserDao.java
Dao接口类,用来对应mapper文件。放在src/main/java下的包com.lin.dao,内容如下:
package com.boot.dao;
import org.apache.ibatis.annotations.Mapper;
import com.boot.domain.User;
@Mapper
public interface UserDao {
public User selectUserById(Integer userId);
}
(3)UserService.java和UserServiceImpl.java
service接口类和实现类,放在src/main/java下的包com.lin.service,内容如下:
UserService.java
package com.boot.service;
import com.boot.domain.User;
public interface UserService {
User selectUserById(Integer userId);
}
UserServiceImpl.java
package com.boot.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.dao.UserDao;
import com.boot.domain.User;
import com.boot.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public User selectUserById(Integer userId) {
return userDao.selectUserById(userId);
}
}
(4)mapper文件
用来和dao文件对应,放在src/main/resources中的com/boot/mapper目录下
UserMapper.xml路径
UserMapper.xml
下列所有文件均在src/main/resources目录下
(1)spring-boot配置
不少人都Properties资源文件来配置,不过这种文件在eclipse编码的默认设置是ISO-8859-1,需要修改eclipse的设置才能显示中文。因此我比较喜欢用yml文件来配置,一个是结构明显,另外一个不用考虑编码的问题。
application.yml
spring:
thymeleaf:
mode: HTML5
encoding: UTF-8
content-type: text/html
# 开发禁用缓存
cache: false
datasource:
url: jdbc:mysql://localhost:3306/ssmDb?characterEncoding=UTF-8&useSSL=true
username: root
password: password
# 可省略驱动配置, sprin-boot 会由url检测出驱动类型
# driver-class-name: com.mysql.jdbc.Driver
hikari:
connection-timeout: 60000
mybatis:
mapperLocations: classpath:com/boot/mapper/*.xml
typeAliasesPackage: cn.boot.domain
# spring-boot默认打印输出info级别以上的,可在此处修改输出级别
logging:
level:
root: info
(2)日志打印logback-spring.xml
%date [%thread] %-5level %logger{80} - %msg%n
INFO
ACCEPT
DENY
./logs/info.%d{yyyy-MM-dd}.log
30
%date [%thread] %-5level %logger{80} - %msg%n
DEBUG
ACCEPT
DENY
./logs/debug.%d{yyyy-MM-dd}.log
30
%date [%thread] %-5level %logger{80} - %msg%n
ERROR
ACCEPT
DENY
./logs/error.%d{yyyy-MM-dd}.log
30
%date [%thread] %-5level %logger{80} - %msg%n
spring-boot单元测试只需引入spring-boot-starter-test即可,里面集成了Junit以及Spring Test & Spring Boot Test等依赖。
测试类目录
(1)测试基类
package com.boot.baseTest;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public abstract class SpringTestCase {
Logger logger = LoggerFactory.getLogger(this.getClass());
}
(2)测试类
package com.boot.serviceTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.boot.baseTest.SpringTestCase;
import com.boot.domain.User;
import com.boot.service.UserService;
public class UserServiceTest extends SpringTestCase{
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private UserService userService;
@Test
public void selectUserByIdTest(){
User user = userService.selectUserById(10);
logger.info("查找结果" + user);
}
}
选中测试类右键run as选中Junit Test即可,可以看到打印输出了id为10的User对象
2018-02-04 21:20:07,442 [main] INFO com.boot.serviceTest.UserServiceTest - 查找结果User [userId=10, userName=apple, userPassword=uih6, [email protected]]
数据库数据
若看到正常输出,此时可以开始配置web页面了。
(1)静态资源
静态资源放在src/main/resources/static目录下
图片.png
(2)index.html
spring-boot支持thymeleaf模板引擎,模板文件默认放在src/main/resources/templates目录下
Hello World!
xmlns:th="http://www.thymeleaf.org"命名空间,将镜头转化为动态的视图,需要进行动态处理的元素使用“th:”前缀;link引入jquery框架,通过@{}引入web静态资源(括号里面是资源路径)访问model中的数据通过${}访问。
(4)自定义错误页面
spring-boot错误页面默认在src/main/resources/static/error或者src/main/resources/public/error下,在此简单写了个404页面,命名为404.html放到目录下即可
404.html
404 not found
(5)controller
package com.boot.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.boot.domain.User;
import com.boot.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "")
public String getIndex(Model model){
User user = userService.selectUserById(1);
model.addAttribute("user", user);
return "index";
}
}
**(6)启动工程
启动工程以后打开http://localhost:8080/user即可看到如下图:
index页面
若打开任意不存在的页面,会返回404.html中内容
404
使用package命令给工程打包成jar包
mvn package
此时会在target下生成一个jar包,启动即可:
java -jar target\testSpringBoot-0.0.1-SNAPSHOT.jar
输出如下
λ java -jar target\testSpringBoot-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE)
作者:萨瓦迪卡卡卡
链接:https://www.jianshu.com/p/95fb7be049ae
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。