如何利用IDEA搭建SpringBoot項(xiàng)目整合mybatis實(shí)現(xiàn)簡(jiǎn)單的登錄功能
利用閑余時(shí)間想自己搭建一個(gè)springboot+mybatis的項(xiàng)目,提升一下自己對(duì)項(xiàng)目的了解,畢竟自己還是一個(gè)小白,在這里為自己創(chuàng)建項(xiàng)目的過程做一個(gè)記錄,以便以后回憶。同時(shí)將搭建中遇到的問題也在這里做記錄。如有遇到同樣問題的同學(xué),希望能給你一下借鑒。
springboot的優(yōu)勢(shì)之一就是快速搭建項(xiàng)目,省去了自己導(dǎo)入jar包和配置xml的時(shí)間,使用非常方便。
一,搭建項(xiàng)目:
1、打開IDEA,點(diǎn)擊File→New→Project...,如圖1所示

圖1 搭建項(xiàng)目
2、當(dāng)我們選擇project...后,會(huì)看見如圖2所示的頁面,選擇Spring Initializr,點(diǎn)擊NEXT即可。

圖2 搭建項(xiàng)目
3、接下來將看到如圖3所示的頁面,在這個(gè)頁面中我只是修改了名稱。然后點(diǎn)擊NEXT進(jìn)行下一步。

圖3 搭建項(xiàng)目
4、當(dāng)我點(diǎn)擊NEXT時(shí),卻出現(xiàn)如圖4所示的錯(cuò)誤,這由于創(chuàng)建時(shí)因?yàn)锳rtifact的命名為大小寫混合,將大寫改為小寫即可正常創(chuàng)建。

圖4 搭建項(xiàng)目 (注意名稱大小寫問題)
5、接下來我們修改上面的錯(cuò)誤,我將springbootTest1→springboottest1,然后進(jìn)行下一步。會(huì)看見如圖5所示的頁面。 在當(dāng)前頁面中我們一次選擇左側(cè)的Web、Template Engines、SQL。然后在中間部分選擇我們需要的選項(xiàng),最終選擇結(jié)果如最右側(cè)所示。然后點(diǎn)擊NEXT,進(jìn)行下一步。

圖5 搭建項(xiàng)目(選擇需要的內(nèi)容)
6、如圖6所示,進(jìn)人創(chuàng)建項(xiàng)目的最后一個(gè)頁面,在這里我們可以修改項(xiàng)目保存的位置。確認(rèn)自己輸入的內(nèi)容,點(diǎn)擊Fiish即可完成項(xiàng)目的創(chuàng)建。

圖6 搭建項(xiàng)目(可以修改文件路徑)
7、點(diǎn)擊Finish后,出現(xiàn)如圖7所示頁面,我們選擇New Window即可,即在新的IDEA中打開我們新建的項(xiàng)目。

圖7 打開項(xiàng)目
二、啟動(dòng)項(xiàng)目、添加配置文件等
1、打開新項(xiàng)目后,我們可以觀察一下左側(cè)的項(xiàng)目結(jié)構(gòu)。如圖8所示。
生成的項(xiàng)目中,resources文件夾下,static文件夾下存放靜態(tài)文件,比如css、js、html和圖片等 。
templates下存放html文件,controller默認(rèn)訪問該文件夾下的html文件。
這個(gè)在application.properties配置文件中是可以修改的。

下面為項(xiàng)目生成的pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.example</groupId> <artifactId>springboottest1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboottest1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
圖8 項(xiàng)目文件結(jié)構(gòu)
2、了解項(xiàng)目的文件結(jié)構(gòu)后,我們嘗試著啟動(dòng)項(xiàng)目。我們發(fā)現(xiàn)項(xiàng)目啟動(dòng)過程中出現(xiàn)錯(cuò)誤,錯(cuò)誤信息如圖9所示。

圖9 項(xiàng)目啟動(dòng)報(bào)錯(cuò)
這是因?yàn)槲覀儎?chuàng)建Spring Boot項(xiàng)目時(shí),在選擇組件時(shí)添加了mysql、mybatis,但現(xiàn)在還沒有配置數(shù)據(jù)庫,導(dǎo)致項(xiàng)目啟動(dòng)報(bào)錯(cuò)。
我們需要在項(xiàng)目的application.properties文件中配置數(shù)據(jù)信息。如圖10所示,是我自己配置的數(shù)據(jù)庫,具體情況根據(jù)自己數(shù)據(jù)庫的設(shè)置配置。

圖10 數(shù)據(jù)庫配置
數(shù)據(jù)庫采用MySQL數(shù)據(jù)庫,下面是數(shù)據(jù)庫的設(shè)計(jì),只有幾個(gè)簡(jiǎn)單的字段。如圖11所示。

圖11 數(shù)據(jù)庫設(shè)計(jì)
這里也可以不使用application.properties文件,采用更加簡(jiǎn)潔的application.yml文件。將resource文件夾下原有的application.properties文件刪除,創(chuàng)建application.yml配置文件(備注:其實(shí)SpringBoot底層會(huì)把application.yml文件解析為application.properties), 文件的內(nèi)容如下(此處只配置最基本的):
server: port: 8080 spring: datasource: name: test url: jdbc:mysql://127.0.0.1:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
接下來再次嘗試重新啟動(dòng)項(xiàng)目。發(fā)現(xiàn)項(xiàng)目可以正常啟動(dòng)了。可以打開瀏覽器訪問http://localhost:8080/,訪問結(jié)果如圖12所示。證明項(xiàng)目正常啟動(dòng)了。

圖12 訪問結(jié)果
三、項(xiàng)目整合mybatis
1、在application.yml文件中添加mybatis。
server: port: 8080 spring: datasource: name: test url: jdbc:mysql://127.0.0.1:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml #注意:一定要對(duì)應(yīng)mapper映射xml文件的所在路徑 type-aliases-package: com.example.springboottest1.entity # 注意:對(duì)應(yīng)實(shí)體類的路徑
2、在pom.xml中添加 mybatis generator 自動(dòng)生成代碼插件。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自動(dòng)生成代碼插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
3、在resource文件夾下創(chuàng)建generator文件夾,并在文件夾中創(chuàng)建generatorConfig.xml文件。文件內(nèi)容如下所示:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 數(shù)據(jù)庫驅(qū)動(dòng):選擇你的本地硬盤上面的數(shù)據(jù)庫驅(qū)動(dòng)包--> <classPathEntry location="C:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.36-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="false"/> </commentGenerator> <!--數(shù)據(jù)庫連接驅(qū)動(dòng)類,URL,用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成(實(shí)體)模型的包名和位置--> <javaModelGenerator targetPackage="com.example.springboottest1.entity" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成XML映射文件的包名和位置--> <sqlMapGenerator targetPackage="resources.mapper" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO接口的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springboottest1.mapper" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是數(shù)據(jù)庫中的表名或視圖名 domainObjectName是實(shí)體類名--> <table tableName="user" domainObjectName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
4、所有文件創(chuàng)建完成后,還需要進(jìn)行簡(jiǎn)單的配置。Run→Edit Configurations...,然后選擇maven,配置Command line。

5、所用文件創(chuàng)建完成,并配置好相關(guān)設(shè)置,重新啟動(dòng)項(xiàng)目,啟動(dòng)后運(yùn)行g(shù)enerator。通過控制臺(tái)可以看見代碼是否生成成功,或失敗的原因。如果代碼生成成功,可以在項(xiàng)目文件夾中看見生成的項(xiàng)目。

這個(gè)過程共生成三個(gè)文件,一個(gè)實(shí)體類,dao,還有sql語句。
5.1、生成的實(shí)體類entity:
5.2、生
package com.example.springboottest1.entity;
public class user {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.id
*
* @mbggenerated
*/
private Integer id;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.username
*
* @mbggenerated
*/
private String username;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.password
*
* @mbggenerated
*/
private String password;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.age
*
* @mbggenerated
*/
private Integer age;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.id
*
* @return the value of user.id
*
* @mbggenerated
*/
public Integer getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.id
*
* @param id the value for user.id
*
* @mbggenerated
*/
public void setId(Integer id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.username
*
* @return the value of user.username
*
* @mbggenerated
*/
public String getUsername() {
return username;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.username
*
* @param username the value for user.username
*
* @mbggenerated
*/
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.password
*
* @return the value of user.password
*
* @mbggenerated
*/
public String getPassword() {
return password;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.password
*
* @param password the value for user.password
*
* @mbggenerated
*/
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.age
*
* @return the value of user.age
*
* @mbggenerated
*/
public Integer getAge() {
return age;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.age
*
* @param age the value for user.age
*
* @mbggenerated
*/
public void setAge(Integer age) {
this.age = age;
}
}
成的Dao:
package com.example.springboottest1.mapper;
import com.example.springboottest1.entity.user;
public interface userMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated
*/
int deleteByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated
*/
int insert(user record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated
*/
int insertSelective(user record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated
*/
user selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated
*/
int updateByPrimaryKeySelective(user record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated
*/
int updateByPrimaryKey(user record);
}
5.3、生成的SQL語句(其中一些沒有用的注釋被我刪掉了):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.springboottest1.mapper.userMapper" >
<resultMap id="BaseResultMap" type="com.example.springboottest1.entity.user" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id,
username,
password,
age
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.springboottest1.entity.user" >
insert into user (id, username, password, age)
values (
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.example.springboottest1.entity.user" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" >
username,
</if>
<if test="password != null" >
password,
</if>
<if test="age != null" >
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.springboottest1.entity.user" >
update user
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.springboottest1.entity.user" >
update user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
四、測(cè)試訪問
做好以上的準(zhǔn)備以后,可以簡(jiǎn)單的寫一個(gè)小頁面,試著訪問一下。
同時(shí)這個(gè)時(shí)候可以自己了解一下@Controller與@RestController的區(qū)別,以及@ResponseBody的用法。
1、比如寫一個(gè)簡(jiǎn)單的HTML頁面,HelloWord.html。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HelloWord</title> </head> <body> <h1>hello springboot!!!</h1> </body> </html>
2、訪問頁面的controller。helloController.java
package com.example.springboottest1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = {"/hello"})
public class helloController {
@RequestMapping(value = {"/springboot"})
public String hello(){
return "HelloWord";
}
}
3、重新啟動(dòng)項(xiàng)目,訪問http://localhost:8080/hello/springboot,訪問的結(jié)果如下所示。

五、編寫用戶登錄過程代碼
1、編寫簡(jiǎn)單的登錄頁面與注冊(cè)頁面
(1)登錄頁面 userLogin.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶登錄</title> </head> <body> 請(qǐng)輸入用戶名與密碼登錄 <form action="/user/userLogin" method="post"> 用戶名:<input type="text" name="username" /><br> 密 碼:<input type="password" name="password" /><br> <input type="submit" value="登錄" /> <a href="/user/registerpage" target="_blank">注冊(cè)</a> </form> </body> </html>
頁面效果:

(2)注冊(cè)頁面 register.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注冊(cè)</title> </head> <body> <form action="/user/uregister" method="post"> 用戶名:<input type="text" name="username" /></br> 密碼:<input type="password" name="password" /></br> 確認(rèn)密碼:<input type="password" name="password2" /></br> 年齡:<input type="text" name="age" /></br> <input type="submit" value="注冊(cè)"> </form> </body> </html>
頁面效果:

(3)登錄成功頁面 index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄成功</title> </head> <body> <h1>用戶名與密碼正確,登錄成功?。?!</h1> </body> </html>
頁面效果:

(4)登錄失敗頁面 loginError.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄失敗</title> </head> <body> <h1>用戶名或密碼錯(cuò)誤,登錄失?。。?!</h1> </body> </html>
頁面效果:

2、對(duì)于寫代碼的順序,我是從xml(sql語句)開始寫,然后Dao,Service,最后寫Controller。下面為我的部分代碼。(開始生成的代碼沒有用到的部分被我刪除掉了)
(1)xml(sql語句) userMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.springboottest1.mapper.userMapper" >
<!--用戶登錄驗(yàn)證-->
<select id="userlogin" parameterType="user" resultType="User">
SELECT id,username,password,age FROM user WHERE username = #{username} AND password = #{password}
</select>
<!--新用戶注冊(cè) 方式1-->
<insert id="adduser" parameterType="user" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user (username,password,age) VALUES (#{username},#{password},#{age})
</insert>
<!--新用戶注冊(cè) 方式2-->
<insert id="adduser1" parameterType="user">
INSERT INTO user (id,username,password,age) VALUES (UUID(),#{username},#{password},#{age})
</insert>
</mapper>
(2)Dao層 userMapper.java
package com.example.springboottest1.mapper;
import com.example.springboottest1.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.Map;
@Mapper
@Component
public interface userMapper {
//用戶登錄
User userlogin(@Param("username") String username,@Param("password") String password);
//注冊(cè)新用戶(方式1)
int adduser(@Param("username") String username, @Param("password") String password, @Param("age") int age);
//注冊(cè)新用戶(方式2)
int adduser1(@Param("username") String username, @Param("password") String password, @Param("age") int age);
}
(3)Servicr層 UserLoginService.java
package com.example.springboottest1.service;
import com.example.springboottest1.entity.User;
import com.example.springboottest1.mapper.userMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class UserLoginService {
/**
* 注入dao
*/
@Autowired
private userMapper usermapper;
//用戶登錄
public User userLogin(String username,String password){
User user = usermapper.userlogin(username,password);
return user;
}
//注冊(cè)新用戶
public int adduser(String username,String password,int age){
return usermapper.adduser(username,password,age);
//return usermapper.adduser1(username,password,age); //對(duì)應(yīng)sql語句中的第二種注冊(cè)方式
}
}
(4)Controller層 UserLoginController.java
package com.example.springboottest1.controller;
import com.example.springboottest1.entity.User;
import com.example.springboottest1.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping(value = {"/user"})
public class UserLoginController {
/**
* 注入service
*/
@Autowired
private UserLoginService userLoginService;
/**
* 跳轉(zhuǎn)到用戶登錄頁面
* @return 登錄頁面
*/
@RequestMapping(value = {"/loginHtml"})
public String loginHtml(){
return "userLogin";
}
/**
* 跳轉(zhuǎn)到用戶注冊(cè)頁面
* @return 注冊(cè)頁面
*/
@RequestMapping(value = {"/registerpage"})
public String registerpage(){
return "register";
}
/**
* 獲取用戶名與密碼,用戶登錄
* @return 登錄成功頁面
*/
@RequestMapping(value = {"/userLogin"})
public String userLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest request){
User user = userLoginService.userLogin(username,password);
if(user != null){ //登錄成功
request.getSession().setAttribute("session_user",user); //將用戶信息放入session
return "index";
}
return "loginError";
}
/**
* 注冊(cè)新用戶
* @return 注冊(cè)結(jié)果
*/
@ResponseBody
@RequestMapping(value = {"/uregister"})
public String addUser(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("password2") String password2,
@RequestParam("age") int age){
if(!password.equals(password2)){
return "兩次密碼不相同,注冊(cè)失?。?!";
}else {
int res = userLoginService.adduser(username,password,age);
if(res == 0){
return "注冊(cè)失?。?;
}else {
return "注冊(cè)成功!";
}
}
}
}
由于有同學(xué)問我要源碼,有時(shí)候我可能看不見消息,所以將源碼在百度云中分享出來,大家根據(jù)自己需要,自行下載吧,如果有什么建議就評(píng)論吧:
鏈接: https://pan.baidu.com/s/1TO00D-eHnwA8tm6VHRgIbA
提取碼: waeu
總結(jié)
到此這篇關(guān)于如何利用IDEA搭建SpringBoot項(xiàng)目整合mybatis實(shí)現(xiàn)簡(jiǎn)單的登錄功能的文章就介紹到這了,更多相關(guān)IDEA搭建SpringBoot項(xiàng)目整合mybatis實(shí)現(xiàn)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Java兩個(gè)浮點(diǎn)型數(shù)字加減乘除的問題
由于浮點(diǎn)數(shù)在計(jì)算機(jī)中是以二進(jìn)制表示的,直接進(jìn)行加減乘除運(yùn)算會(huì)出現(xiàn)精度誤差,想要得到精確結(jié)果,應(yīng)使用BigDecimal類進(jìn)行運(yùn)算2024-10-10
java使用Apache工具集實(shí)現(xiàn)ftp文件傳輸代碼詳解
這篇文章主要介紹了java使用Apache工具集實(shí)現(xiàn)ftp文件傳輸代碼詳解,分享了詳細(xì)連接ftp server和上傳文件,下載文件的代碼,以及結(jié)果展示,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
gateway網(wǎng)關(guān)接口請(qǐng)求的校驗(yàn)方式
這篇文章主要介紹了gateway網(wǎng)關(guān)接口請(qǐng)求的校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
java多線程關(guān)鍵字final和static詳解
這篇文章主要介紹了java多線程關(guān)鍵字final和static詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
利用SpringDataJPA開啟審計(jì)功能,自動(dòng)保存操作人操作時(shí)間
這篇文章主要介紹了利用SpringDataJPA開啟審計(jì)功能,自動(dòng)保存操作人操作時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽教程詳解
這篇文章主要介紹了zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽,主要介紹了什么是服務(wù)器動(dòng)態(tài)上下線監(jiān)聽及為什么要實(shí)現(xiàn)對(duì)服務(wù)器上下線的監(jiān)聽,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Intellij IDEA中啟動(dòng)多個(gè)微服務(wù)(開啟Run Dashboard管理)
這篇文章主要介紹了Intellij IDEA中啟動(dòng)多個(gè)微服務(wù)(開啟Run Dashboard管理),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

