spring boot 整合mybatis

环境:

eclipse:

Version: 2019-06 (4.12.0)

jdk:

java version "1.8.0_144"

maven:

Apache Maven 3.6.1

步骤:

1.
https://spring.io/projects/spring-boot
到该网页的最下面
找到

Quick start
Bootstrap your application with Spring Initializr.

生成一个spring boot 项目导入到eclipse
目前最新版本2.2.1官方说明文档地址
https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/index.html

2.
导入后启动报错:
No active profile set, falling back to default profiles: default
pom.xml中是因为没有starter
添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
重启
正常启动后的日志
2019-11-18 14:05:37.865  INFO 43628 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  :
 Tomcat started on port(s): 8080 (http) with context path ''
2019-11-18 14:05:37.869  INFO 43628 --- [           main] c.s.s.SecretkeymanagementApplication     :
Started SecretkeymanagementApplication in 2.644 seconds (JVM running for 4.396)

3.
配置这个spring boot 项目
application.yml
添加
server:
  port: 8888
  servlet:
    context-path: /sec
    
注意port: 后面有个空格然后8888后面不允许有空格

正常启动后会有
2019-11-18 14:15:40.212  INFO 44172 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  :
Tomcat started on port(s): 8888 (http) with context path '/sec'
2019-11-18 14:15:40.215  INFO 44172 --- [           main] c.s.s.SecretkeymanagementApplication     :
Started SecretkeymanagementApplication in 2.531 seconds (JVM running for 4.397)

4.
浏览器访问
http://localhost:8888/sec/

页面出现
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Nov 18 14:16:21 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
暂时先不处理

5.
spring整合mybatis
参考https://blog.csdn.net/iku5200/article/details/82856621

添加 mysql-connector-java <version>5.1.48</version>和 org.mybatis.spring.boot <version>2.1.1</version>的依赖包


6.
在项目中配置多套环境的配置方法
(1)修改application.yml文件,
清空然后添加
spring:
  profiles:
    active: dev

(2)在application.yml同级目录添加application-dev.yml文件
然后添加
server:
  port: 8888
  servlet:
    context-path: /sec
 
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  #type-aliases-package: com.snc.secretkeymanagement.entity
 
#showSql
logging:
  level:
    com:
      snc:
        secretkeymanagement:
          mapper : debug
注释:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:

application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值.
另外logging:元素是指定com.snc.secretkeymanagement.mapper路径即mapper接口所在路径本例中SecMaskruleMapper.java所在package
在调试模式启动打印mybatis执行的sql

2019-11-22 14:26:48.902 DEBUG 18300 --- [nio-8888-exec-1] c.s.s.m.S.selectByPrimaryKey             : ==>  Preparing: select maskrule_id, name, descript, data_type, mask_type, create_user, create_datetime, last_modified_user, last_modified_time, create_type, id, mode, start_point from sec_maskrule where maskrule_id = ?
2019-11-22 14:26:48.922 DEBUG 18300 --- [nio-8888-exec-1] c.s.s.m.S.selectByPrimaryKey             : ==> Parameters: 1(Long)
2019-11-22 14:26:48.938 DEBUG 18300 --- [nio-8888-exec-1] c.s.s.m.S.selectByPrimaryKey             : <==      Total: 1

7.
创建包及文件夹

(1)在main方法所在类的同级目录创建4个package分别用于controller,service,mapper,entity.
(2)resources下创建 mapping 文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里.

8.
使用mybatis-generator-core  生成entity,mapper接口,mapper.xml
自动生成对应文件名称为SecMaskrule.java,SecMaskruleMapper.java,SecMaskruleMapper.xml
生成文件名称的规律
使用mybatis-generator-core 的配置文件generatorConfig.xml中指定表对应生成的domainObjectName,
其他两个文件直接在后面加上Mapper.java与Mapper.xml

<table tableName="sec_maskrule"
       domainObjectName="SecMaskrule"    
       enableCountByExample="false"
       enableUpdateByExample="false"
       enableDeleteByExample="false"    
       enableSelectByExample="false"
       selectByExampleQueryId="false" >    
            <property name="useActualColumnNames" value="false"/>    
</table>  

9.
添加service接口与实现类和controller实现类


启动springboot项目报错

Description:

Field secMaskruleService in com.snc.secretkeymanagement.controller.SecMaskruleController required a bean of type 'com.snc.secretkeymanagement.service.SecMaskruleService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.snc.secretkeymanagement.service.SecMaskruleService' in your configuration.

解决办法
在serviceImpl的实现类加上 注解 org.springframework.stereotype.Service;   
添加方式在类上添加@Service

重启报错

Description:

Field secMaskruleMapper in com.snc.secretkeymanagement.service.impl.SecMaskruleServiceImpl required a bean of type 'com.snc.secretkeymanagement.mapper.SecMaskruleMapper' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.snc.secretkeymanagement.mapper.SecMaskruleMapper' in your configuration.

尝试解决1  
在 自动生成的SecMaskruleMapper.java接口中添加
注解 import org.springframework.stereotype.Repository
@Repository

重启报错

Description:

Field secMaskruleMapper in com.snc.secretkeymanagement.service.impl.SecMaskruleServiceImpl required a bean of type 'com.snc.secretkeymanagement.mapper.SecMaskruleMapper' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.snc.secretkeymanagement.mapper.SecMaskruleMapper' in your configuration.

依旧报出与尝试解决之前的错误

去掉SecMaskruleMapper.java 上的@Repository注解

尝试解决2
释放
application-dev.yml 配置文件
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.snc.secretkeymanagement.entity

重启报错
 
  Description:

Field secMaskruleMapper in com.snc.secretkeymanagement.service.impl.SecMaskruleServiceImpl required a bean of type 'com.snc.secretkeymanagement.mapper.SecMaskruleMapper' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.snc.secretkeymanagement.mapper.SecMaskruleMapper' in your configuration.


 
依旧报出与尝试解决之前的错误
注释掉application-dev.yml 配置文件
#type-aliases-package: com.snc.secretkeymanagement.entity

尝试解决3
在springboot 项目的启动类上加
@MapperScan("com.snc.secretkeymanagement.mapper")
重启启动不在报错

10.
页面访问
http://localhost:8888/sec/SecMaskrule/selectByPrimaryKey?maskruleId=1
页面报错浏览器发请求后台接收不到
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Nov 22 11:52:54 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

尝试解决1
修改controller 类里的
//@RequestMapping("selectByPrimaryKey/{maskruleId}")
    @RequestMapping("selectByPrimaryKey")
重启访问依旧报错
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Nov 22 11:57:10 CST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Missing URI template variable 'maskruleId' for method parameter of type Long


尝试解决2
去掉请求方法(@PathVariable
//SecMaskrule selectByPrimaryKey(@PathVariable Long maskruleId) {
    SecMaskrule selectByPrimaryKey( Long maskruleId) {
重新访问可以得到后台相应的数据

最终发现是URL写法导致
http://localhost:8888/sec/SecMaskrule/selectByPrimaryKey/1
也就是说
http://localhost:8888/sec/SecMaskrule/selectByPrimaryKey?maskruleId=1
对应controller
@RequestMapping("selectByPrimaryKey")
SecMaskrule selectByPrimaryKey( Long maskruleId) {

接收请求路径中占位符的值@PathVariable("maskruleId")
http://localhost:8888/sec/SecMaskrule/selectByPrimaryKey/1
对应controller
@RequestMapping("selectByPrimaryKey/{maskruleId}")
SecMaskrule selectByPrimaryKey(@PathVariable("maskruleId") Long maskruleId) {

简单的spring 整合mybatis便实现了
同步github
https://github.com/zhangxiaoshan123/secretkeymanagement/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值