构建第一个Spring Boot2.0应用之集成mybatis(六)
一、环境:
IDE:IntelliJ IDEA 2017.1.1
JDK:1.8.0_161
Maven:3.3.9
springboot:2.0.2.RELEASE
二、步骤
方式一:利用配置文件配置
1.创建springboot项目,并修改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.yy</groupId>
<artifactId>sbmybitas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>sbmybitas</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--Mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--Springboot mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- SpringBoot - MyBatis 逆向工程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</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>
<!-- MyBatis 逆向工程 插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- 自动生成的配置 -->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<!--是否覆盖-->
<overwrite>true</overwrite>
<!--允许移动生成的文件-->
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build> </project>
2. application.properties配置
#数据库连接信息
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.generatorConfig.xml配置
<?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>
<!-- 引入配置文件 -->
<properties resource="application.properties"/>
<!-- 指定数据库连接驱动jara地址 -->
<!-- 数据库驱动:选择本地硬盘上面的数据库驱动包-->
<classPathEntry location="D:\java\mysql-connector-java-5.1.17.jar"></classPathEntry> <!-- 一个数据库一个context -->
<context id="mysqltable1" targetRuntime="Mybatis3">
<!-- 生成的pojo,将implements Serializable -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <commentGenerator>
<!-- 是否取消注释 -->
<!--是否去除自动生成注释true:是 false:否-->
<property name="suppressAll" value="true"></property>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!--<jdbcConnection-->
<!--driverClass="com.mysql.jdbc.Driver"-->
<!--connectionURL="jdbc:mysql://localhost:3306/test"-->
<!--userId="root"-->
<!--password="mysql">-->
<!--</jdbcConnection>-->
<jdbcConnection driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}"/>
<!--mybatis里专门用来处理NUMERIC和DECIMAL类型的策略-->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
设置为true,把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"></property>
</javaTypeResolver> <!--生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.yy.entity" targetProject="src/main/java">
<!--enableSubPackages:是否让schema作为报的后缀-->
<property name="enableSubPackages" value="true"></property>
<!--从数据库返回的值被清理前后的空格-->
<property name="trimStrings" value="true"></property>
</javaModelGenerator>
<!--生成映射文件的包名和位置 mapper.xml
注意位置: targetProject="src/main/resources"
-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"></property>
</sqlMapGenerator>
<!--生成DAOMapper的包名和位置 mapper.java-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yy.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"></property>
</javaClientGenerator>
<!--table是指定每个表的生成策略-->
<!--tableName:用于自动生成代码数据库中的表名或视图名,domainObjectName:是对应的实体类名-->
<table
tableName="User_Info"
domainObjectName="UserInfo"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"></property>
</table>
</context>
</generatorConfiguration>
4.自动生成启动配置
打开项目Edit Configurations

新加Maven配置

设置启动命令

配置完成后启动自动生成

显示启动成功

目录结构如下

5.编写controller测试
5.1 修改启动类(Application.java)SbmybitasApplication.java
添加MapperScan,使之能够扫描到mapper接口,
@SpringBootApplication
@MapperScan("com.yy.mapper")
public class SbmybitasApplication {
public static void main(String[] args) {
SpringApplication.run(SbmybitasApplication.class,args);
}
}
5.2 编写HelloContoller测试
import com.yy.entity.UserInfo;
import com.yy.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; /**
* Created by Administrator on 2018-05-31.
*/
@Controller
@RequestMapping("/users")
public class HelloController { @Autowired
UserInfoMapper userInfoMapper;
@RequestMapping("/{id}")
public String getUserById(@PathVariable("id") Integer id, ModelMap mp)
{ UserInfo userInfo= userInfoMapper.selectByPrimaryKey(id);
mp.addAttribute("userinfo",userInfo);
return "index1";
}
@RequestMapping("/addUser" )
public String addUser(@RequestParam(value="age",required=true) Integer age,
@RequestParam (value="name",required = true) String name ,
@RequestParam(value="city",required = true) String city ,
@RequestParam(value="job",required = true) String job,
@RequestParam(value="sex",defaultValue ="1") Integer sex,
@RequestParam(value="province",defaultValue ="sichuan") String province ,ModelMap mp)
{
UserInfo userInfo=new UserInfo();
userInfo.setAge(age);
userInfo.setName(name);
userInfo.setJob(job);
userInfo.setCity(city);
userInfo.setSex(sex);
userInfo.setProvince(province);
userInfoMapper.insert(userInfo);
mp.addAttribute("userinfo",userInfo);
return "index";
}
}
若在
@Autowired
UserInfoMapper userInfoMapper;
出现 could not autoWired,No beans of 'UserInfoMapper' type found 提示,

则需修改UserInfoMapper,在类上添加@Component或@Repository 注解,即
package com.yy.mapper; import com.yy.entity.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository; //@Component(value="userInfoMapper")
//@Component
@Repository
public interface UserInfoMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_info
*
* @mbggenerated Sun Jun 03 22:06:22 CST 2018
*/
int deleteByPrimaryKey(Integer id); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_info
*
* @mbggenerated Sun Jun 03 22:06:22 CST 2018
*/
int insert(UserInfo record); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_info
*
* @mbggenerated Sun Jun 03 22:06:22 CST 2018
*/
int insertSelective(UserInfo record); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_info
*
* @mbggenerated Sun Jun 03 22:06:22 CST 2018
*/
UserInfo selectByPrimaryKey(Integer id); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_info
*
* @mbggenerated Sun Jun 03 22:06:22 CST 2018
*/
int updateByPrimaryKeySelective(UserInfo record); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_info
*
* @mbggenerated Sun Jun 03 22:06:22 CST 2018
*/
int updateByPrimaryKey(UserInfo record);
}
启动项目
、
6.测试
利用postman测试
6.1添加用户信息

参数设置后,send调用,浏览器返回,调用index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户信息如下:</h1>
<div th:text="${userinfo.name}">james</div>
</body>
</html>
结果如下

添加信息完成,数据库中记录如下

6.2 根据ID获取刚才添加的用户信息,返回结果如下

构建第一个Spring Boot2.0应用之集成mybatis(六)的更多相关文章
- 构建第一个Spring Boot2.0应用之集成mybatis、Druid(七)
一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.说明: 本文综合之 ...
- 构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)
一.环境: Windows: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE Linux(C ...
- 构建第一个Spring Boot2.0应用之项目创建(一)
1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...
- 构建第一个Spring Boot2.0应用之application.properties和application.yml(八)
本节学习在项目中配置文件配置的方式,一种是通过applicaiton.properties,一种是通过application.yml方式. 一.环境: IDE:IntelliJ IDEA 2017.1 ...
- 构建第一个spring boot2.0应用之项目启动运行的几种方式(二)
方法一. 配置Run/Debug Configuration 选择Main Class为项目 Application启动类(入口main方法) (2).进行项目目录,即包含pom.xml的目录下,启 ...
- 构建第一个Spring Boot2.0应用之Controller(三)
Controller控制器主要是接收浏览器请求.下面说一说@Controller注解和@RestController的区别: (1)@Controller类中的方法可以直接通过返回String跳转到j ...
- 构建第一个Spring Boot2.0应用之RequestMapping(四)
在学习controller的时候,测试了在RequestMapping中,value参数中配置集合,实现不同的URL访问同一方法. 本章继续学习和测试RequestMapping的其他特性. 一.Pa ...
- 快速搭建spring boot2.0 项目
快速搭建spring boot2.0+mybatis+thymeleaf 项目 使用工具STS 3.9.7(eclipse) 首先创建一个spring boot2.0项目(具体创建方法就不写了) 然后 ...
- spring boot2.0(一 ) 基础环境搭建
1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...
随机推荐
- VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置
VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置 在编译大型项目的时候,总是VS编译器的输出窗口总是会出现一堆warning警告,要想在里面找到error错误,要使用鼠标 ...
- linux date用法
读者可以设定特定的格式,格式设定规则:一个加号后接数个标记,每个标记中都有%,其中可用的标记列表和说明如下: %n : 下一行 %t : 跳格 %H : 小时(00..23) %I : 小时(01. ...
- p2023&bzoj1798 维护序列
传送门(洛谷) 传送门(bzoj) 题目 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全 ...
- kylin cube 构建过程
本文是对 http://kylin.apache.org/docs20/howto/howto_optimize_build.html的翻译,以便阅读. 1. 创建 Hive 中间表(Create ...
- 洛谷P3193 [HNOI2008]GT考试(KMP,矩阵)
传送门 大佬讲的真吼->这里 首先考虑dp,设$f[i][j]$表示长串匹配到第$i$位,短串最多匹配到$j$位时的方案数 那么答案就是$\sum_{i=0}^{m-1}f[n][i]$ 然后考 ...
- 在JAVA中自定义连接数据库的工具类
为什么要自定义数据库连接的工具类: 在开发中,我们在对数据库进行操作时,必须要先获取数据库的连接,在上一篇随笔中提到的获取数据库连接的步骤为: 1.定义好4个参数并赋值 2.加载驱动类 3.获取数据库 ...
- 关于CSRF攻击详解
CSRF的原理以及防范 CSRF概念:CSRF跨站点请求伪造(Cross—Site Request Forgery),跟XSS攻击一样,存在巨大的危害性,你可以这样来理解: 攻击者盗用了你的身份,以你 ...
- BUYING FEED
Problem F: F BUYING FEED Description Farmer John needs to travel to town to pick up K (1 <= K < ...
- angularJs1.x 版本中 uib-tabset 如何默认激活不同的标签页
<uib-tabset> 默认有个active属性,根据官方文档,active的默认值是0,也就是说,默认显示索引为0的标签页,可以通过修改这个值来默认显示不同的索引的标签页. 示例: ...
- 使用spring boot admin
spring boot admin管理端, 需要部署成独立的应用 pom中添加依赖 <dependency> <groupId>de.codecentric</group ...