Spring boot 简介:
1.内置了tomcat, 主语内部集成 ssm打包是一个war包 web工程,boot是jar包,就是一个主函数运行。
2.配置简化
boot是一种主流的微服务的实现。
3.boot的使用:建议前后端分离使用,没有分离的不建议使用
spring boot 整合ssm:
1.可以到https://start.spring.io下创建springboot项目:
也可以自己在工具中手动创建,个人感觉这样比较好用。
2.项目导入后会缺少一些需要使用的包包,需要自己添加,pom配置如下,后期有需要的包再引入:
<?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.ruifeng</groupId>
<artifactId>mam</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mam</name>
<description>M</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
<fastjson.version>1.2.12</fastjson.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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<!--<include>contexts/</include>-->
<!--<include>*.xml</include>-->
<include>*.yml</include>
<include>*.properties</include>
<include>*.xml</include>
<include>enc_pri</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.application-dev.yml配置(数据库相关)需要注意yml文件的缩进:
spring:
datasource:
name: server
url: jdbc:mysql://localhost:3306/mamabike
username: root
password: root
3.1.application.yml配置:
#server
server:
port: 80
#spring
spring:
application:
name: mamabike
profiles:
active: dev #此处映入上面sql的配置信息
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
#mybatis xml path 包扫描
mybatis:
mapper-locations: classpath:com/ruifeng/**/**.xml
type-aliases-package: classpath:com.ruifeng.**.entity
4.然后可以使用generator生成dao、entity:
4.1.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>
<!--classPathEntry:数据库的JDBC驱动 -->
<!--这里是maven仓库中mysql-connector-java-5.1.35.jar的路径-->
<classPathEntry
location="E:\myWork\server\mvnPro\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" />
<context id="MysqlTables" targetRuntime="MyBatis3">
<!-- 注意这里面的顺序确定的,不能随变更改 -->
<!-- 自定义的分页插件 <plugin type="com.deppon.foss.module.helloworld.shared.PaginationPlugin"/> -->
<!-- 可选的(0 or 1) -->
<!-- 注释生成器 -->
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 必须的(1 required) -->
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mamabike"
userId="root" password="123456">
</jdbcConnection>
<!-- 可选的(0 or 1) -->
<!-- 类型转换器或者加类型解析器 -->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 必须的(1 required) -->
<!-- java模型生成器 -->
<!-- targetProject:自动生成代码的位置 -->
<javaModelGenerator targetPackage="com.ruifeng.mam.user.entity"
targetProject="C:\Users\Administrator\Desktop\mam\src\main\java"
>
<!-- TODO enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 必须的(1 required) -->
<!-- map xml 生成器 -->
<sqlMapGenerator targetPackage="com.ruifeng.mam.user.dao"
targetProject="C:\Users\Administrator\Desktop\mam\src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 可选的(0 or 1) -->
<!-- mapper 或者就是dao接口生成器 -->
<javaClientGenerator targetPackage="com.ruifeng.mam.user.dao"
targetProject="C:\Users\Administrator\Desktop\mam\src\main\java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 必须的(1...N) -->
<!-- pojo 实体生成器 -->
<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
<!-- schema即为数据库名 可不写 -->
<table tableName="user" domainObjectName="User"
enableInsert="true" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<!-- 忽略字段 可选的(0 or 1) -->
<!-- <ignoreColumn column="is_use" /> -->
<!--//无论字段是什么类型,生成的类属性都是varchar。 可选的(0 or 1) 测试无效 -->
<!-- <columnOverride column="city_code" jdbcType="VARCHAR" /> -->
</table>
</context>
</generatorConfiguration>
我这里使用的是IDEA编辑的,然后附上生成步骤:
5.简单的写一个controller:
package com.ruifeng.mamabike.user.controller;
import com.ruifeng.mamabike.user.dao.UserMapper;
import com.ruifeng.mamabike.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("user")
public class UserController {
//注入mapper
@Resource
private UserMapper userMapper;
@RequestMapping("/hello")
public User hello(){
User user = userMapper.selectByPrimaryKey(1l);//根据主键去查询user对象
return user;
}
}
6.最后直接运行主类main方法就搞定了:
package com.ruifeng.mamabike;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class MamabikeApplication {
/**
* spring boot项目入口
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(MamabikeApplication.class, args);
}
}
成功页面:
如果出现userMapper注入失败的错误,可以在UserMapper接口上使用@Mapper来解决。
来源:CSDN
作者:简简丨单单
链接:https://blog.csdn.net/weixin_37715257/article/details/80272681