简单的springboot整合ssm
1.使用idea选择maven创建war项目
注意maven仓库的选择
另外数据库中需要已经有对应的表
2.补全项目结构
3.在pom.xml中添加项目依赖
- springboot的父项目(这里用的2.0.0版本)
<!--继承springboot父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
- 相关启动器
<dependencies>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<!-- pagehelper启动器 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- optional=true,依赖不会往下传递,如果有项目依赖本项目,并且想要使用devtools,需要重新引入 -->
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork><!-- 如果没有该配置,热部署的devtools不生效 -->
</configuration>
</plugin>
<!-- 自定义配置spring Boot使用的JDK版本 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
4.实体类
对应数据库表结构创建实体类
5.dao层接口
增删改查的接口
例如:List<Reglog> queryReglog();
6.mybatis映射文件
文件名一般为:xxxMapper.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.ttkj.game.dao.ReglogDao">
<select id="queryReglog" resultType="map">
select * from tab_reglog order by time limit 7
</select>
</mapper>
7.mybaits配置文件
空着就行
mybatisConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
8.springboot的配置文件yml
放在目录的resources下
application.yml
连接的MySQL数据库
#服务器配置
server:
port: 8182
servlet:
path: /gameUI
spring:
resources:
static-locations:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
- classpath:/img/
- classpath:/js/
#视图解析
mvc:
view:
prefix: /
suffix: .jsp
#创建数据源
spring:
datasource:
name: druid
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://192.168.32.101:3306/game
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
#整合mybatis
mybatis:
mapper-locations: classpath:mybatis/mapper/*Mapper.xml
config-location: classpath:mybatis/mybatisConfig.xml
type-aliases-package: com.ttkj.game.entity
9.创建主启动类
@SpringBootApplication
@ComponentScan(basePackages = "com.ttkj.game")
@MapperScan("com.ttkj.game.dao")
public class Mainapplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Mainapplication.class);
application.run(args);
}
}
10.运行主启动类
提示:
2019-11-28 15:18:37.180 INFO 5360 — [ main] com.ttkj.game.Mainapplication : Started Mainapplication in 6.245 seconds (JVM running for 6.883)
即运行成功