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
写完了