1、Springboot
1.1、Idea 从头搭建Springboot+Maven的web项目
1.创建新项目
打开编辑器,File-->New-->project,
2.配置环境变量
然后选择Spring Initializr,配置包的名称,路径等
3.添加依赖
选择依赖,这里我们添加Spring Web,Mybatis Framework,MySQLDriver这几个就可以,添加成功后可以在右侧看到
4.项目目录结构
点击Finish项目创建完毕,目录结构如下所示:
5.配置application.yml
首先将application.properties重命名为.yml文件,然后配置端口、数据库、和mybatis
server:
port:8080
spring:
devtools:
restart:
enabled:false
livereload:
enabled:true
datasource:
url: jdbc:mysql://localhost:3306/edison?serverTimezone=Asia/Shanghai&characterEncoding=utf-8
username: root
password: 4568
driver-class-name:com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.example.model
#showSql
logging:
level:
com:
example:
mapper :debug
6.web界面测试
在static目录下创建index.html,随便写点测试内容,然后点击右上角的启动按钮
项目启动完成之后,打凯浏览器,输入“localhost:8080”,即可访问刚才写的测试页面
7.整合mybatis
a.使用generatoConfig.xml的配置完成逆向工程配置
首先在pom.xml中添加相关依赖和插件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> src/main/resources/mybatis/generatorConfig.xml 然后在src/main/resources/mybatis下创建generatorConfig.xml,目录路径与pom.xml中的路径保持一致。具体配置信息如下:(注意生成文件的存放路径需要与项目的实际路径保持一致)。
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> userId="root"password="4568"> enableUpdateByExample="true"enableDeleteByExample="true"enableSelectByExample="true" selectByExampleQueryId="true"> 配置完成后,Reload项目,然后点击maven下的mybatis-generator插件 运行结束后,dao、mapper、model自动创建完成,之后目录结构如下: 8.web查询demo 接下来我们可以通过简单的demo来实现前后台交互完成查询。 a.controller层 packagecom.example.controller; importcom.alibaba.fastjson.JSONObject; importcom.example.service.UserInfoService; importcom.example.model.UserInfo; importcom.example.model.UserInfoExample; importcom.example.util.PbConstants; importcom.sun.deploy.net.HttpResponse; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RestController; importorg.springframework.web.servlet.ModelAndView; importjavax.servlet.http.HttpServletRequest; importjava.util.ArrayList; importjava.util.List; @RestController @RequestMapping("/user") publicclassUserInfoController{ @Autowired privateUserInfoServiceuserInfoService; /** * 登录 * @return */ @RequestMapping(value="/login",method=RequestMethod.POST) publicList // username = "edison"; // password = "111"; returnuserInfoService.getUserInfo(username,password); } /** * 获取所有用户 * @return */ @RequestMapping(value="/getAllUserInfo",method=RequestMethod.GET) publicList returnuserInfoService.getAllUserInfo(); } } b.service层 packagecom.example.service; importcom.alibaba.fastjson.JSONObject; importcom.example.mapper.UserInfoMapper; importcom.example.model.UserInfo; importcom.example.model.UserInfoExample; importcom.example.util.PbConstants; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Service; importorg.springframework.web.servlet.ModelAndView; importjava.util.ArrayList; importjava.util.List; @Service publicclassUserInfoService{ @Autowired privateUserInfoMapperuserInfoMapper; publicList List returnlist; } publicList List userInfos=userInfoMapper.getAllUserInfo(); returnuserInfos; } } c.由于model和mapper是自动生成的,因为我另外新加了一个查询所以表记录的方法,因此这里只展示mapper代码: packagecom.example.mapper; importcom.example.model.UserInfo; importcom.example.model.UserInfoExample; importjava.util.List; importorg.apache.ibatis.annotations.*; @Mapper publicinterfaceUserInfoMapper{ longcountByExample(UserInfoExampleexample); intdeleteByExample(UserInfoExampleexample); @Delete({ "delete from userinfo", "where id = #{id,jdbcType=VARCHAR}" }) intdeleteByPrimaryKey(Stringid); @Insert({ "insert into userinfo (id, username, ", "password, yxbz, ", "by1, by2, by3, ", "by4)", "values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, ", "#{password,jdbcType=VARCHAR}, #{yxbz,jdbcType=VARCHAR}, ", "#{by1,jdbcType=VARCHAR}, #{by2,jdbcType=VARCHAR}, #{by3,jdbcType=VARCHAR}, ", "#{by4,jdbcType=VARCHAR})" }) intinsert(UserInforecord); intinsertSelective(UserInforecord); List @Select({ "select", "id, username, password, yxbz, by1, by2, by3, by4", "from userinfo", "where id = #{id,jdbcType=VARCHAR}" }) @ResultMap("com.example.mapper.UserInfoMapper.BaseResultMap") UserInfoselectByPrimaryKey(Stringid); @Select({ "select", "id, username, password, yxbz, by1, by2, by3, by4", "from userinfo", "where username = #{username,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR} " }) @ResultMap("com.example.mapper.UserInfoMapper.BaseResultMap") List @Select({ "select", "id, username, password, yxbz, by1, by2, by3, by4", "from userinfo" }) @ResultMap("com.example.mapper.UserInfoMapper.BaseResultMap") List intupdateByExampleSelective(@Param("record")UserInforecord,@Param("example")UserInfoExampleexample); intupdateByExample(@Param("record")UserInforecord,@Param("example")UserInfoExampleexample); intupdateByPrimaryKeySelective(UserInforecord); @Update({ "update userinfo", "set username = #{username,jdbcType=VARCHAR},", "password = #{password,jdbcType=VARCHAR},", "yxbz = #{yxbz,jdbcType=VARCHAR},", "by1 = #{by1,jdbcType=VARCHAR},", "by2 = #{by2,jdbcType=VARCHAR},", "by3 = #{by3,jdbcType=VARCHAR},", "by4 = #{by4,jdbcType=VARCHAR}", "where id = #{id,jdbcType=VARCHAR}" }) intupdateByPrimaryKey(UserInforecord); } d.测试页面index.html *{ margin:0; padding:0; } .result{ position:fixed; width:100%; bottom:0; left:0; height:600px; background-color:rgba(0,0,0,.8); color:white; text-align:center; letter-spacing:2px; padding-top:20px; font-size:18px; line-height:28px; overflow:scroll; }