方式一:用IDEA自带的SpringBoot工程
1.用IDEA创建SpringBoot工程
创建好后的工程目录:
SpringbootDemoApplication:核心入口
application.properties:SpringBoot默认配置文件
2.在springboot_demo包下创建controller测试项目
切记一定要在springboot_demo包下和核心入口类在同级目录,不然会报错
之后,运行核心入口main方法
在地址栏输入 http://localhost:8080/findOne
3.之前两步已经证明我们的SpringBoot能正常运行,现在要做的就是搭建SSM,执行最基本的查询
搭建之后的目录结构:
持久层接口:
package com.springboot.springboot_demo.mapper;
import com.springboot.springboot_demo.pojo.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
User selectById(String id);
}
持久层映射文件:
<?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.springboot.springboot_demo.mapper.UserMapper">
<select id="selectById" resultType="User">
SELECT * FROM user WHERE id=#{id}
</select>
</mapper>
业务层接口:
package com.springboot.springboot_demo.service;
import com.springboot.springboot_demo.pojo.User;
public interface UserService {
User findById(String id);
}
业务层实现:这里要注意一下,userMapper可能会有红线,但不影响程序运行。
package com.springboot.springboot_demo.service.impl;
import com.springboot.springboot_demo.mapper.UserMapper;
import com.springboot.springboot_demo.pojo.User;
import com.springboot.springboot_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findById(String id) {
return userMapper.selectById(id);
}
}
修改之前的控制层:
package com.springboot.springboot_demo.controller;
import com.springboot.springboot_demo.pojo.User;
import com.springboot.springboot_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findOne")
public User findOne(String id) {
return userService.findById(id);
}
}
application.properties配置文件:
#DataSource
spring.datasource.url = jdbc:mysql:///mybatisdb
spring.datasource.username = root
spring.datasource.password = 3416
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#Mybatis
#指定映射文件位置
mybatis.mapper-locations=classpath:mybatis/*.xml
#别名配置
mybatis.type-aliases-package=com.springboot.springboot_demo.pojo
新增pom坐标:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
以上代码敲完之后,运行核心入口main方法,有可能会报错:
The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one
这是MySql时区的问题,具体解决方法:
之后会正常启动,然后在地址栏输入 http://localhost:8080/findOne?id=
至此,第一种SpringBoot工程方式的SSM简单整合就完成了,当然这是最简陋的,里面有很多都还没配置,比如事务,过滤器等等…,但是这个教程只是让你入门,了解SpringBoot的强大。
方式二:普通maven的java工程
1.创建工程
创建完成之后
2.导入pom坐标
<properties>
<java.version>1.8</java.version>
</properties>
<!--父工程坐标-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<!--web启动器-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.创建启动类,注意包名
package com.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.这次我们采用配置Druid数据源的方式,
导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
创建配置类
package com.boot.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class JdbcConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}
5.service,dao,controller同方式一,整体项目目录
6.启动核心入口的main方法,在地址栏输入 http://localhost:8080/findOne?id=
至此,第二种实现方式写完了。其实两种都差不太多,第一种IDEA把相关的坐标依赖自动导入了,第二种则需要自己手动导入。
谢谢观看,如有不足,还请指正。