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. ...
随机推荐
- 2019-8-31-dotnet-特性-DynamicallyInvokable-是用来做什么的
title author date CreateTime categories dotnet 特性 DynamicallyInvokable 是用来做什么的 lindexi 2019-08-31 16 ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...
- docker 个人遇到问题日志记录
system: openSUSE Leap 42.3 在openSUSE中可直接运行" sudo zypper in docker"进行安装docker-ce wakasann@l ...
- 【JZOJ3400】旅行
description 从前有一位旅者,他想要游遍天下所有的景点.这一天他来到了一个神奇的王国:在这片土地上,有n个城市,从1到n进行编号.王国中有m条道路,第i条道路连接着两个城市ai,bi,由于年 ...
- thinkphp Mongo模型
Mongo模型是专门为Mongo数据库驱动而支持的Model扩展,如果需要操作Mongo数据库的话,自定义的模型类必须继承Think\Model\MongoModel. Mongo模型为操作Mongo ...
- zabbix被监控端代理设置
zabbix被监控端代理设置 安装zabbix-agent客户端 rpm -ivh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-re ...
- php多维数组
一个数组中的值可以是另一个数组,另一个数组的值也可以是一个数组.依照这种方式,我们可以创建二维或者三维数组: 实例 <?php // A two-dimensional array: $cars ...
- PAT甲级——A1068 Find More Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- php从5.6升级到php7后,扩展出现segment fault的问题解决
php7的文档中有这样的描述: Both mistakes might cause memory corruptions and segfaults:1)char *str;long str_len; ...
- ORC格式hive逻辑中case when问题
前阵子做hive开发发现orc格式文件使用case when情况下会造成nullcount会统计入内问题,修改为sum就没此问题.具体例子下次放假回来记录,现在不在公司,这里做个mark