SpringBoot + mybatis JSP完整项目搭建

一、项目创建

  • 选择Spring Initializr,配置JDK
    SpringBoot + mybatis JSP完整项目搭建_第1张图片
  • 项目名
    SpringBoot + mybatis JSP完整项目搭建_第2张图片
  • 选择构建web项目所需的staters(启动器)

SpringBoot + mybatis JSP完整项目搭建_第3张图片

  • mybabis
    SpringBoot + mybatis JSP完整项目搭建_第4张图片
  • 保存到自己的项目地址
    SpringBoot + mybatis JSP完整项目搭建_第5张图片
  • 不需要的文件删除
    SpringBoot + mybatis JSP完整项目搭建_第6张图片
  • 最终项目目录结构如下
    SpringBoot + mybatis JSP完整项目搭建_第7张图片

resources目录结构:

  • static:保存所有的静态资源:js、css、images;
  • templates:保存所有的模板页面:(SpringBoot默认jar包使用嵌入式的Tomcat运行项目,而该服务器默认不支持JSP页面,需要JSP页面在下面);可以使用模板引擎(freemarker、thymeleaf);
  • application.properties:Spring Boot应用的全局配置文件;可以修改一些默认配置,例如:tomcat端口号等。

这里咱们使用jsp 所以 static ,templates 包没有用处

  • 添加webapp
    SpringBoot + mybatis JSP完整项目搭建_第8张图片
    这个路径看个人习惯 我喜欢放在main 下

  • 项目

SpringBoot + mybatis JSP完整项目搭建_第9张图片

二、项目架构分析!

这里写着发现上面 helloworld 中 l没打出来 变成helloword 有点尴尬 后来反抗了一下 把

在这里插入图片描述
这个改了

  • springboot项目主配置类分析
  • pom.xml配置文件

三、项目搭建

  • 一下没停住 弄了这么多 一个一个来看

  • springboot项目主配置类
package com.maple.springboot.springboothelloworld;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author rocky.maple
 * @SpringBootApplication:来标注一个主程序类,说明这是一个Spring Boot应用
 * @MapperScan 通过使用@MapperScan可以指定要扫描的Mapper类的包的路径
 * Spring Boot项目的启动入口
 * @date 2019-06-30 09:57
 */
@MapperScan("com.maple.springboot.springboothelloworld.dao")
@SpringBootApplication
public class SpringBootHelloworldApplication {
    public static void main(String[] args) {
        //加载自动配置类
        //帮我们免去了手动编写配置文件注入功能组件的工作
        SpringApplication.run(SpringBootHelloworldApplication.class, args);
    }

}

  • pom 文件


    4.0.0
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    
    com.maple.springboot
    spring-boot-helloword
    0.0.1-SNAPSHOT
    spring-boot-helloword
    Demo project for Spring Boot

    
    
        1.8
    

    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
        
            jstl
            jstl
            1.2
        
        
            taglibs
            standard
            1.1.2
        
        
        
        
        
        

        
            mysql
            mysql-connector-java
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        

        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        

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

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



  • application.properties 文件
spring.profiles.active=dev 
  • application-dev.yml 文件
server:
#  端口号
  port: 8080

spring:
  datasource:
#    数据库名称
    username: root
#    密码
    password: root
#
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
#    配置视图解析器属性
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

mybatis:
  #mybatis 的mapping扫描
  mapper-locations: classpath:mapping/*.xml
  #对应实体包
  type-aliases-package: com.maple.springboot.springboothelloworld.entity

#showSql
logging:
  level:
    com:
      example:
        mapper : debug
  • mapping 下的xml 文件






	
		
		
		
		
	

	
	
	
	
	
	    delete from `user` where userId=#{id}
	
	
	
	    update `user` set name=#{name},password =#{password},createdate=#{createdate} where userId=#{userId}
	
	
	
	    delete from `user` where 1=1
	    
			#{userId}
			    
	
	
	
	    INSERT INTO  `user`(userId,`name`,password ,createdate) values(#{userId},#{userName},#{password},#{createdate})
	
	


  • controller 下的类
package com.maple.springboot.springboothelloworld.controller;

import com.maple.springboot.springboothelloworld.entity.User;
import com.maple.springboot.springboothelloworld.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rocky.maple
 * @date 2019-06-11 21:47
 *
 */
@Controller
public class UserController {
	
	@Resource
	private UserService userService;

	@RequestMapping("/home")
	public String home(){
		return "home";
	}

	@RequestMapping("user/list.action")
	public ModelAndView list(){
		ModelAndView view = new ModelAndView();
		List users = userService.getList();
		view.addObject("users", users);
		view.setViewName("userList");
		return view;
	}
	@RequestMapping("user/listString")
	public String listString(){
		List users = userService.getList();
		return users.toString();
	}
//	public ModelAndView edit(@RequestParam("userId") String id){
	@RequestMapping("user/edit.action")
	public ModelAndView edit(String userId){
		ModelAndView view = new ModelAndView();
		User user = this.userService.getModel(userId);
		view.addObject("model", user);
		view.setViewName("userEdit");
		return view;
	}

	@RequestMapping("user/add.action")
	public ModelAndView add(User user){
		ModelAndView view = new ModelAndView();
		this.userService.addModel(user);
		view.addObject("model", user);
		view.setViewName("userEdit");
		return view;
	}
	@RequestMapping("user/userEdit")
	public String userEdit(){
		return "userEdit";
	}
	@RequestMapping("user/save.action")
	//@ResponseBody
	public @ResponseBody
    Map save(@Validated User user, BindingResult bindingResult ){
		
		Map res = new HashMap<>(2);
		if(bindingResult.hasErrors()){
			StringBuilder sbr = new StringBuilder();
			for(ObjectError v : bindingResult.getAllErrors()){
				sbr.append(v.getDefaultMessage());
			}			
			res.put("res", "false");
			res.put("msg", sbr.toString());
		}else{
			this.userService.save(user);
			res.put("res", "true");
			res.put("msg", "成功");
		}
		return res;

	}
	@RequestMapping("user/delete.action")
	public String delete(String[] userIds ){
		this.userService.delete(userIds);
		return "redirect:list.action";
	}
	
}

  • dao 下类
package com.maple.springboot.springboothelloworld.dao;

import com.maple.springboot.springboothelloworld.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author rocky.maple
 * @date 2019-06-11 21:47
 *
 */

@Repository
public interface UserDao {

	/**
	 * 获取用户 根据Id
	 * @param id id
	 * @return
	 * @throws Exception
	 */
	public User getUserById(String id) throws Exception;

	/**
	 * 查询用户 根据名字
	 * @param name 名字
	 * @return
	 * @throws Exception
	 */
	List getUserByName(String name) throws Exception;

	/**
	 * 删除用户根据Id
	 * @param id
	 * @return
	 * @throws Exception
	 */
	int deleteUserById(String id) throws Exception;

	/**
	 * 修改用户
	 * @param user
	 * @return
	 * @throws Exception
	 */
	 int updateUser(User user) throws Exception;

	/**
	 * 批量删除
	 * @param userIds
	 * @return
	 * @throws Exception
	 */
	 int deleteUsers(String[] userIds)throws Exception;

	/**
	 * 添加用户
	 * @param user
	 * @return
	 * @throws Exception
	 */
	 int addUser(User user)throws Exception;
}

  • entity 下类
package com.maple.springboot.springboothelloworld.entity;

import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;


/**
 * @author rocky.maple
 * @date 2019-06-11 21:47
 *
 */
public class User implements Serializable {
	
	private String userId;

	private String name;

	private String password;

	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	@NotNull(message="user.createdate.isNull")
	private Date createdate;


	public String getUserId() {
		return userId;
	}
	//用户Id
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getName() {
		return name;
	}
	//用户名称
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	//用户密码
	public void setPassword(String password) {
		this.password = password;
	}
	public Date getCreatedate() {
		return createdate;
	}
	//用户名称
	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}
}

  • service 下 impl 下的类
package com.maple.springboot.springboothelloworld.service;

import com.maple.springboot.springboothelloworld.entity.User;

import java.util.List;

public interface UserService {
	
	List getList();
	
	User getModel(String id);
	
	void save(User model);
	
	void delete(String[] userIds);
	
	void addModel(User user);
	
}

  • 这个可以测试下跳转
<%--
  Created by IntelliJ IDEA.
  User: maple
  Date: 2019-06-28
  Time: 16:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    嘻嘻哈哈



  • 下面是jsp 啥都没加
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>  
<%
	String Path = request.getContextPath();
	String basePath = request.getScheme() + "://" 
			+ request.getServerName() + ":" + request.getServerPort()
			+ Path + "/";
%>     

       




用户列表

<%----%>
	


	
复选框 用户编号 用户名 用户密码 创建日期 操作
${user.userId } ${user.name } ${user.password } 编辑

你可能感兴趣的:(java)