springboot+maven+mybaits项目整合最新版

前沿:一直想整理一下springboot项目与各种技术的整合,个人又有个症状,jar包什么的都要最新的(哈哈哈),尴尬。

1.首先新建一个springboot项目,可以到spring官网生成一个maven的springboot项目

地址:https://start.spring.io/

group输入com.zhangdi , Artifact 输入springboot,可以直接复制粘贴下面的代码

springboot+maven+mybaits项目整合最新版_第1张图片

2.将生成的springboot项目导入到eclipse中,补全其中的的包名

springboot+maven+mybaits项目整合最新版_第2张图片

controller-控制层,domain-实体层,mapper-放置mybaits的xml文件,statics-静态资源文件,templates-页面文件

完善pom.xml文件



	4.0.0

	com.zhangdi
	springboot
	0.0.1-SNAPSHOT
	jar

	springboot
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		
         
        
           org.springframework.boot
           spring-boot-starter-web
        
        
          org.springframework.boot
          spring-boot-starter-thymeleaf
         

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		 
		  org.mybatis.spring.boot 
		 mybatis-spring-boot-starter 
		 1.3.1 
		 
	
       
       mysql
       mysql-connector-java
       5.1.21
       
 
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



 

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,下面包含好几个方法,我就不进行筛选。不影响测试




  
    
     
      
      
  

    


	

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 getUser(String username,String password) {
		Map map = new HashMap();
		 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);
	}
}

数据库信息:

springboot+maven+mybaits项目整合最新版_第3张图片

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

 springboot+maven+mybaits项目整合最新版_第4张图片

页面测试:index.html



  
    
    Bootstrap Admin
    
    
    
     
  

   
   
    Index.html
 
  

启动后:访问路径:http://localhost:8080/index 

对应UserController中的index()方法返回index.html页面

springboot+maven+mybaits项目整合最新版_第5张图片

至此,springboot与maven,mybaits的项目整合就结束了。该项目主要是介绍springboot项目的必要步骤,先跑出结果后删减的的,读者需要有一定的经验。

你可能感兴趣的:(springboot,springboot,springboot,maven)