Spring+SpringMVC+Mybatis開發框架ssm框架demo搭建|html|jsp
step1:java環境配置,安裝jdk8,此步略。
step2:工具準備,安裝java版eclipse
https://www.eclipse.org/
step3:安裝 Spring Tool Suite
help->Install New Software->通過下面地址進行安裝
http://download.springsource.com/release/TOOLS/update/e4.9/ |
step4:重啓eclipse
多瞭如上圖標,這表示工具安裝成功。
step5:手動創建springboot項目
New->Project->Spring Boot->Spring Starter Project
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表
運行項目,瀏覽器訪問
http://localhost:8080/hello
寫完了