spring-boot mybatis整合实例,STS开发

1. 创建项目

File——>Spring Starter Project,,这个页面应该都可以看懂。next——》可以添加一些你需要的依赖,这里只是apache的maven架包。点击Finish。

2.引入依赖

在生成的都pom文件中引入依赖

<dependencies>

<!-- WEB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- thymeleaf 模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>

<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<!-- druid数据源 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.6</version>
</dependency>

<!-- 内置的Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<!-- fastJson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>

<!-- A.CTable -->
<!-- https://mvnrepository.com/artifact/com.gitee.sunchenbin.mybatis.actable/mybatis-enhance-actable -->
<dependency>
    <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
    <artifactId>mybatis-enhance-actable</artifactId>
    <version>1.0.3</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

耐心等下方的进度条到100%,表示架包全部引入成功,

这时候项目初始化就完成了,可以点击运行(Spring Boot APP)出现这个图标就表示项目运行成功了

3.写配置文件

虽然说spring帮你省去了很多繁琐的配置(约定大于配置),但关键的配置还是必不可少的:在application.properties中做如下配置

# 指定端口号

server.port=8081

#指定访问的前缀

server.servlet.context-path=/menu




# thymeleaf 静态资源配置
spring.thymeleaf.prefix=classpath:/templates/thymeleaf/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html; charset=utf-8
# 关闭缓存,及时刷新,上线生产环境需要改为true
spring.thymeleaf.cache=false


# 设置静态文件路径
spring.mvc.static-path-pattern=/static/**


# 数据源配置——druid数据源
spring.datasource.url=jdbc:mysql://localhost:3306/menu_v1
spring.datasource.username=你的数据库账号
spring.datasource.password=你的数据库密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view.servlet.allow=true


# mybatis配置
mybatis.type-aliases-package=com.example.pojo

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

在resources中建如下的文件夹。

mapper : mybatis的mapper文件(sql写在mapper中方便后期做数据库优化)

static:存放静态资源(js,css,图片等)

templates:存放系统前端页面


4.建包


java文件夹下建如下包,必须都在example包下,否则spring-boot找不到

在程序入口类做如下配置:就是加上两个标签

@SpringBootApplication

@MapperScan(basePackages="com.example.dao")

public class MenuV2Application {


public static void main(String[] args) {
SpringApplication.run(MenuV2Application.class, args);
}

}

到此,项目配置就全部完成了

5.写controller


service接口


service实现类


实体类在pojo包下创建

创建dao


写SQL


数据库表字段的创建略去。

到这里就全部完成了。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值