寫一個簡易的項目
環境: jdk1.8
編輯器:idea
用到的技術:springboot + mybatis-plus + mysql
1.創建項目

2. jdk 就用我電腦上的1.8

3. 改成自己想要的名字 因為准備打war包所以這里是war

4. web

5. 習慣改成yml
端口號 配置一下 ,
前台頁面的路徑配置一下

server: port: 8091 max-http-header-size: 10240 spring: mvc: view: prefix: /pages
6. 創建一個html

7. 創建controller

8. 展示 沒問題

# 9. 如果想打包的話
mvn clean package -Dmaven.test.skip=true

包在這呦

下一步-》》》》》連數據庫
1. 引入pom
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>

2.改yml
數據庫連接池配置:
當然這之前要建表 。
spring: mvc: view: prefix: /pages datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mypro?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT username: root password: root type: com.alibaba.druid.pool.DruidDataSource

簡單配一下 mybatis-plus:
mybatis-plus: mapper-locations: classpath:/mapping/*.xml

3. 把 service mapper 之類的都建好
看一下現在的目錄

看一下代碼
controller
package com.example.king.hello.controller; import com.example.king.hello.dto.Hello; import com.example.king.hello.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller @RequestMapping("/hello") public class HelloController { @Autowired private HelloService helloService; @RequestMapping("") public String index() { return "/hello.html"; } @ResponseBody @RequestMapping("/getData") public List<Hello> getData() { return helloService.getData(); } }
service
package com.example.king.hello.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.king.hello.dto.Hello; import java.util.List; public interface HelloService extends IService<Hello> { List<Hello> getData(); }
serviceImpl
package com.example.king.hello.service; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.king.hello.dto.Hello; import com.example.king.hello.mapper.HelloMapper; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.List; @Service @Transactional public class HelloServiceImpl extends ServiceImpl<HelloMapper, Hello> implements HelloService { @Override public List<Hello> getData() { QueryWrapper<Hello> wrapper = new QueryWrapper<>(); return this.baseMapper.selectList(wrapper); } }
實體類
package com.example.king.hello.dto; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; @TableName("tab_hello") public class Hello implements Serializable { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.AUTO) private Long id; @TableField("name") private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Hello{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
mapper
package com.example.king.hello.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.king.hello.dto.Hello; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; @Mapper public interface HelloMapper extends BaseMapper<Hello> { }
為了測試表里只有兩個字段 id 和name
看一下結果

##如果需要寫xml 可以這樣配置:
pom 中加上
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

配置文件中:
mybatis-plus: global-config: db-config: id-type: auto field-strategy: not_empty table-underline: true db-type: mysql logic-delete-value: 1 # 邏輯已刪除值(默認為 1) logic-not-delete-value: 0 # 邏輯未刪除值(默認為 0) mapper-locations: classpath*:com/example/king/**/mapper/mapping/*.xml

@
