springboot入門,eclipse搭建springboot開發框架ssm框架。

Spring+SpringMVC+Mybatis開發框架ssm框架demo搭建|html|jsphtml

step1:java環境配置,安裝jdk8,此步略。java

step2:工具準備,安裝java版eclipsemysql

https://www.eclipse.org/web

step3:安裝 Spring Tool Suitespring

help->Install New  Software->經過下面地址進行安裝sql

http://download.springsource.com/release/TOOLS/update/e4.9/

或者(Version: 2019-03 (4.11.0)):
help->Eclipse Marketplace->search關鍵字,spring-tool-suite

step4:重啓eclipse數據庫

多瞭如上圖標,這表示工具安裝成功。apache

step5:手動建立springboot項目瀏覽器

New->Project->Spring Boot->Spring Starter Projectspringboot

step6:項目名稱

step7:根據你項目具體狀況進行選擇,這裏選擇Web+Mysql+DevTools+MyBatis+Thymeleaf

目錄及文件建立狀況:

step8:文件源碼:

DolearnApplication.java:自動建立

StringUtil.java

package com.example.demo.common;
 
public class StringUtil {
	public static boolean isNullOrZero (String str) {
		if(str == null || str.trim().length() == 0) {
			return true;
		}
		return false;
	}
}

LoginController.java

package com.example.demo.controller;
 
import org.mybatis.spring.annotation.MapperScan;
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.RequestMethod;
 
import com.example.demo.entity.Member;
import com.example.demo.service.IMember;
 
@Controller 
@MapperScan("com.example.demo.dao") //mapper掃描
public class LoginController{
 
	@Autowired
	IMember iMemberFunc;
	@RequestMapping(value = "/hello")
	public String hello(){
		System.out.println("Hello");
		return "Log";
	}
 
	@RequestMapping(value = "/login",method = RequestMethod.GET)
    public String addUser1(String name,String password) {
        System.out.println("loginName is:"+name);
        System.out.println("loginPassword is:"+password);
        try {
			Member member = iMemberFunc.login(name, password);
			if(member == null){
				System.out.println("登陸失敗");
				return "Log_fail";
			}else {
				System.out.println("登陸成功");
				return "Log_success";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getMessage());
			System.out.println("登陸異常");
		}
		return null;
        
	}
 
}

MemberDao.java

package com.example.demo.dao;
 
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.example.demo.entity.Member;
import org.apache.ibatis.annotations.Select;
 
//@Mapper    
public interface MemberDao {
    @Select("select * from Member where name = #{name}")
	Member selectMemberByName(@Param("name")String name)throws Exception;
}

Member.java

package com.example.demo.entity;
 
public class Member {
	private int id;
	private String name;
	private String password;
	public Member(){}
	public Member(int id, String name, String password) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	@Override
	public String toString() {
		return "Member [id=" + id + ", name=" + name + ", password=" + password
				+ "]";
	}
	
}

IMember.java

package com.example.demo.service;
 
import com.example.demo.entity.Member;
 
public interface IMember {
	Member login(String name, String passsword) throws Exception;
}

MemberImpl.java

package com.example.demo.service;
 
import com.example.demo.service.IMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.common.StringUtil;
import com.example.demo.entity.Member;
import com.example.demo.dao.MemberDao;
 
@Service
public class MemberImpl implements IMember {
		@Autowired
		private MemberDao mDao;
 
		public Member login(String name, String password) throws Exception {
		// System.out.println(name + password);
		if (StringUtil.isNullOrZero(name)) {
			System.out.println("登陸名不能爲空");
			return null;
		}
		if (StringUtil.isNullOrZero(password)) {
			System.out.println("密碼不能爲空");
			return null;
		}
		Member member = mDao.selectMemberByName(name);
		if (member == null)
			System.out.println("登陸名錯誤");
		if (member != null&&member.getName().equals(name)&&!password.equals(member.getPassword())) {
			System.out.println("密碼錯誤");	
		return null;
		}
		return member;
	}
 
}

Log_fail.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登陸失敗</title>
    <style>
    .center {
    margin: auto;
    width: 20%;
    border: 3px solid #73AD21;
    padding: 5px;
    text-align: center;
    }
    </style>
</head>
</head>
 
<body>
   <div class="center">	
	       <h1>登陸失敗</h1>	   
	       <div class="alert alert-warning" role="alert">帳號或密碼錯誤</div>
	       <span class="btn btn-default"> <a href="/hello" role="button"> [返回]</a></span> 	       	
	</div>
</body>
</html>

Log_success.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登陸成功</title>
    <style>
    .center {
    margin: auto;
    width: 20%;
    border: 3px solid #73AD21;
    padding: 5px;
    text-align: center;
    }
    </style>
</head>
 
 
<body>
   <div class="center">
	  	<h1>登陸成功</h1>	  
	    <span class="btn btn-default"> <a href="/hello" role="button"> [退出]</a></span>	  
	</div>
</body>
</html>

Log.html

<!DOCTYPE html >
<html >
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登陸頁面</title>
    <style>
    .center {
    margin: auto;
    width: 15%;
    border: 3px solid #73AD21;
    padding: 5px;
    text-align: center;
    }
    
    </style>
</head>
<body>
	<div >
		<div class="center">			
			<form action="/login" >				  		
				<input type="text" name="name" value="" placeholder="用戶名">	<br>		
				<input type="password" name="password" value="" placeholder="密碼"><br>				
				<input type="submit" name="" value="登陸">			
			</form>		
			
		</div>
		
	</div>
</body>
</html>

application.properties

spring.datasource.url=jdbc:mysql://192.168.190.**:3306/test
spring.datasource.username=root
spring.datasource.password=123456
 
#mybatis.config-location=classpath:mybatis-config.xml    
#mybatis.mapper-locations=classpath*:mapper/**/*.xml
mybatis.type-aliases-package=com.example.demo

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

數據庫member表

CREATE TABLE `member` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

運行項目,瀏覽器訪問

http://localhost:8080/hello

【異常】|  unrecognized or represents more than one time zone. You must configure either the server or JDBC driver 修改application.properties配置文件中的mysql配置,問題解決 spring.datasource.url=jdbc:mysql://192.168.190.68:3306/test?characterEncoding=utf8&serverTimezone=UTC 寫完了

相關文章
相關標籤/搜索