TZ_11_Spring-Boot的整合SpringMvc和MyBatis
1.整合SpringMVC
虽然默认配置已经可以使用SpringMVC了,不过我们有时候需要进行自定义配置。
1>修改方式 通过application.yaml 此名字不需要使用@PropertySource("classpath:jdbc.properties")
server:
#配置端口号
port: 8080
#配置前端控制器
servlet:
path: "*.html"
#配置jdbc
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shop
username: root
password: 12345
#配置别名
mybatis:
type-aliases-package: com.hdh.pojo
2.jar工程访问静态资源(基本用不上)
在ResourceProperties类中明确表明了默认加载静态资源的文件夹 只要静态资源放在这些目录中任何一个,SpringMVC都会帮我们处理。
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
3.整合mybatis
1>SpringBoot官方并没有提供Mybatis的启动器,不过Mybatis官网自己实现了:
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
配置,基本没有需要配置的:
# mybatis 别名扫描
mybatis.type-aliases-package=com.heima.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xml
需要注意,这里没有配置mapper接口扫描包,因此我们需要给每一个Mapper接口添加@Mapper注解,才能被识别。
@Mapper// 为了把mapper这个DAO交給Spring管理 不需要配置mapper映射文件 mapper-locations:
interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {
}
2>同用mapper 自动生成MySQL单表查询的基础语句
使用方法:
启动器:中包含了jdbc和mybatis的启动器
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
4.整合注解
1>实体类的注解
@Data//自动生成get set方法
@Table(name = "tb_user")
public class User implements Serializable {
@Id //设置主键
@KeySql(useGeneratedKeys = true) //主键自增长
private Integer id;
private Date updated;
@Transient //不需要持久化的字段
private String note;
}
5.如果不想每次都写private final Logger logger = LoggerFactory.getLogger(XXX.class); 可以用注解@Slf4j
SLF4J所提供的核心API是一些接口以及一个LoggerFactory的工厂类。SLF4J提供了统一的记录日志的接口,只要按照其提供的方法记录即可,最终日志的格式、记录级别、输出方式等通过具体日志系统的配置来实现,因此可以在应用中灵活切换日志系统。
启动器
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
6.事务控制
SpringBoot中通过注解来控制。就是我们熟知的@Transactional
@Transactional
public void deleteById(Long id){
this.userMapper.deleteByPrimaryKey(id);
}
TZ_11_Spring-Boot的整合SpringMvc和MyBatis的更多相关文章
- 003 SpringBoot整合SpringMVC、Mybatis 案例
1.不使用骨架创建一个maven工程 2.修改POM.xml文件 <?xml version="1.0" encoding="UTF-8"?> &l ...
- 【AngularJS】AngularJS整合Springmvc、Mybatis环境搭建
近期想学习AngularJS的使用,网上搜了一圈后,折腾了半天解决bug后,成功使用AngularJS整合Springmvc.Spring.Mybatis搭建了一个开发环境.(这里Spring使用的版 ...
- 手把手教你整合 SpringMvc+Spring+MyBatis+Maven
注:该教程是参考孙宇老师的<SpringMvc+Spring+Mybatis+Maven整合视频教程1>整理的,花了我六个多小时,边复习视频边调代码边写教程,保证该教程每一步都能正确执行, ...
- spring整合springMVC、mybatis、shiro
jar包: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding& ...
- 【Spring】整合SpringMVC、MyBatis
在使用xml配置方式的最佳整合方式: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...
- SpringMVC学习(三)整合SpringMVC和MyBatis
工程结构 导入jar包 配置文件 applicationContext-dao.xml---配置数据源.SqlSessionFactory.mapper扫描器 applicationContext-s ...
- SSM 框架 整合<SpringMVC+Spring+MyBatis>
一 框架的搭建1.建立一个maven项目 2.建立五个module(entity,dao,service,action,web-view) 3.给予它们之间的依赖关系 dao-->entity ...
- 整合SpringMVC与Mybatis
第一步.导包 第二步.配置springmvc springmvc.xml <?xml version="1.0" encoding="UTF-8"?> ...
- spring整合springMVC、mybatis、hibernate、mongodb框架
开发环境 eclipse Mars 4.5 JDK 1.7 框架 spring 4.0.5 mybatis 3.2.7 hibernate 4.3.6 mongodb 1.7 数据库 MySQL 5. ...
随机推荐
- /bin /usr/bin /sbin /usr/sbin 目录的作用
/bin是系统的一些指令.bin为binary的简写主要放置一些系统的必备执行档例如:cat.cp.chmod df.dmesg.gzip.kill.ls.mkdir.more.mount.rm.su ...
- 动软DbHelperSQL
using System; using System.Collections; using System.Data; using System.Data.SqlClient; using System ...
- 中国剩余定理模数不互质的情况(poj 2891
中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...
- barrel shifter, logarthmic shifter and funnel shifter
1,shifter小集合 (1) simple shift 左移或右移补0 (2) arthmetic shift 左移补0,右移补符号位 (3) barrel shifter 桶型,顾名思义,应该头 ...
- Apache Flink 1.9.0版本新功能介绍
摘要:Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台,它能够基于同一个Flink运行时,提供支持流处理和批处理两种类型应用的功能.目前,Apache Flink 1.9 ...
- 从微服务治理的角度看RSocket、. Envoy和. Istio
很多同学看到这个题目,一定会提这样的问题:RSocket是个协议,Envoy是一个 proxy,Istio是service mesh control plane + data plane. 这三种技术 ...
- 修改cmd命令默认路径
未修改之前: 修改方法: 1.win+r打开运行对话框,输入 regedit 打开注册表编辑器 2.在注册表中找到:HKEY_CURRENT_USER\Software\Microsoft\Comma ...
- GCC 参数详解
转载出处:http://blog.csdn.net/yff1030/article/details/8592077 原文:http://www.cppblog.com/SEMAN/archive/20 ...
- PAT甲级——A1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- 安装office2019
win10系统安装office2019 安装文件下载 https://pan.baidu.com/s/1VnqJ-hNwysPKBhdzE3FSww#list/path=%2F&parentP ...