IDE:Intellij Idea 2018.03版本
数据库:MySQL
其实可以去Spring官网下载SpringBoot搭建好的工程,网址是:
(https://start.spring.io/)
我在这里使用了IDEA提供的Spring框架搭建的。
pom.xml
中。到了这里,我们就可以说项目已经搭建起来了!!!!!!!
目的:我们要实现数据库交互完成登陆页面的操作。
工程刚刚建好后,右下角会出现一个弹窗。点 Enable Auto-Import 即可,让IDEA帮我们自动导包。
1.建表
#为了方便主键id也没弄,就用户名和密码
CREATE TABLE `user` (
`username` varchar(255) COLLATE utf8_bin NOT NULL COMMENT '用户名',
`password` varchar(255) COLLATE utf8_bin NOT NULL COMMENT '密码'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
2.添加信息
#就插入一条数据
INSERT INTO `user` VALUES('xiaofang','1234')
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>ssmspringbootartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>ssmspringbootname>
<description>description>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
代码如下:
package com.example.ssmspringboot.domain;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/*
由于Lombok插件的原因只需要
@Data:生成所有,getter() setter() toString()等等方法
@Setter :生成setter()方法
@Getter:生成getter()方法
*/
@Data
public class User {
@Setter @Getter
private String username;
@Setter @Getter
private String password;
}
代码如下:
package com.example.ssmspringboot.dao;
import com.example.ssmspringboot.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper //声明是一个Mapper,与springbootApplication中的@MapperScan二选一写上即可
@Repository
public interface UserMapper {
User findByUsername(String username);
}
代码如下:(由于需求问题只写了一句,其他的可以自己加)
<mapper namespace="com.example.ssmspringboot.dao.UserMapper" >
<select id="findByUsername" resultType="User">
SELECT * FROM user WHERE username = #{username}
select>
mapper>
1.UserService接口:
package com.example.ssmspringboot.service;
import com.example.ssmspringboot.domain.User;
public interface UserService {
User getUser(String username);
}
2.UserServiceImpl.class:
package com.example.ssmspringboot.service.impl;
import com.example.ssmspringboot.dao.UserMapper;
import com.example.ssmspringboot.domain.User;
import com.example.ssmspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
//依赖注入
@Autowired
UserMapper userMapper;
@Override
public User getUser(String username) {
return userMapper.findByUsername(username);
}
}
代码如下:
package com.example.ssmspringboot.controller;
import com.example.ssmspringboot.domain.User;
import com.example.ssmspringboot.service.UserService;
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;
@Controller //声明这是Controller层
public class UserController {
//依赖注入
@Autowired
UserService userService;
@RequestMapping(value="/login",method = RequestMethod.POST)
public String login(User user) {
//调用dao层
System.out.println("前端"+user);
User u = userService.getUser(user.getUsername());
System.out.println("数据库"+u);
if (u.getPassword().equals(user.getPassword())){
return "success";
}
return "failed";
}
}
代码如下:
package com.example.ssmspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication //声明这是SpringBoot启动类
@EnableTransactionManagement //开启事务管理
public class SsmspringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SsmspringbootApplication.class, args);
}
}
#数据库指向,school是我的数据库名,改成自己的
spring.datasource.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF-8
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=admin
#出现mysql jdbc标红报错问题------>去pom.xml中删除mysql-connector-java下面的runtime那一行
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database = mysql
#Mybatis扫描
mybatis.mapper-locations=classpath*:mapper/*.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
mybatis.type-aliases-package=com.example.ssmspringboot.domain
1.index.html页面
由于SpringBoot的特性,没有之前的WEB-INF文件夹了,所以 static 文件夹下的index页面,只要访问localhost:8080
就能访问到主页,不再需要项目名了。
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<h1 align="center">请登录h1>
<form action="/login" method="post">
<table align="center" width="80%" border="1">
<tr>
<td width="40%">USERNAME:td>
<td><input type="text" name="username">td>
tr>
<tr>
<td width="40%">PASSWORD:td>
<td><input type="password" name="password">td>
tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="LOGIN">
td>
tr>
table>
form>
body>
html>
2.success.html页面
<html lang="en">
<head>
<meta charset="UTF-8">
<title>成功了title>
head>
<body>
<h1>恭喜你,登陆成功!h1>
body>
html>
3.failed.html页面
<html lang="en">
<head>
<meta charset="UTF-8">
<title>出错了title>
head>
<body>
<h1>你出错了,你知道吗?h1>
body>
html>
到此为止,我们的项目已经彻底的搭建好了,接下去就是测试了,见证奇迹吧~
1.运行SsmspringbootApplication类的 main 方法
恭喜你,成功了~~~~~~~~~~~~~