前沿:一直想整理一下springboot项目与各种技术的整合,个人又有个症状,jar包什么的都要最新的(哈哈哈),尴尬。
1.首先新建一个springboot项目,可以到spring官网生成一个maven的springboot项目
group输入com.zhangdi , Artifact 输入springboot,可以直接复制粘贴下面的代码
2.将生成的springboot项目导入到eclipse中,补全其中的的包名
controller-控制层,domain-实体层,mapper-放置mybaits的xml文件,statics-静态资源文件,templates-页面文件
完善pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.zhangdi</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybaits -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.application.properties文件配置
spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://localhost/springboot?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# Advanced configuration...
spring.datasource.max-active=50
spring.datasource.max-idle=6
spring.datasource.min-idle=2
spring.datasource.initial-size=6
#mybaits
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
#mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
#mybatis.type-aliases-package = com.zhangdi.sunflower
server.port=8080
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8
spring.resources.static-locations=classpath:/statics/
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
配置中包含数据库连接,mybaits中的mapper.xml路径,静态资源配置,mybaits分页插件配置信息。
4.下面是查询用户的整个代码流程 :实体User.java
package com.zhangdi.springboot.domain;
public class User {
private int id;
private String username;
private String password;
private String type;
private String crateTime;
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 getCrateTime() {
return crateTime;
}
public void setCrateTime(String crateTime) {
this.crateTime = crateTime;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", type=" + type
+ ", crateTime=" + crateTime + ", getId()=" + getId() + ", getUsername()=" + getUsername()
+ ", getPassword()=" + getPassword() + ", getCrateTime()=" + getCrateTime() + ", getType()=" + getType()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
+ "]";
}
}
UserMapper.xml,下面包含好几个方法,我就不进行筛选。不影响测试
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhangdi.springboot.dao.UserMapper">
<resultMap id="BaseResultMap" type="com.zhangdi.springboot.domain.User">
<result column="id" property="id" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="crate_time" property="crateTime" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectUser" resultMap="BaseResultMap" parameterType="string">
SELECT * FROM user
WHERE username = #{username}
</select>
</mapper>
dao层 UserMapper.java
package com.zhangdi.springboot.dao;
import org.apache.ibatis.annotations.Param;
import com.zhangdi.springboot.domain.User;
public interface UserMapper {
User selectUser(@Param("username") String username);
}
service层 UserService.java
package com.zhangdi.springboot.service;
import com.zhangdi.springboot.domain.User;
public interface UserService {
public User getUser(String username);
}
service实现层UserServiceImpl.java
package com.zhangdi.springboot.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhangdi.springboot.dao.UserMapper;
import com.zhangdi.springboot.domain.User;
import com.zhangdi.springboot.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
@Override
public User getUser(String username) {
// TODO Auto-generated method stub
return userMapper.selectUser(username);
}
}
controller层UserController.java
package com.zhangdi.springboot.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhangdi.springboot.domain.User;
import com.zhangdi.springboot.service.UserService;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value ="/index")
public String index() {
return "index";
}
@RequestMapping(value ="/login")
public String login() {
return "login";
}
@RequestMapping(value ="/getUser")
@ResponseBody
public Map<String,Object> getUser(String username,String password) {
Map<String,Object> map = new HashMap<String,Object>();
User userInfo = userService.getUser(username);
if(userInfo != null) {
map.put("success", true);
map.put("userInfo", userInfo);
}else {
map.put("success", false);
}
return map;
}
}
启动类,我修改为Application.java 添加注解@MapperScan("com.zhangdi.springboot.dao")
package com.zhangdi.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.zhangdi.springboot.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
数据库信息:
sql语句:
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50717
Source Host : localhost:3306
Source Database : sunflower
Target Server Type : MYSQL
Target Server Version : 50717
File Encoding : 65001
Date: 2018-11-19 16:24:37
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`type` char(1) DEFAULT NULL,
`crate_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'admin', 'admin', '1', '2018-10-13 18:36:02');
接口测试:启动后 访问路径:localhost:8080/getUser?username=admin
页面测试:index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap Admin</title>
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
</head>
<body class="">
Index.html
</body>
</html>
启动后:访问路径:http://localhost:8080/index
对应UserController中的index()方法返回index.html页面
至此,springboot与maven,mybaits的项目整合就结束了。该项目主要是介绍springboot项目的必要步骤,先跑出结果后删减的的,读者需要有一定的经验。