1. 程式人生 > >(一)搭建基礎框架(Springboot+SSM)

(一)搭建基礎框架(Springboot+SSM)

首先建立一個maven專案,新增pom依賴包如下:
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>per.san</groupId> <artifactId>jee-love</artifactId> <version>1.0-SNAPSHOT</version> <!-- 繼承spring-boot父包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath></relativePath> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <
artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <!-- 表示開發的時候引入,釋出的時候不會載入此包 --> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!-- 配置java版本 不配置的話預設父類配置的是1.6--> <pluginManagement> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>

新增配置檔案:
application.yml

server:
  port: 8090
spring:
  application:
    name: jee-love
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/jee-love?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=yes
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath*:/mapper/*/*.xml

然後開始編寫demo程式碼測試:
StartApplication.java

@MapperScan(basePackages = {"per.san.*.mapper","per.san.*.mapper.*"})
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

DemoController.java

@Controller
public class DemoController {

    @Autowired
    IDemoService iDemoService;

    @RequestMapping("/demo")
    @ResponseBody
    List<Demo> demo() {
        return iDemoService.select();
    }
}

DemoMapper.java

public interface DemoMapper extends BaseMapper<Demo> {
}

測試表

CREATE TABLE `demo` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

繼承的BaseMapper包含了基本業務所需的增刪該查,直接呼叫即可
啟動專案,訪問http://localhost:8090/demo,出現如下格式資料(資料為表裡自己新增的資料)即專案搭建成功:
在這裡插入圖片描述

專案程式碼地址 https://github.com/977735287/jee-love.git