如何利用IDEA搭建SpringBoot项目整合mybatis实现简单的登录功能
利用闲余时间想自己搭建一个springboot+mybatis的项目,提升一下自己对项目的了解,毕竟自己还是一个小白,在这里为自己创建项目的过程做一个记录,以便以后回忆。同时将搭建中遇到的问题也在这里做记录。如有遇到同样问题的同学,希望能给你一下借鉴。
springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便。
一,搭建项目:
1、打开IDEA,点击File→New→Project...,如图1所示
图1 搭建项目
2、当我们选择project...后,会看见如图2所示的页面,选择SpringInitializr,点击NEXT即可。
图2搭建项目
3、接下来将看到如图3所示的页面,在这个页面中我只是修改了名称。然后点击NEXT进行下一步。
图3搭建项目
4、当我点击NEXT时,却出现如图4所示的错误,这由于创建时因为Artifact的命名为大小写混合,将大写改为小写即可正常创建。
图4搭建项目(注意名称大小写问题)
5、接下来我们修改上面的错误,我将springbootTest1→springboottest1,然后进行下一步。会看见如图5所示的页面。在当前页面中我们一次选择左侧的Web、TemplateEngines、SQL。然后在中间部分选择我们需要的选项,最终选择结果如最右侧所示。然后点击NEXT,进行下一步。
图5搭建项目(选择需要的内容)
6、如图6所示,进人创建项目的最后一个页面,在这里我们可以修改项目保存的位置。确认自己输入的内容,点击Fiish即可完成项目的创建。
图6搭建项目(可以修改文件路径)
7、点击Finish后,出现如图7所示页面,我们选择NewWindow即可,即在新的IDEA中打开我们新建的项目。
图7打开项目
二、启动项目、添加配置文件等
1、打开新项目后,我们可以观察一下左侧的项目结构。如图8所示。
生成的项目中,resources文件夹下,static文件夹下存放静态文件,比如css、js、html和图片等。
templates下存放html文件,controller默认访问该文件夹下的html文件。
这个在application.properties配置文件中是可以修改的。
下面为项目生成的pom.xml文件:
4.0.0 com.example springboottest1 0.0.1-SNAPSHOT jar springboottest1 DemoprojectforSpringBoot org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
图8 项目文件结构
2、了解项目的文件结构后,我们尝试着启动项目。我们发现项目启动过程中出现错误,错误信息如图9所示。
图9项目启动报错
这是因为我们创建SpringBoot项目时,在选择组件时添加了mysql、mybatis,但现在还没有配置数据库,导致项目启动报错。
我们需要在项目的application.properties文件中配置数据信息。如图10所示,是我自己配置的数据库,具体情况根据自己数据库的设置配置。
图10数据库配置
数据库采用MySQL数据库,下面是数据库的设计,只有几个简单的字段。如图11所示。
图11数据库设计
这里也可以不使用application.properties文件,采用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties), 文件的内容如下(此处只配置最基本的):
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
接下来再次尝试重新启动项目。发现项目可以正常启动了。可以打开浏览器访问http://localhost:8080/,访问结果如图12所示。证明项目正常启动了。
图12访问结果
三、项目整合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#注意:一定要对应mapper映射xml文件的所在路径 type-aliases-package:com.example.springboottest1.entity#注意:对应实体类的路径
2、在pom.xml中添加 mybatisgenerator自动生成代码插件。
org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.1 ${basedir}/src/main/resources/generator/generatorConfig.xml true true
3、在resource文件夹下创建generator文件夹,并在文件夹中创建generatorConfig.xml文件。文件内容如下所示:
4、所有文件创建完成后,还需要进行简单的配置。Run→EditConfigurations...,然后选择maven,配置Commandline。
5、所用文件创建完成,并配置好相关设置,重新启动项目,启动后运行generator。通过控制台可以看见代码是否生成成功,或失败的原因。如果代码生成成功,可以在项目文件夹中看见生成的项目。
这个过程共生成三个文件,一个实体类,dao,还有sql语句。
5.1、生成的实体类entity:
5.2、生
packagecom.example.springboottest1.entity; publicclassuser{ /** *ThisfieldwasgeneratedbyMyBatisGenerator. *Thisfieldcorrespondstothedatabasecolumnuser.id * *@mbggenerated */ privateIntegerid; /** *ThisfieldwasgeneratedbyMyBatisGenerator. *Thisfieldcorrespondstothedatabasecolumnuser.username * *@mbggenerated */ privateStringusername; /** *ThisfieldwasgeneratedbyMyBatisGenerator. *Thisfieldcorrespondstothedatabasecolumnuser.password * *@mbggenerated */ privateStringpassword; /** *ThisfieldwasgeneratedbyMyBatisGenerator. *Thisfieldcorrespondstothedatabasecolumnuser.age * *@mbggenerated */ privateIntegerage; /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodreturnsthevalueofthedatabasecolumnuser.id * *@returnthevalueofuser.id * *@mbggenerated */ publicIntegergetId(){ returnid; } /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodsetsthevalueofthedatabasecolumnuser.id * *@paramidthevalueforuser.id * *@mbggenerated */ publicvoidsetId(Integerid){ this.id=id; } /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodreturnsthevalueofthedatabasecolumnuser.username * *@returnthevalueofuser.username * *@mbggenerated */ publicStringgetUsername(){ returnusername; } /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodsetsthevalueofthedatabasecolumnuser.username * *@paramusernamethevalueforuser.username * *@mbggenerated */ publicvoidsetUsername(Stringusername){ this.username=username==null?null:username.trim(); } /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodreturnsthevalueofthedatabasecolumnuser.password * *@returnthevalueofuser.password * *@mbggenerated */ publicStringgetPassword(){ returnpassword; } /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodsetsthevalueofthedatabasecolumnuser.password * *@parampasswordthevalueforuser.password * *@mbggenerated */ publicvoidsetPassword(Stringpassword){ this.password=password==null?null:password.trim(); } /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodreturnsthevalueofthedatabasecolumnuser.age * *@returnthevalueofuser.age * *@mbggenerated */ publicIntegergetAge(){ returnage; } /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodsetsthevalueofthedatabasecolumnuser.age * *@paramagethevalueforuser.age * *@mbggenerated */ publicvoidsetAge(Integerage){ this.age=age; } }
成的Dao:
packagecom.example.springboottest1.mapper; importcom.example.springboottest1.entity.user; publicinterfaceuserMapper{ /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodcorrespondstothedatabasetableuser * *@mbggenerated */ intdeleteByPrimaryKey(Integerid); /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodcorrespondstothedatabasetableuser * *@mbggenerated */ intinsert(userrecord); /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodcorrespondstothedatabasetableuser * *@mbggenerated */ intinsertSelective(userrecord); /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodcorrespondstothedatabasetableuser * *@mbggenerated */ userselectByPrimaryKey(Integerid); /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodcorrespondstothedatabasetableuser * *@mbggenerated */ intupdateByPrimaryKeySelective(userrecord); /** *ThismethodwasgeneratedbyMyBatisGenerator. *Thismethodcorrespondstothedatabasetableuser * *@mbggenerated */ intupdateByPrimaryKey(userrecord); }
5.3、生成的SQL语句(其中一些没有用的注释被我删掉了):
id, username, password, age select fromuser whereid=#{id,jdbcType=INTEGER} deletefromuser whereid=#{id,jdbcType=INTEGER} insertintouser(id,username,password,age) values( #{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) insertintouser id, username, password, age, #{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, updateuser whereid=#{id,jdbcType=INTEGER} username=#{username,jdbcType=VARCHAR}, password=#{password,jdbcType=VARCHAR}, age=#{age,jdbcType=INTEGER}, updateuser setusername=#{username,jdbcType=VARCHAR}, password=#{password,jdbcType=VARCHAR}, age=#{age,jdbcType=INTEGER} whereid=#{id,jdbcType=INTEGER}
四、测试访问
做好以上的准备以后,可以简单的写一个小页面,试着访问一下。
同时这个时候可以自己了解一下@Controller与@RestController的区别,以及@ResponseBody的用法。
1、比如写一个简单的HTML页面,HelloWord.html。
HelloWord hellospringboot!!!