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的更多相关文章

  1. 003 SpringBoot整合SpringMVC、Mybatis 案例

    1.不使用骨架创建一个maven工程 2.修改POM.xml文件 <?xml version="1.0" encoding="UTF-8"?> &l ...

  2. 【AngularJS】AngularJS整合Springmvc、Mybatis环境搭建

    近期想学习AngularJS的使用,网上搜了一圈后,折腾了半天解决bug后,成功使用AngularJS整合Springmvc.Spring.Mybatis搭建了一个开发环境.(这里Spring使用的版 ...

  3. 手把手教你整合 SpringMvc+Spring+MyBatis+Maven

    注:该教程是参考孙宇老师的<SpringMvc+Spring+Mybatis+Maven整合视频教程1>整理的,花了我六个多小时,边复习视频边调代码边写教程,保证该教程每一步都能正确执行, ...

  4. spring整合springMVC、mybatis、shiro

    jar包: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding& ...

  5. 【Spring】整合SpringMVC、MyBatis

    在使用xml配置方式的最佳整合方式: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  6. SpringMVC学习(三)整合SpringMVC和MyBatis

    工程结构 导入jar包 配置文件 applicationContext-dao.xml---配置数据源.SqlSessionFactory.mapper扫描器 applicationContext-s ...

  7. SSM 框架 整合<SpringMVC+Spring+MyBatis>

    一 框架的搭建1.建立一个maven项目 2.建立五个module(entity,dao,service,action,web-view) 3.给予它们之间的依赖关系 dao-->entity ...

  8. 整合SpringMVC与Mybatis

    第一步.导包 第二步.配置springmvc springmvc.xml <?xml version="1.0" encoding="UTF-8"?> ...

  9. 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. ...

随机推荐

  1. leetcode-买卖股票最佳时机含冷冻期

    题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: n = len(prices) dp_i_0 ...

  2. Eureka服务治理学习笔记(摘抄)

    1.简介 EureKa在Spring Cloud全家桶中担任着服务的注册与发现的落地实现.Netflix在设计EureKa时遵循着AP原则,它基于R EST的服务,用于定位服务,以实现云端中间层服务发 ...

  3. Python-进程(1)

    目录 操作系统发展史 穿孔卡片 联机批处理系统 统计批处理系统 单道 多道技术 空间上复用 时间上复用 并行与并发 进程 程序与进程 进程调度 进程的三个状态 就绪态 运行态 阻塞态 同步和异步 阻塞 ...

  4. 容斥原理——poj1091

    将m质因子分解,然后枚举选取的质因子个数i进行容斥,每种情况进行一次dfs即可 dfs结束标记:当质因子个数达到i时退出递归,同时累加该解对应的方案数 /* 给定n,m 共有n个数的数组a,不超过m ...

  5. sulin Python3.6爬虫+Djiago2.0+Mysql --实例demo

    1.切换到项目目录下,启动测试服务器 manage.py runserver 192.168.0.108:8888 2.设置相关配置 项目目录展示如下: beauty=>settings.py ...

  6. 深入浅出 Java Concurrency (35): 线程池 part 8 线程池的实现及原理 (3)[转]

    线程池任务执行结果 这一节来探讨下线程池中任务执行的结果以及如何阻塞线程.取消任务等等. 1 package info.imxylz.study.concurrency.future;2 3 publ ...

  7. PAT甲级——A1091 Acute Stroke【30】

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  8. exiftool(-k)与gui的联合使用

    首先下载一个exiftool下载后改名字https://sno.phy.queensu.ca/~phil/exiftool/ 根据自己的操作系统选择,我需要这个 然后下载guihttp://u88.n ...

  9. Linux查看温度

    step 1: centos $ sudo yum install lm_sensors ubuntu $ sudo apt-get install lm_sensors step2$ sudo se ...

  10. Servlet与Struts的区别

    启动: ● Servlet:无 ● Struts:配置filter,设置struts入口 创建: ● Servlet:继承HttpServlet,重写doGet与doPost方法: 添加注解或配置we ...